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 { |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 401 | // OpenCL kernels are called via an explicit runtime API with arguments |
| 402 | // set with clSetKernelArg(), not as normal sub-functions. |
| 403 | // Return SPIR_KERNEL by default as the kernel calling convention to |
| 404 | // ensure the fingerprint is fixed such way that each OpenCL argument |
| 405 | // gets one matching argument in the produced kernel function argument |
| 406 | // list to enable feasible implementation of clSetKernelArg() with |
| 407 | // aggregates etc. In case we would use the default C calling conv here, |
| 408 | // clSetKernelArg() might break depending on the target-specific |
| 409 | // conventions; different targets might split structs passed as values |
| 410 | // to multiple function arguments etc. |
| 411 | return llvm::CallingConv::SPIR_KERNEL; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 412 | } |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 413 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 414 | llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 415 | llvm::PointerType *T, QualType QT) const { |
| 416 | return llvm::ConstantPointerNull::get(T); |
| 417 | } |
| 418 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 419 | unsigned TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 420 | const VarDecl *D) const { |
| 421 | assert(!CGM.getLangOpts().OpenCL && |
| 422 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 423 | "Address space agnostic languages only"); |
Yaxun Liu | 7bce642 | 2017-07-08 19:13:41 +0000 | [diff] [blame] | 424 | return D ? D->getType().getAddressSpace() |
| 425 | : static_cast<unsigned>(LangAS::Default); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 428 | llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 429 | CodeGen::CodeGenFunction &CGF, llvm::Value *Src, unsigned SrcAddr, |
| 430 | unsigned DestAddr, llvm::Type *DestTy, bool isNonNull) const { |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 431 | // Since target may map different address spaces in AST to the same address |
| 432 | // space, an address space conversion may end up as a bitcast. |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 433 | if (auto *C = dyn_cast<llvm::Constant>(Src)) |
| 434 | return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 435 | return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DestTy); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 438 | llvm::Constant * |
| 439 | TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src, |
| 440 | unsigned SrcAddr, unsigned DestAddr, |
| 441 | llvm::Type *DestTy) const { |
| 442 | // Since target may map different address spaces in AST to the same address |
| 443 | // space, an address space conversion may end up as a bitcast. |
| 444 | return llvm::ConstantExpr::getPointerCast(Src, DestTy); |
| 445 | } |
| 446 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 447 | llvm::SyncScope::ID |
| 448 | TargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, llvm::LLVMContext &C) const { |
| 449 | return C.getOrInsertSyncScopeID(""); /* default sync scope */ |
| 450 | } |
| 451 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 452 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 453 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 454 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 455 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 456 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 457 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 458 | if (FD->isUnnamedBitfield()) |
| 459 | return true; |
| 460 | |
| 461 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 462 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 463 | // Constant arrays of empty records count as empty, strip them off. |
| 464 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 465 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 466 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 467 | if (AT->getSize() == 0) |
| 468 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 469 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 470 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 471 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 472 | const RecordType *RT = FT->getAs<RecordType>(); |
| 473 | if (!RT) |
| 474 | return false; |
| 475 | |
| 476 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 477 | // |
| 478 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 479 | // current ABI. |
| 480 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 481 | return false; |
| 482 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 483 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 486 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 487 | /// fields. Note that a structure with a flexible array member is not |
| 488 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 489 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 490 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 491 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 492 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 493 | const RecordDecl *RD = RT->getDecl(); |
| 494 | if (RD->hasFlexibleArrayMember()) |
| 495 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 496 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 497 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 498 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 499 | for (const auto &I : CXXRD->bases()) |
| 500 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 501 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 502 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 503 | for (const auto *I : RD->fields()) |
| 504 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 505 | return false; |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | /// isSingleElementStruct - Determine if a structure is a "single |
| 510 | /// element struct", i.e. it has exactly one non-empty field or |
| 511 | /// exactly one field which is itself a single element |
| 512 | /// struct. Structures with flexible array members are never |
| 513 | /// considered single element structs. |
| 514 | /// |
| 515 | /// \return The field declaration for the single non-empty field, if |
| 516 | /// it exists. |
| 517 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 518 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 519 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 520 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 521 | |
| 522 | const RecordDecl *RD = RT->getDecl(); |
| 523 | if (RD->hasFlexibleArrayMember()) |
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 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 526 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 527 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 528 | // If this is a C++ record, check the bases first. |
| 529 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 530 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 531 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 532 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 533 | continue; |
| 534 | |
| 535 | // If we already found an element then this isn't a single-element struct. |
| 536 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 537 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 538 | |
| 539 | // If this is non-empty and not a single element struct, the composite |
| 540 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 541 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 542 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 543 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 548 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 549 | QualType FT = FD->getType(); |
| 550 | |
| 551 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 552 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 553 | continue; |
| 554 | |
| 555 | // If we already found an element then this isn't a single-element |
| 556 | // struct. |
| 557 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 558 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 559 | |
| 560 | // Treat single element arrays as the element. |
| 561 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 562 | if (AT->getSize().getZExtValue() != 1) |
| 563 | break; |
| 564 | FT = AT->getElementType(); |
| 565 | } |
| 566 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 567 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 568 | Found = FT.getTypePtr(); |
| 569 | } else { |
| 570 | Found = isSingleElementStruct(FT, Context); |
| 571 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 572 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 576 | // We don't consider a struct a single-element struct if it has |
| 577 | // padding beyond the element type. |
| 578 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 579 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 580 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 581 | return Found; |
| 582 | } |
| 583 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 584 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 585 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 586 | const ABIArgInfo &AI) { |
| 587 | // This default implementation defers to the llvm backend's va_arg |
| 588 | // instruction. It can handle only passing arguments directly |
| 589 | // (typically only handled in the backend for primitive types), or |
| 590 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 591 | // flag has ABI impact in the callee, this implementation cannot |
| 592 | // work.) |
| 593 | |
| 594 | // Only a few cases are covered here at the moment -- those needed |
| 595 | // by the default abi. |
| 596 | llvm::Value *Val; |
| 597 | |
| 598 | if (AI.isIndirect()) { |
| 599 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 600 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 601 | assert( |
| 602 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 603 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 604 | |
| 605 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 606 | CharUnits TyAlignForABI = TyInfo.second; |
| 607 | |
| 608 | llvm::Type *BaseTy = |
| 609 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 610 | llvm::Value *Addr = |
| 611 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 612 | return Address(Addr, TyAlignForABI); |
| 613 | } else { |
| 614 | assert((AI.isDirect() || AI.isExtend()) && |
| 615 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 616 | |
| 617 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 618 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 619 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 620 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 621 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 622 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 623 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 624 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 625 | |
| 626 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 627 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 628 | CGF.Builder.CreateStore(Val, Temp); |
| 629 | return Temp; |
| 630 | } |
| 631 | } |
| 632 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 633 | /// DefaultABIInfo - The default implementation for ABI specific |
| 634 | /// details. This implementation provides information which results in |
| 635 | /// self-consistent and sensible LLVM IR generation, but does not |
| 636 | /// conform to any particular ABI. |
| 637 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 638 | public: |
| 639 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 640 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 641 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 642 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 643 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 644 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 645 | if (!getCXXABI().classifyReturnType(FI)) |
| 646 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 647 | for (auto &I : FI.arguments()) |
| 648 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 649 | } |
| 650 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 651 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 652 | QualType Ty) const override { |
| 653 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 654 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 655 | }; |
| 656 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 657 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 658 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 659 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 660 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 661 | }; |
| 662 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 663 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 664 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 665 | |
| 666 | if (isAggregateTypeForABI(Ty)) { |
| 667 | // Records with non-trivial destructors/copy-constructors should not be |
| 668 | // passed by value. |
| 669 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 670 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 671 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 672 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 673 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 674 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 675 | // Treat an enum type as its underlying type. |
| 676 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 677 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 678 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 679 | return (Ty->isPromotableIntegerType() ? |
| 680 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 683 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 684 | if (RetTy->isVoidType()) |
| 685 | return ABIArgInfo::getIgnore(); |
| 686 | |
| 687 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 688 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 689 | |
| 690 | // Treat an enum type as its underlying type. |
| 691 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 692 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 693 | |
| 694 | return (RetTy->isPromotableIntegerType() ? |
| 695 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 696 | } |
| 697 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 698 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 699 | // WebAssembly ABI Implementation |
| 700 | // |
| 701 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 702 | //===----------------------------------------------------------------------===// |
| 703 | |
| 704 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 705 | public: |
| 706 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 707 | : DefaultABIInfo(CGT) {} |
| 708 | |
| 709 | private: |
| 710 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 711 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 712 | |
| 713 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 714 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 715 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 716 | void computeInfo(CGFunctionInfo &FI) const override { |
| 717 | if (!getCXXABI().classifyReturnType(FI)) |
| 718 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 719 | for (auto &Arg : FI.arguments()) |
| 720 | Arg.info = classifyArgumentType(Arg.type); |
| 721 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 722 | |
| 723 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 724 | QualType Ty) const override; |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 725 | }; |
| 726 | |
| 727 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 728 | public: |
| 729 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 730 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 731 | }; |
| 732 | |
| 733 | /// \brief Classify argument of given type \p Ty. |
| 734 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 735 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 736 | |
| 737 | if (isAggregateTypeForABI(Ty)) { |
| 738 | // Records with non-trivial destructors/copy-constructors should not be |
| 739 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 740 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 741 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 742 | // Ignore empty structs/unions. |
| 743 | if (isEmptyRecord(getContext(), Ty, true)) |
| 744 | return ABIArgInfo::getIgnore(); |
| 745 | // Lower single-element structs to just pass a regular value. TODO: We |
| 746 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 747 | // though watch out for things like bitfields. |
| 748 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 749 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | // Otherwise just do the default thing. |
| 753 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 754 | } |
| 755 | |
| 756 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 757 | if (isAggregateTypeForABI(RetTy)) { |
| 758 | // Records with non-trivial destructors/copy-constructors should not be |
| 759 | // returned by value. |
| 760 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 761 | // Ignore empty structs/unions. |
| 762 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 763 | return ABIArgInfo::getIgnore(); |
| 764 | // Lower single-element structs to just return a regular value. TODO: We |
| 765 | // could do reasonable-size multiple-element structs too, using |
| 766 | // ABIArgInfo::getDirect(). |
| 767 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 768 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | // Otherwise just do the default thing. |
| 773 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 774 | } |
| 775 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 776 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 777 | QualType Ty) const { |
| 778 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 779 | getContext().getTypeInfoInChars(Ty), |
| 780 | CharUnits::fromQuantity(4), |
| 781 | /*AllowHigherAlign=*/ true); |
| 782 | } |
| 783 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 784 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 785 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 786 | // |
| 787 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 788 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 789 | //===----------------------------------------------------------------------===// |
| 790 | |
| 791 | class PNaClABIInfo : public ABIInfo { |
| 792 | public: |
| 793 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 794 | |
| 795 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 796 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 797 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 798 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 799 | Address EmitVAArg(CodeGenFunction &CGF, |
| 800 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 801 | }; |
| 802 | |
| 803 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 804 | public: |
| 805 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 806 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 807 | }; |
| 808 | |
| 809 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 810 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 811 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 812 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 813 | for (auto &I : FI.arguments()) |
| 814 | I.info = classifyArgumentType(I.type); |
| 815 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 816 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 817 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 818 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 819 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 820 | // function classification. Structs get passed directly for varargs |
| 821 | // functions, through a rewriting transform in |
| 822 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 823 | // this target to actually support a va_arg instructions with an |
| 824 | // aggregate type, unlike other targets. |
| 825 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 828 | /// \brief Classify argument of given type \p Ty. |
| 829 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 830 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 831 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 832 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 833 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 834 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 835 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 836 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 837 | } else if (Ty->isFloatingType()) { |
| 838 | // Floating-point types don't go inreg. |
| 839 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 840 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 841 | |
| 842 | return (Ty->isPromotableIntegerType() ? |
| 843 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 847 | if (RetTy->isVoidType()) |
| 848 | return ABIArgInfo::getIgnore(); |
| 849 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 850 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 851 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 852 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 853 | |
| 854 | // Treat an enum type as its underlying type. |
| 855 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 856 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 857 | |
| 858 | return (RetTy->isPromotableIntegerType() ? |
| 859 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 860 | } |
| 861 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 862 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 863 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 864 | // 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] | 865 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 866 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 867 | IRType->getScalarSizeInBits() != 64; |
| 868 | } |
| 869 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 870 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 871 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 872 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 873 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 874 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 875 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 876 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 879 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 883 | return Ty; |
| 884 | } |
| 885 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 886 | /// Returns true if this type can be passed in SSE registers with the |
| 887 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 888 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 889 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 890 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { |
| 891 | if (BT->getKind() == BuiltinType::LongDouble) { |
| 892 | if (&Context.getTargetInfo().getLongDoubleFormat() == |
| 893 | &llvm::APFloat::x87DoubleExtended()) |
| 894 | return false; |
| 895 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 896 | return true; |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 897 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 898 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 899 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 900 | // registers specially. |
| 901 | unsigned VecSize = Context.getTypeSize(VT); |
| 902 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 903 | return true; |
| 904 | } |
| 905 | return false; |
| 906 | } |
| 907 | |
| 908 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 909 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 910 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 911 | return NumMembers <= 4; |
| 912 | } |
| 913 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 914 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 915 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 916 | auto AI = ABIArgInfo::getDirect(T); |
| 917 | AI.setInReg(true); |
| 918 | AI.setCanBeFlattened(false); |
| 919 | return AI; |
| 920 | } |
| 921 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 922 | //===----------------------------------------------------------------------===// |
| 923 | // X86-32 ABI Implementation |
| 924 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 925 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 926 | /// \brief Similar to llvm::CCState, but for Clang. |
| 927 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 928 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 929 | |
| 930 | unsigned CC; |
| 931 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 932 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 933 | }; |
| 934 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 935 | enum { |
| 936 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 937 | VectorcallMaxParamNumAsReg = 6 |
| 938 | }; |
| 939 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 940 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 941 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 942 | enum Class { |
| 943 | Integer, |
| 944 | Float |
| 945 | }; |
| 946 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 947 | static const unsigned MinABIStackAlignInBytes = 4; |
| 948 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 949 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 950 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 951 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 952 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 953 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 954 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 955 | |
| 956 | static bool isRegisterSize(unsigned Size) { |
| 957 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 958 | } |
| 959 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 960 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 961 | // FIXME: Assumes vectorcall is in use. |
| 962 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 963 | } |
| 964 | |
| 965 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 966 | uint64_t NumMembers) const override { |
| 967 | // FIXME: Assumes vectorcall is in use. |
| 968 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 969 | } |
| 970 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 971 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 972 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 973 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 974 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 975 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 976 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 977 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 978 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 979 | /// \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] | 980 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 981 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 982 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 983 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 984 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 985 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 986 | /// \brief Updates the number of available free registers, returns |
| 987 | /// true if any registers were allocated. |
| 988 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 989 | |
| 990 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 991 | bool &NeedsPadding) const; |
| 992 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 993 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 994 | bool canExpandIndirectArgument(QualType Ty) const; |
| 995 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 996 | /// \brief Rewrite the function info so that all memory arguments use |
| 997 | /// inalloca. |
| 998 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 999 | |
| 1000 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1001 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1002 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1003 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1004 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1005 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 1006 | public: |
| 1007 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1008 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1009 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1010 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1011 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1012 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1013 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1014 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1015 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1016 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 1017 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1018 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1019 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1020 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1021 | |
| 1022 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 1023 | ArrayRef<llvm::Type*> scalars, |
| 1024 | bool asReturnValue) const override { |
| 1025 | // LLVM's x86-32 lowering currently only assigns up to three |
| 1026 | // integer registers and three fp registers. Oddly, it'll use up to |
| 1027 | // four vector registers for vectors, but those can overlap with the |
| 1028 | // scalar registers. |
| 1029 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
| 1030 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 1031 | |
| 1032 | bool isSwiftErrorInRegister() const override { |
| 1033 | // x86-32 lowering does not support passing swifterror in a register. |
| 1034 | return false; |
| 1035 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1036 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1037 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1038 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1039 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1040 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1041 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1042 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 1043 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1044 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 1045 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1046 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1047 | static bool isStructReturnInRegABI( |
| 1048 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1049 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1050 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1051 | CodeGen::CodeGenModule &CGM, |
| 1052 | ForDefinition_t IsForDefinition) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1053 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1054 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1055 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1056 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1057 | return 4; |
| 1058 | } |
| 1059 | |
| 1060 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1061 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1062 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1063 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1064 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1065 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1066 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1067 | } |
| 1068 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1069 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1070 | std::string &Constraints, |
| 1071 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1072 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1073 | std::vector<LValue> &ResultRegDests, |
| 1074 | std::string &AsmString, |
| 1075 | unsigned NumOutputs) const override; |
| 1076 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1077 | llvm::Constant * |
| 1078 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1079 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1080 | (0x06 << 8) | // .+0x08 |
| 1081 | ('F' << 16) | |
| 1082 | ('T' << 24); |
| 1083 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1084 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1085 | |
| 1086 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1087 | return "movl\t%ebp, %ebp" |
| 1088 | "\t\t## marker for objc_retainAutoreleaseReturnValue"; |
| 1089 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1090 | }; |
| 1091 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1092 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1093 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1094 | /// Rewrite input constraint references after adding some output constraints. |
| 1095 | /// In the case where there is one output and one input and we add one output, |
| 1096 | /// we need to replace all operand references greater than or equal to 1: |
| 1097 | /// mov $0, $1 |
| 1098 | /// mov eax, $1 |
| 1099 | /// The result will be: |
| 1100 | /// mov $0, $2 |
| 1101 | /// mov eax, $2 |
| 1102 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1103 | unsigned NumNewOuts, |
| 1104 | std::string &AsmString) { |
| 1105 | std::string Buf; |
| 1106 | llvm::raw_string_ostream OS(Buf); |
| 1107 | size_t Pos = 0; |
| 1108 | while (Pos < AsmString.size()) { |
| 1109 | size_t DollarStart = AsmString.find('$', Pos); |
| 1110 | if (DollarStart == std::string::npos) |
| 1111 | DollarStart = AsmString.size(); |
| 1112 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1113 | if (DollarEnd == std::string::npos) |
| 1114 | DollarEnd = AsmString.size(); |
| 1115 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1116 | Pos = DollarEnd; |
| 1117 | size_t NumDollars = DollarEnd - DollarStart; |
| 1118 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1119 | // We have an operand reference. |
| 1120 | size_t DigitStart = Pos; |
| 1121 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1122 | if (DigitEnd == std::string::npos) |
| 1123 | DigitEnd = AsmString.size(); |
| 1124 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1125 | unsigned OperandIndex; |
| 1126 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1127 | if (OperandIndex >= FirstIn) |
| 1128 | OperandIndex += NumNewOuts; |
| 1129 | OS << OperandIndex; |
| 1130 | } else { |
| 1131 | OS << OperandStr; |
| 1132 | } |
| 1133 | Pos = DigitEnd; |
| 1134 | } |
| 1135 | } |
| 1136 | AsmString = std::move(OS.str()); |
| 1137 | } |
| 1138 | |
| 1139 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1140 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1141 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1142 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1143 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1144 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1145 | unsigned NumOutputs) const { |
| 1146 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1147 | |
| 1148 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1149 | // larger. |
| 1150 | if (!Constraints.empty()) |
| 1151 | Constraints += ','; |
| 1152 | if (RetWidth <= 32) { |
| 1153 | Constraints += "={eax}"; |
| 1154 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1155 | } else { |
| 1156 | // Use the 'A' constraint for EAX:EDX. |
| 1157 | Constraints += "=A"; |
| 1158 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1159 | } |
| 1160 | |
| 1161 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1162 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1163 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1164 | |
| 1165 | // Coerce the integer by bitcasting the return slot pointer. |
| 1166 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1167 | CoerceTy->getPointerTo())); |
| 1168 | ResultRegDests.push_back(ReturnSlot); |
| 1169 | |
| 1170 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1171 | } |
| 1172 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1173 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1174 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1175 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1176 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1177 | uint64_t Size = Context.getTypeSize(Ty); |
| 1178 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1179 | // For i386, type must be register sized. |
| 1180 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1181 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1182 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1183 | |
| 1184 | if (Ty->isVectorType()) { |
| 1185 | // 64- and 128- bit vectors inside structures are not returned in |
| 1186 | // registers. |
| 1187 | if (Size == 64 || Size == 128) |
| 1188 | return false; |
| 1189 | |
| 1190 | return true; |
| 1191 | } |
| 1192 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1193 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1194 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1195 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1196 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1197 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1198 | return true; |
| 1199 | |
| 1200 | // Arrays are treated like records. |
| 1201 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1202 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1203 | |
| 1204 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1205 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1206 | if (!RT) return false; |
| 1207 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1208 | // FIXME: Traverse bases here too. |
| 1209 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1210 | // Structure types are passed in register if all fields would be |
| 1211 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1212 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1213 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1214 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1215 | continue; |
| 1216 | |
| 1217 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1218 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1219 | return false; |
| 1220 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1221 | return true; |
| 1222 | } |
| 1223 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1224 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1225 | // Treat complex types as the element type. |
| 1226 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1227 | Ty = CTy->getElementType(); |
| 1228 | |
| 1229 | // Check for a type which we know has a simple scalar argument-passing |
| 1230 | // convention without any padding. (We're specifically looking for 32 |
| 1231 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1232 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1233 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1234 | return false; |
| 1235 | |
| 1236 | uint64_t Size = Context.getTypeSize(Ty); |
| 1237 | return Size == 32 || Size == 64; |
| 1238 | } |
| 1239 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1240 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1241 | uint64_t &Size) { |
| 1242 | for (const auto *FD : RD->fields()) { |
| 1243 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1244 | // argument is smaller than 32-bits, expanding the struct will create |
| 1245 | // alignment padding. |
| 1246 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1247 | return false; |
| 1248 | |
| 1249 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1250 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1251 | // counts as "basic" is more complicated than what we were doing previously. |
| 1252 | if (FD->isBitField()) |
| 1253 | return false; |
| 1254 | |
| 1255 | Size += Context.getTypeSize(FD->getType()); |
| 1256 | } |
| 1257 | return true; |
| 1258 | } |
| 1259 | |
| 1260 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1261 | uint64_t &Size) { |
| 1262 | // Don't do this if there are any non-empty bases. |
| 1263 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1264 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1265 | Size)) |
| 1266 | return false; |
| 1267 | } |
| 1268 | if (!addFieldSizes(Context, RD, Size)) |
| 1269 | return false; |
| 1270 | return true; |
| 1271 | } |
| 1272 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1273 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1274 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1275 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1276 | /// optimizations. |
| 1277 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1278 | // We can only expand structure types. |
| 1279 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1280 | if (!RT) |
| 1281 | return false; |
| 1282 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1283 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1284 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1285 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1286 | // On non-Windows, we have to conservatively match our old bitcode |
| 1287 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1288 | if (!CXXRD->isCLike()) |
| 1289 | return false; |
| 1290 | } else { |
| 1291 | // Don't do this for dynamic classes. |
| 1292 | if (CXXRD->isDynamicClass()) |
| 1293 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1294 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1295 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1296 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1297 | } else { |
| 1298 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1299 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | // We can do this if there was no alignment padding. |
| 1303 | return Size == getContext().getTypeSize(Ty); |
| 1304 | } |
| 1305 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1306 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1307 | // If the return value is indirect, then the hidden argument is consuming one |
| 1308 | // integer register. |
| 1309 | if (State.FreeRegs) { |
| 1310 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1311 | if (!IsMCUABI) |
| 1312 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1313 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1314 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1317 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1318 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1319 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1320 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1321 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1322 | const Type *Base = nullptr; |
| 1323 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1324 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1325 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1326 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1327 | // The LLVM struct type for such an aggregate should lower properly. |
| 1328 | return ABIArgInfo::getDirect(); |
| 1329 | } |
| 1330 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1331 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1332 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1333 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1334 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1335 | |
| 1336 | // 128-bit vectors are a special case; they are returned in |
| 1337 | // registers and we need to make sure to pick a type the LLVM |
| 1338 | // backend will like. |
| 1339 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1340 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1341 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1342 | |
| 1343 | // Always return in register if it fits in a general purpose |
| 1344 | // register, or if it is 64 bits and has a single element. |
| 1345 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1346 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1347 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1348 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1349 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1350 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1354 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1355 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1356 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1357 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1358 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1359 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1360 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1361 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1362 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1363 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1364 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1365 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1366 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1367 | // Ignore empty structs/unions. |
| 1368 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1369 | return ABIArgInfo::getIgnore(); |
| 1370 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1371 | // Small structures which are register sized are generally returned |
| 1372 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1373 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1374 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1375 | |
| 1376 | // As a special-case, if the struct is a "single-element" struct, and |
| 1377 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1378 | // floating-point register. (MSVC does not apply this special case.) |
| 1379 | // We apply a similar transformation for pointer types to improve the |
| 1380 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1381 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1382 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1383 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1384 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1385 | |
| 1386 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1387 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1388 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1391 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1392 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1393 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1394 | // Treat an enum type as its underlying type. |
| 1395 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1396 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1397 | |
| 1398 | return (RetTy->isPromotableIntegerType() ? |
| 1399 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1402 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1403 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1404 | } |
| 1405 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1406 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1407 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1408 | if (!RT) |
| 1409 | return 0; |
| 1410 | const RecordDecl *RD = RT->getDecl(); |
| 1411 | |
| 1412 | // If this is a C++ record, check the bases first. |
| 1413 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1414 | for (const auto &I : CXXRD->bases()) |
| 1415 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1416 | return false; |
| 1417 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1418 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1419 | QualType FT = i->getType(); |
| 1420 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1421 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1422 | return true; |
| 1423 | |
| 1424 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1425 | return true; |
| 1426 | } |
| 1427 | |
| 1428 | return false; |
| 1429 | } |
| 1430 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1431 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1432 | unsigned Align) const { |
| 1433 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1434 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1435 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1436 | return 0; // Use default alignment. |
| 1437 | |
| 1438 | // On non-Darwin, the stack type alignment is always 4. |
| 1439 | if (!IsDarwinVectorABI) { |
| 1440 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1441 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1442 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1443 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1444 | // 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] | 1445 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1446 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1447 | return 16; |
| 1448 | |
| 1449 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1452 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1453 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1454 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1455 | if (State.FreeRegs) { |
| 1456 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1457 | if (!IsMCUABI) |
| 1458 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1459 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1460 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1461 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1462 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1463 | // Compute the byval alignment. |
| 1464 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1465 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1466 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1467 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1468 | |
| 1469 | // If the stack alignment is less than the type alignment, realign the |
| 1470 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1471 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1472 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1473 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1476 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1477 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1478 | if (!T) |
| 1479 | T = Ty.getTypePtr(); |
| 1480 | |
| 1481 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1482 | BuiltinType::Kind K = BT->getKind(); |
| 1483 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1484 | return Float; |
| 1485 | } |
| 1486 | return Integer; |
| 1487 | } |
| 1488 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1489 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1490 | if (!IsSoftFloatABI) { |
| 1491 | Class C = classify(Ty); |
| 1492 | if (C == Float) |
| 1493 | return false; |
| 1494 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1495 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1496 | unsigned Size = getContext().getTypeSize(Ty); |
| 1497 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1498 | |
| 1499 | if (SizeInRegs == 0) |
| 1500 | return false; |
| 1501 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1502 | if (!IsMCUABI) { |
| 1503 | if (SizeInRegs > State.FreeRegs) { |
| 1504 | State.FreeRegs = 0; |
| 1505 | return false; |
| 1506 | } |
| 1507 | } else { |
| 1508 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1509 | // earlier parameters that are passed on the stack. Also, |
| 1510 | // it does not allow passing >8-byte structs in-register, |
| 1511 | // even if there are 3 free registers available. |
| 1512 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1513 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1514 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1515 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1516 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1517 | return true; |
| 1518 | } |
| 1519 | |
| 1520 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
| 1521 | bool &InReg, |
| 1522 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1523 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1524 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1525 | // (HFAs) have already been dealt with at this point. |
| 1526 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1527 | return false; |
| 1528 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1529 | NeedsPadding = false; |
| 1530 | InReg = !IsMCUABI; |
| 1531 | |
| 1532 | if (!updateFreeRegs(Ty, State)) |
| 1533 | return false; |
| 1534 | |
| 1535 | if (IsMCUABI) |
| 1536 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1537 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1538 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1539 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1540 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1541 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1542 | NeedsPadding = true; |
| 1543 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1544 | return false; |
| 1545 | } |
| 1546 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1547 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1550 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1551 | if (!updateFreeRegs(Ty, State)) |
| 1552 | return false; |
| 1553 | |
| 1554 | if (IsMCUABI) |
| 1555 | return false; |
| 1556 | |
| 1557 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1558 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1559 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1560 | if (getContext().getTypeSize(Ty) > 32) |
| 1561 | return false; |
| 1562 | |
| 1563 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
| 1564 | Ty->isReferenceType()); |
| 1565 | } |
| 1566 | |
| 1567 | return true; |
| 1568 | } |
| 1569 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1570 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1571 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1572 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1573 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1574 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1575 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1576 | // Check with the C++ ABI first. |
| 1577 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1578 | if (RT) { |
| 1579 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1580 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1581 | return getIndirectResult(Ty, false, State); |
| 1582 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1583 | // The field index doesn't matter, we'll fix it up later. |
| 1584 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1585 | } |
| 1586 | } |
| 1587 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1588 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1589 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1590 | const Type *Base = nullptr; |
| 1591 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1592 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1593 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1594 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1595 | if (State.FreeSSERegs >= NumElts) { |
| 1596 | State.FreeSSERegs -= NumElts; |
| 1597 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1598 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1599 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1600 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1601 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1605 | // Structures with flexible arrays are always indirect. |
| 1606 | // FIXME: This should not be byval! |
| 1607 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1608 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1609 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1610 | // Ignore empty structs/unions on non-Windows. |
| 1611 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1612 | return ABIArgInfo::getIgnore(); |
| 1613 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1614 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1615 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1616 | bool NeedsPadding = false; |
| 1617 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1618 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1619 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1620 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1621 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1622 | if (InReg) |
| 1623 | return ABIArgInfo::getDirectInReg(Result); |
| 1624 | else |
| 1625 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1626 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1627 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1628 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1629 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1630 | // of those arguments will match the struct. This is important because the |
| 1631 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1632 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1633 | // Don't do this for the MCU if there are still free integer registers |
| 1634 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1635 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1636 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1637 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1638 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1639 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1640 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1641 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1642 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1643 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1646 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1647 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1648 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1649 | if (IsDarwinVectorABI) { |
| 1650 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1651 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1652 | (Size == 64 && VT->getNumElements() == 1)) |
| 1653 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1654 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1655 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1656 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1657 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1658 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1659 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1660 | return ABIArgInfo::getDirect(); |
| 1661 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1662 | |
| 1663 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1664 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1665 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1666 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1667 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1668 | |
| 1669 | if (Ty->isPromotableIntegerType()) { |
| 1670 | if (InReg) |
| 1671 | return ABIArgInfo::getExtendInReg(); |
| 1672 | return ABIArgInfo::getExtend(); |
| 1673 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1674 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1675 | if (InReg) |
| 1676 | return ABIArgInfo::getDirectInReg(); |
| 1677 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1680 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1681 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1682 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1683 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1684 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1685 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1686 | // registers. |
| 1687 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1688 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1689 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1690 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1691 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1692 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1693 | // First pass do all the vector types. |
| 1694 | const Type *Base = nullptr; |
| 1695 | uint64_t NumElts = 0; |
| 1696 | const QualType& Ty = I.type; |
| 1697 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1698 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1699 | if (State.FreeSSERegs >= NumElts) { |
| 1700 | State.FreeSSERegs -= NumElts; |
| 1701 | I.info = ABIArgInfo::getDirect(); |
| 1702 | } else { |
| 1703 | I.info = classifyArgumentType(Ty, State); |
| 1704 | } |
| 1705 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1706 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1707 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1708 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1709 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1710 | // Second pass, do the rest! |
| 1711 | const Type *Base = nullptr; |
| 1712 | uint64_t NumElts = 0; |
| 1713 | const QualType& Ty = I.type; |
| 1714 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1715 | |
| 1716 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1717 | // Assign true HVAs (non vector/native FP types). |
| 1718 | if (State.FreeSSERegs >= NumElts) { |
| 1719 | State.FreeSSERegs -= NumElts; |
| 1720 | I.info = getDirectX86Hva(); |
| 1721 | } else { |
| 1722 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1723 | } |
| 1724 | } else if (!IsHva) { |
| 1725 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1726 | I.info = classifyArgumentType(Ty, State); |
| 1727 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1728 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1729 | } |
| 1730 | } |
| 1731 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1732 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1733 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1734 | if (IsMCUABI) |
| 1735 | State.FreeRegs = 3; |
| 1736 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1737 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1738 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1739 | State.FreeRegs = 2; |
| 1740 | State.FreeSSERegs = 6; |
| 1741 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1742 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1743 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1744 | State.FreeRegs = 5; |
| 1745 | State.FreeSSERegs = 8; |
| 1746 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1747 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1748 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1749 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1750 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1751 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1752 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1753 | // return value was sret and put it in a register ourselves if appropriate. |
| 1754 | if (State.FreeRegs) { |
| 1755 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1756 | if (!IsMCUABI) |
| 1757 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1758 | } |
| 1759 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1760 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1761 | // The chain argument effectively gives us another free register. |
| 1762 | if (FI.isChainCall()) |
| 1763 | ++State.FreeRegs; |
| 1764 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1765 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1766 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1767 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1768 | } else { |
| 1769 | // If not vectorcall, revert to normal behavior. |
| 1770 | for (auto &I : FI.arguments()) { |
| 1771 | I.info = classifyArgumentType(I.type, State); |
| 1772 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1773 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1777 | // all the memory arguments to use inalloca. |
| 1778 | if (UsedInAlloca) |
| 1779 | rewriteWithInAlloca(FI); |
| 1780 | } |
| 1781 | |
| 1782 | void |
| 1783 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1784 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1785 | QualType Type) const { |
| 1786 | // Arguments are always 4-byte-aligned. |
| 1787 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1788 | |
| 1789 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1790 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1791 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1792 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1793 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1794 | // Insert padding bytes to respect alignment. |
| 1795 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1796 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1797 | if (StackOffset != FieldEnd) { |
| 1798 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1799 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1800 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1801 | FrameFields.push_back(Ty); |
| 1802 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1805 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1806 | // Leave ignored and inreg arguments alone. |
| 1807 | switch (Info.getKind()) { |
| 1808 | case ABIArgInfo::InAlloca: |
| 1809 | return true; |
| 1810 | case ABIArgInfo::Indirect: |
| 1811 | assert(Info.getIndirectByVal()); |
| 1812 | return true; |
| 1813 | case ABIArgInfo::Ignore: |
| 1814 | return false; |
| 1815 | case ABIArgInfo::Direct: |
| 1816 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1817 | if (Info.getInReg()) |
| 1818 | return false; |
| 1819 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1820 | case ABIArgInfo::Expand: |
| 1821 | case ABIArgInfo::CoerceAndExpand: |
| 1822 | // These are aggregate types which are never passed in registers when |
| 1823 | // inalloca is involved. |
| 1824 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1825 | } |
| 1826 | llvm_unreachable("invalid enum"); |
| 1827 | } |
| 1828 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1829 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1830 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1831 | |
| 1832 | // Build a packed struct type for all of the arguments in memory. |
| 1833 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1834 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1835 | // The stack alignment is always 4. |
| 1836 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1837 | |
| 1838 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1839 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1840 | |
| 1841 | // Put 'this' into the struct before 'sret', if necessary. |
| 1842 | bool IsThisCall = |
| 1843 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1844 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1845 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1846 | isArgInAlloca(I->info)) { |
| 1847 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1848 | ++I; |
| 1849 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1850 | |
| 1851 | // 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] | 1852 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1853 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1854 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1855 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1856 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1860 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1861 | ++I; |
| 1862 | |
| 1863 | // Put arguments passed in memory into the struct. |
| 1864 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1865 | if (isArgInAlloca(I->info)) |
| 1866 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1870 | /*isPacked=*/true), |
| 1871 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1874 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1875 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1876 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1877 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1878 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1879 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1880 | // |
| 1881 | // Just messing with TypeInfo like this works because we never pass |
| 1882 | // anything indirectly. |
| 1883 | TypeInfo.second = CharUnits::fromQuantity( |
| 1884 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1885 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1886 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1887 | TypeInfo, CharUnits::fromQuantity(4), |
| 1888 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1891 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1892 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1893 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1894 | |
| 1895 | switch (Opts.getStructReturnConvention()) { |
| 1896 | case CodeGenOptions::SRCK_Default: |
| 1897 | break; |
| 1898 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1899 | return false; |
| 1900 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1901 | return true; |
| 1902 | } |
| 1903 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1904 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1905 | return true; |
| 1906 | |
| 1907 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1908 | case llvm::Triple::DragonFly: |
| 1909 | case llvm::Triple::FreeBSD: |
| 1910 | case llvm::Triple::OpenBSD: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1911 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1912 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1913 | default: |
| 1914 | return false; |
| 1915 | } |
| 1916 | } |
| 1917 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1918 | void X86_32TargetCodeGenInfo::setTargetAttributes( |
| 1919 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 1920 | ForDefinition_t IsForDefinition) const { |
| 1921 | if (!IsForDefinition) |
| 1922 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1923 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1924 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1925 | // Get the LLVM function. |
| 1926 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1927 | |
| 1928 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1929 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1930 | B.addStackAlignmentAttr(16); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1931 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1932 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1933 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1934 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1935 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1936 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1940 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1941 | CodeGen::CodeGenFunction &CGF, |
| 1942 | llvm::Value *Address) const { |
| 1943 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1944 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1945 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1946 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1947 | // 0-7 are the eight integer registers; the order is different |
| 1948 | // on Darwin (for EH), but the range is the same. |
| 1949 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1950 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1951 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1952 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1953 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1954 | // These have size 16, which is sizeof(long double) on |
| 1955 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1956 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1957 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1958 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1959 | } else { |
| 1960 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1961 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1962 | Builder.CreateAlignedStore( |
| 1963 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1964 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1965 | |
| 1966 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1967 | // These have size 12, which is sizeof(long double) on |
| 1968 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1969 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1970 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1971 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1972 | |
| 1973 | return false; |
| 1974 | } |
| 1975 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1976 | //===----------------------------------------------------------------------===// |
| 1977 | // X86-64 ABI Implementation |
| 1978 | //===----------------------------------------------------------------------===// |
| 1979 | |
| 1980 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1981 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1982 | /// The AVX ABI level for X86 targets. |
| 1983 | enum class X86AVXABILevel { |
| 1984 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1985 | AVX, |
| 1986 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1987 | }; |
| 1988 | |
| 1989 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1990 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1991 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1992 | case X86AVXABILevel::AVX512: |
| 1993 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1994 | case X86AVXABILevel::AVX: |
| 1995 | return 256; |
| 1996 | case X86AVXABILevel::None: |
| 1997 | return 128; |
| 1998 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1999 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2002 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2003 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2004 | enum Class { |
| 2005 | Integer = 0, |
| 2006 | SSE, |
| 2007 | SSEUp, |
| 2008 | X87, |
| 2009 | X87Up, |
| 2010 | ComplexX87, |
| 2011 | NoClass, |
| 2012 | Memory |
| 2013 | }; |
| 2014 | |
| 2015 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2016 | /// |
| 2017 | /// Merge an accumulating classification \arg Accum with a field |
| 2018 | /// classification \arg Field. |
| 2019 | /// |
| 2020 | /// \param Accum - The accumulating classification. This should |
| 2021 | /// always be either NoClass or the result of a previous merge |
| 2022 | /// call. In addition, this should never be Memory (the caller |
| 2023 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2024 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2025 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2026 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2027 | /// |
| 2028 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2029 | /// final MEMORY or SSE classes when necessary. |
| 2030 | /// |
| 2031 | /// \param AggregateSize - The size of the current aggregate in |
| 2032 | /// the classification process. |
| 2033 | /// |
| 2034 | /// \param Lo - The classification for the parts of the type |
| 2035 | /// residing in the low word of the containing object. |
| 2036 | /// |
| 2037 | /// \param Hi - The classification for the parts of the type |
| 2038 | /// residing in the higher words of the containing object. |
| 2039 | /// |
| 2040 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2041 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2042 | /// classify - Determine the x86_64 register classes in which the |
| 2043 | /// given type T should be passed. |
| 2044 | /// |
| 2045 | /// \param Lo - The classification for the parts of the type |
| 2046 | /// residing in the low word of the containing object. |
| 2047 | /// |
| 2048 | /// \param Hi - The classification for the parts of the type |
| 2049 | /// residing in the high word of the containing object. |
| 2050 | /// |
| 2051 | /// \param OffsetBase - The bit offset of this type in the |
| 2052 | /// containing object. Some parameters are classified different |
| 2053 | /// depending on whether they straddle an eightbyte boundary. |
| 2054 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2055 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2056 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2057 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2058 | /// If a word is unused its result will be NoClass; if a type should |
| 2059 | /// be passed in Memory then at least the classification of \arg Lo |
| 2060 | /// will be Memory. |
| 2061 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2062 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2063 | /// |
| 2064 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2065 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2066 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2067 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2068 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2069 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2070 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2071 | unsigned IROffset, QualType SourceTy, |
| 2072 | unsigned SourceOffset) const; |
| 2073 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2074 | unsigned IROffset, QualType SourceTy, |
| 2075 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2076 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2077 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2078 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2079 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2080 | |
| 2081 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2082 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2083 | /// |
| 2084 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2085 | /// available. |
| 2086 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2087 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2088 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2089 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2090 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2091 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2092 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2093 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2094 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2095 | unsigned &NeededSSE) const; |
| 2096 | |
| 2097 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2098 | unsigned &NeededSSE) const; |
| 2099 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2100 | bool IsIllegalVectorType(QualType Ty) const; |
| 2101 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2102 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2103 | /// unfortunately in ways that were not always consistent with |
| 2104 | /// certain previous compilers. In particular, platforms which |
| 2105 | /// required strict binary compatibility with older versions of GCC |
| 2106 | /// may need to exempt themselves. |
| 2107 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2108 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2111 | /// GCC classifies <1 x long long> as SSE but compatibility with older clang |
| 2112 | // compilers require us to classify it as INTEGER. |
| 2113 | bool classifyIntegerMMXAsSSE() const { |
| 2114 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2115 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2116 | return false; |
| 2117 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2118 | return false; |
| 2119 | return true; |
| 2120 | } |
| 2121 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2122 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2123 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2124 | // 64-bit hardware. |
| 2125 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2126 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2127 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2128 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2129 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2130 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2131 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2132 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2133 | bool isPassedUsingAVXType(QualType type) const { |
| 2134 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2135 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2136 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2137 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2138 | if (info.isDirect()) { |
| 2139 | llvm::Type *ty = info.getCoerceToType(); |
| 2140 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2141 | return (vectorTy->getBitWidth() > 128); |
| 2142 | } |
| 2143 | return false; |
| 2144 | } |
| 2145 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2146 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2147 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2148 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2149 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2150 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2151 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2152 | |
| 2153 | bool has64BitPointers() const { |
| 2154 | return Has64BitPointers; |
| 2155 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2156 | |
| 2157 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 2158 | ArrayRef<llvm::Type*> scalars, |
| 2159 | bool asReturnValue) const override { |
| 2160 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2161 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2162 | bool isSwiftErrorInRegister() const override { |
| 2163 | return true; |
| 2164 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2165 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2166 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2167 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2168 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2169 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2170 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2171 | : SwiftABIInfo(CGT), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2172 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2173 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2174 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2175 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2176 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2177 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2178 | |
| 2179 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2180 | // FIXME: Assumes vectorcall is in use. |
| 2181 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2182 | } |
| 2183 | |
| 2184 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2185 | uint64_t NumMembers) const override { |
| 2186 | // FIXME: Assumes vectorcall is in use. |
| 2187 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2188 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2189 | |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2190 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 2191 | ArrayRef<llvm::Type *> scalars, |
| 2192 | bool asReturnValue) const override { |
| 2193 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2194 | } |
| 2195 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2196 | bool isSwiftErrorInRegister() const override { |
| 2197 | return true; |
| 2198 | } |
| 2199 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2200 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2201 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2202 | bool IsVectorCall, bool IsRegCall) const; |
| 2203 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2204 | const ABIArgInfo ¤t) const; |
| 2205 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2206 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2207 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2208 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2209 | }; |
| 2210 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2211 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2212 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2213 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2214 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2215 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2216 | const X86_64ABIInfo &getABIInfo() const { |
| 2217 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2218 | } |
| 2219 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2220 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2221 | return 7; |
| 2222 | } |
| 2223 | |
| 2224 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2225 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2226 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2227 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2228 | // 0-15 are the 16 integer registers. |
| 2229 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2230 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2231 | return false; |
| 2232 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2233 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2234 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2235 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2236 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2237 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2238 | } |
| 2239 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2240 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2241 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2242 | // The default CC on x86-64 sets %al to the number of SSA |
| 2243 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2244 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2245 | // that when AVX types are involved: the ABI explicitly states it is |
| 2246 | // undefined, and it doesn't work in practice because of how the ABI |
| 2247 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2248 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2249 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2250 | for (CallArgList::const_iterator |
| 2251 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2252 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2253 | HasAVXType = true; |
| 2254 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2255 | } |
| 2256 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2257 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2258 | if (!HasAVXType) |
| 2259 | return true; |
| 2260 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2261 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2262 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2265 | llvm::Constant * |
| 2266 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2267 | unsigned Sig; |
| 2268 | if (getABIInfo().has64BitPointers()) |
| 2269 | Sig = (0xeb << 0) | // jmp rel8 |
| 2270 | (0x0a << 8) | // .+0x0c |
| 2271 | ('F' << 16) | |
| 2272 | ('T' << 24); |
| 2273 | else |
| 2274 | Sig = (0xeb << 0) | // jmp rel8 |
| 2275 | (0x06 << 8) | // .+0x08 |
| 2276 | ('F' << 16) | |
| 2277 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2278 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2279 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2280 | |
| 2281 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2282 | CodeGen::CodeGenModule &CGM, |
| 2283 | ForDefinition_t IsForDefinition) const override { |
| 2284 | if (!IsForDefinition) |
| 2285 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2286 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2287 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2288 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2289 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2290 | } |
| 2291 | } |
| 2292 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2293 | }; |
| 2294 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2295 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2296 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2297 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2298 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2299 | |
| 2300 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 2301 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2302 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 2303 | // If the argument contains a space, enclose it in quotes. |
| 2304 | if (Lib.find(" ") != StringRef::npos) |
| 2305 | Opt += "\"" + Lib.str() + "\""; |
| 2306 | else |
| 2307 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2308 | } |
| 2309 | }; |
| 2310 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2311 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2312 | // If the argument does not end in .lib, automatically add the suffix. |
| 2313 | // If the argument contains a space, enclose it in quotes. |
| 2314 | // This matches the behavior of MSVC. |
| 2315 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2316 | std::string ArgStr = Quote ? "\"" : ""; |
| 2317 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 2318 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2319 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2320 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2321 | return ArgStr; |
| 2322 | } |
| 2323 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2324 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2325 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2326 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2327 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2328 | unsigned NumRegisterParameters) |
| 2329 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2330 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2331 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2332 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2333 | CodeGen::CodeGenModule &CGM, |
| 2334 | ForDefinition_t IsForDefinition) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2335 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2336 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2337 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2338 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2339 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2340 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2341 | |
| 2342 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2343 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2344 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2345 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2346 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2347 | }; |
| 2348 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2349 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 2350 | llvm::GlobalValue *GV, |
| 2351 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 2352 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2353 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 2354 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2355 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2356 | Fn->addFnAttr("stack-probe-size", |
| 2357 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2358 | } |
| 2359 | } |
| 2360 | } |
| 2361 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2362 | void WinX86_32TargetCodeGenInfo::setTargetAttributes( |
| 2363 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 2364 | ForDefinition_t IsForDefinition) const { |
| 2365 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition); |
| 2366 | if (!IsForDefinition) |
| 2367 | return; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2368 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2369 | } |
| 2370 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2371 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2372 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2373 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2374 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2375 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2376 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2377 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2378 | CodeGen::CodeGenModule &CGM, |
| 2379 | ForDefinition_t IsForDefinition) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2380 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2381 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2382 | return 7; |
| 2383 | } |
| 2384 | |
| 2385 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2386 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2387 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2388 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2389 | // 0-15 are the 16 integer registers. |
| 2390 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2391 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2392 | return false; |
| 2393 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2394 | |
| 2395 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2396 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2397 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2398 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2399 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2400 | |
| 2401 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2402 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2403 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2404 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2405 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2406 | }; |
| 2407 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2408 | void WinX86_64TargetCodeGenInfo::setTargetAttributes( |
| 2409 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 2410 | ForDefinition_t IsForDefinition) const { |
| 2411 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition); |
| 2412 | if (!IsForDefinition) |
| 2413 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2414 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2415 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2416 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2417 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2418 | } |
| 2419 | } |
| 2420 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2421 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2422 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2423 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2424 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2425 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2426 | Class &Hi) const { |
| 2427 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2428 | // |
| 2429 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2430 | // memory. |
| 2431 | // |
| 2432 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2433 | // memory. |
| 2434 | // |
| 2435 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2436 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2437 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2438 | // ABI working for processors that don't support the __m256 type. |
| 2439 | // |
| 2440 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2441 | // |
| 2442 | // Some of these are enforced by the merging logic. Others can arise |
| 2443 | // only with unions; for example: |
| 2444 | // union { _Complex double; unsigned; } |
| 2445 | // |
| 2446 | // Note that clauses (b) and (c) were added in 0.98. |
| 2447 | // |
| 2448 | if (Hi == Memory) |
| 2449 | Lo = Memory; |
| 2450 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2451 | Lo = Memory; |
| 2452 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2453 | Lo = Memory; |
| 2454 | if (Hi == SSEUp && Lo != SSE) |
| 2455 | Hi = SSE; |
| 2456 | } |
| 2457 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2458 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2459 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2460 | // classified recursively so that always two fields are |
| 2461 | // considered. The resulting class is calculated according to |
| 2462 | // the classes of the fields in the eightbyte: |
| 2463 | // |
| 2464 | // (a) If both classes are equal, this is the resulting class. |
| 2465 | // |
| 2466 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2467 | // the other class. |
| 2468 | // |
| 2469 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2470 | // class. |
| 2471 | // |
| 2472 | // (d) If one of the classes is INTEGER, the result is the |
| 2473 | // INTEGER. |
| 2474 | // |
| 2475 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2476 | // MEMORY is used as class. |
| 2477 | // |
| 2478 | // (f) Otherwise class SSE is used. |
| 2479 | |
| 2480 | // Accum should never be memory (we should have returned) or |
| 2481 | // ComplexX87 (because this cannot be passed in a structure). |
| 2482 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2483 | "Invalid accumulated classification during merge."); |
| 2484 | if (Accum == Field || Field == NoClass) |
| 2485 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2486 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2487 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2488 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2489 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2490 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2491 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2492 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2493 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2494 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2495 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2498 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2499 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2500 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2501 | // Class pairs with appropriate constructor methods for the various |
| 2502 | // situations. |
| 2503 | |
| 2504 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2505 | // shouldn't be passed in registers for example, so there is no chance they |
| 2506 | // can straddle an eightbyte. Verify & simplify. |
| 2507 | |
| 2508 | Lo = Hi = NoClass; |
| 2509 | |
| 2510 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2511 | Current = Memory; |
| 2512 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2513 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2514 | BuiltinType::Kind k = BT->getKind(); |
| 2515 | |
| 2516 | if (k == BuiltinType::Void) { |
| 2517 | Current = NoClass; |
| 2518 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2519 | Lo = Integer; |
| 2520 | Hi = Integer; |
| 2521 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2522 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2523 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2524 | Current = SSE; |
| 2525 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2526 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2527 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2528 | Lo = SSE; |
| 2529 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2530 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2531 | Lo = X87; |
| 2532 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2533 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2534 | Current = SSE; |
| 2535 | } else |
| 2536 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2537 | } |
| 2538 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2539 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
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 EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2544 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2545 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2546 | return; |
| 2547 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2548 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2549 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2550 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2551 | return; |
| 2552 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2553 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2554 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2555 | if (Ty->isMemberFunctionPointerType()) { |
| 2556 | if (Has64BitPointers) { |
| 2557 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2558 | // Lo and Hi now. |
| 2559 | Lo = Hi = Integer; |
| 2560 | } else { |
| 2561 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2562 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2563 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2564 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2565 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2566 | Lo = Hi = Integer; |
| 2567 | } else { |
| 2568 | Current = Integer; |
| 2569 | } |
| 2570 | } |
| 2571 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2572 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2573 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2574 | return; |
| 2575 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2576 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2577 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2578 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2579 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2580 | // gcc passes the following as integer: |
| 2581 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2582 | // 2 bytes - <2 x char>, <1 x short> |
| 2583 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2584 | Current = Integer; |
| 2585 | |
| 2586 | // If this type crosses an eightbyte boundary, it should be |
| 2587 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2588 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2589 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2590 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2591 | Hi = Lo; |
| 2592 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2593 | QualType ElementType = VT->getElementType(); |
| 2594 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2595 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2596 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2597 | return; |
| 2598 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2599 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2600 | // pass them as integer. For platforms where clang is the de facto |
| 2601 | // platform compiler, we must continue to use integer. |
| 2602 | if (!classifyIntegerMMXAsSSE() && |
| 2603 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2604 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2605 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2606 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2607 | Current = Integer; |
| 2608 | else |
| 2609 | Current = SSE; |
| 2610 | |
| 2611 | // If this type crosses an eightbyte boundary, it should be |
| 2612 | // split. |
| 2613 | if (OffsetBase && OffsetBase != 64) |
| 2614 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2615 | } else if (Size == 128 || |
| 2616 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2617 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2618 | // least significant one belongs to class SSE and all the others to class |
| 2619 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2620 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2621 | // This design isn't correct for 256-bits, but since there're no cases |
| 2622 | // where the upper parts would need to be inspected, avoid adding |
| 2623 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2624 | // |
| 2625 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2626 | // registers if they are "named", i.e. not part of the "..." of a |
| 2627 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2628 | // |
| 2629 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2630 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2631 | Lo = SSE; |
| 2632 | Hi = SSEUp; |
| 2633 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2634 | return; |
| 2635 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2636 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2637 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2638 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2639 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2640 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2641 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2642 | if (Size <= 64) |
| 2643 | Current = Integer; |
| 2644 | else if (Size <= 128) |
| 2645 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2646 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2647 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2648 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2649 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2650 | } else if (ET == getContext().LongDoubleTy) { |
| 2651 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2652 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2653 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2654 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2655 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2656 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2657 | Lo = Hi = SSE; |
| 2658 | else |
| 2659 | llvm_unreachable("unexpected long double representation!"); |
| 2660 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2661 | |
| 2662 | // If this complex type crosses an eightbyte boundary then it |
| 2663 | // should be split. |
| 2664 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2665 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2666 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2667 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2668 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2669 | return; |
| 2670 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2671 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2672 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2673 | // Arrays are treated like structures. |
| 2674 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2675 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2676 | |
| 2677 | // 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] | 2678 | // than eight eightbytes, ..., it has class MEMORY. |
| 2679 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2680 | return; |
| 2681 | |
| 2682 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2683 | // fields, it has class MEMORY. |
| 2684 | // |
| 2685 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2686 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2687 | return; |
| 2688 | |
| 2689 | // Otherwise implement simplified merge. We could be smarter about |
| 2690 | // this, but it isn't worth it and would be harder to verify. |
| 2691 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2692 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2693 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2694 | |
| 2695 | // The only case a 256-bit wide vector could be used is when the array |
| 2696 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2697 | // 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] | 2698 | // |
| 2699 | if (Size > 128 && |
| 2700 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2701 | return; |
| 2702 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2703 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2704 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2705 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2706 | Lo = merge(Lo, FieldLo); |
| 2707 | Hi = merge(Hi, FieldHi); |
| 2708 | if (Lo == Memory || Hi == Memory) |
| 2709 | break; |
| 2710 | } |
| 2711 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2712 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2713 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2714 | return; |
| 2715 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2716 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2717 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2718 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2719 | |
| 2720 | // 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] | 2721 | // than eight eightbytes, ..., it has class MEMORY. |
| 2722 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2723 | return; |
| 2724 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2725 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2726 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2727 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2728 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2729 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2730 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2731 | const RecordDecl *RD = RT->getDecl(); |
| 2732 | |
| 2733 | // Assume variable sized types are passed in memory. |
| 2734 | if (RD->hasFlexibleArrayMember()) |
| 2735 | return; |
| 2736 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2737 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2738 | |
| 2739 | // Reset Lo class, this will be recomputed. |
| 2740 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2741 | |
| 2742 | // If this is a C++ record, classify the bases first. |
| 2743 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2744 | for (const auto &I : CXXRD->bases()) { |
| 2745 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2746 | "Unexpected base class!"); |
| 2747 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2748 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2749 | |
| 2750 | // Classify this field. |
| 2751 | // |
| 2752 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2753 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2754 | // initialized to class NO_CLASS. |
| 2755 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2756 | uint64_t Offset = |
| 2757 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2758 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2759 | Lo = merge(Lo, FieldLo); |
| 2760 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2761 | if (Lo == Memory || Hi == Memory) { |
| 2762 | postMerge(Size, Lo, Hi); |
| 2763 | return; |
| 2764 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2769 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2770 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2771 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2772 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2773 | bool BitField = i->isBitField(); |
| 2774 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2775 | // Ignore padding bit-fields. |
| 2776 | if (BitField && i->isUnnamedBitfield()) |
| 2777 | continue; |
| 2778 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2779 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2780 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2781 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2782 | // The only case a 256-bit wide vector could be used is when the struct |
| 2783 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2784 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2785 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2786 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2787 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2788 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2789 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2790 | return; |
| 2791 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2792 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2793 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2794 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2795 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2796 | return; |
| 2797 | } |
| 2798 | |
| 2799 | // Classify this field. |
| 2800 | // |
| 2801 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2802 | // exceeds a single eightbyte, each is classified |
| 2803 | // separately. Each eightbyte gets initialized to class |
| 2804 | // NO_CLASS. |
| 2805 | Class FieldLo, FieldHi; |
| 2806 | |
| 2807 | // Bit-fields require special handling, they do not force the |
| 2808 | // structure to be passed in memory even if unaligned, and |
| 2809 | // therefore they can straddle an eightbyte. |
| 2810 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2811 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2812 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2813 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2814 | |
| 2815 | uint64_t EB_Lo = Offset / 64; |
| 2816 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2817 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2818 | if (EB_Lo) { |
| 2819 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2820 | FieldLo = NoClass; |
| 2821 | FieldHi = Integer; |
| 2822 | } else { |
| 2823 | FieldLo = Integer; |
| 2824 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2825 | } |
| 2826 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2827 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2828 | Lo = merge(Lo, FieldLo); |
| 2829 | Hi = merge(Hi, FieldHi); |
| 2830 | if (Lo == Memory || Hi == Memory) |
| 2831 | break; |
| 2832 | } |
| 2833 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2834 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2835 | } |
| 2836 | } |
| 2837 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2838 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2839 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2840 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2841 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2842 | // Treat an enum type as its underlying type. |
| 2843 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2844 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2845 | |
| 2846 | return (Ty->isPromotableIntegerType() ? |
| 2847 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2848 | } |
| 2849 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2850 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2853 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2854 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2855 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2856 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2857 | if (Size <= 64 || Size > LargestVector) |
| 2858 | return true; |
| 2859 | } |
| 2860 | |
| 2861 | return false; |
| 2862 | } |
| 2863 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2864 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2865 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2866 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2867 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2868 | // |
| 2869 | // This assumption is optimistic, as there could be free registers available |
| 2870 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2871 | // the argument in the free register. This does not seem to happen currently, |
| 2872 | // but this code would be much safer if we could mark the argument with |
| 2873 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2874 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2875 | // Treat an enum type as its underlying type. |
| 2876 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2877 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2878 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2879 | return (Ty->isPromotableIntegerType() ? |
| 2880 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2881 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2882 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2883 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2884 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2885 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2886 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2887 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2888 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2889 | |
| 2890 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2891 | // is important for good codegen. |
| 2892 | // |
| 2893 | // We do this by coercing the value into a scalar type which the backend can |
| 2894 | // handle naturally (i.e., without using byval). |
| 2895 | // |
| 2896 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2897 | // free integer registers. Doing this when there are free integer registers |
| 2898 | // would require more care, as we would have to ensure that the coerced value |
| 2899 | // did not claim the unused register. That would require either reording the |
| 2900 | // arguments to the function (so that any subsequent inreg values came first), |
| 2901 | // or only doing this optimization when there were no following arguments that |
| 2902 | // might be inreg. |
| 2903 | // |
| 2904 | // We currently expect it to be rare (particularly in well written code) for |
| 2905 | // arguments to be passed on the stack when there are still free integer |
| 2906 | // registers available (this would typically imply large structs being passed |
| 2907 | // by value), so this seems like a fair tradeoff for now. |
| 2908 | // |
| 2909 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2910 | // attributes. See PR12193. |
| 2911 | if (freeIntRegs == 0) { |
| 2912 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2913 | |
| 2914 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2915 | // type, which will end up on the stack (with alignment 8). |
| 2916 | if (Align == 8 && Size <= 64) |
| 2917 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2918 | Size)); |
| 2919 | } |
| 2920 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2921 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2924 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2925 | /// 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] | 2926 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2927 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2928 | // vectors; strip them off if present. |
| 2929 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2930 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2931 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2932 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2933 | if (isa<llvm::VectorType>(IRType) || |
| 2934 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2935 | return IRType; |
| 2936 | |
| 2937 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2938 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2939 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2940 | |
| 2941 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2942 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2943 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2946 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2947 | /// is known to either be off the end of the specified type or being in |
| 2948 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2949 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2950 | /// classification that put one of the two halves in the INTEGER class. |
| 2951 | /// |
| 2952 | /// It is conservatively correct to return false. |
| 2953 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2954 | unsigned EndBit, ASTContext &Context) { |
| 2955 | // If the bytes being queried are off the end of the type, there is no user |
| 2956 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2957 | // types that don't contain interesting padding. |
| 2958 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2959 | if (TySize <= StartBit) |
| 2960 | return true; |
| 2961 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2962 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2963 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2964 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2965 | |
| 2966 | // Check each element to see if the element overlaps with the queried range. |
| 2967 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2968 | // If the element is after the span we care about, then we're done.. |
| 2969 | unsigned EltOffset = i*EltSize; |
| 2970 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2971 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2972 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2973 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2974 | EndBit-EltOffset, Context)) |
| 2975 | return false; |
| 2976 | } |
| 2977 | // If it overlaps no elements, then it is safe to process as padding. |
| 2978 | return true; |
| 2979 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2980 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2981 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2982 | const RecordDecl *RD = RT->getDecl(); |
| 2983 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2984 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2985 | // If this is a C++ record, check the bases first. |
| 2986 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2987 | for (const auto &I : CXXRD->bases()) { |
| 2988 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2989 | "Unexpected base class!"); |
| 2990 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2991 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
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 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2994 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2995 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2996 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2997 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2998 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2999 | EndBit-BaseOffset, Context)) |
| 3000 | return false; |
| 3001 | } |
| 3002 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3003 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3004 | // Verify that no field has data that overlaps the region of interest. Yes |
| 3005 | // this could be sped up a lot by being smarter about queried fields, |
| 3006 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 3007 | // much. |
| 3008 | unsigned idx = 0; |
| 3009 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3010 | i != e; ++i, ++idx) { |
| 3011 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3012 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3013 | // If we found a field after the region we care about, then we're done. |
| 3014 | if (FieldOffset >= EndBit) break; |
| 3015 | |
| 3016 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 3017 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 3018 | Context)) |
| 3019 | return false; |
| 3020 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3021 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3022 | // If nothing in this record overlapped the area of interest, then we're |
| 3023 | // clean. |
| 3024 | return true; |
| 3025 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3026 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3027 | return false; |
| 3028 | } |
| 3029 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3030 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3031 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3032 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3033 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3034 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3035 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3036 | // Base case if we find a float. |
| 3037 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3038 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3039 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3040 | // 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] | 3041 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3042 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3043 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3044 | IROffset -= SL->getElementOffset(Elt); |
| 3045 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3046 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3047 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3048 | // 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] | 3049 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3050 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3051 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3052 | IROffset -= IROffset/EltSize*EltSize; |
| 3053 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3054 | } |
| 3055 | |
| 3056 | return false; |
| 3057 | } |
| 3058 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3059 | |
| 3060 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3061 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3062 | llvm::Type *X86_64ABIInfo:: |
| 3063 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3064 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3065 | // 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] | 3066 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3067 | // structs that contain 3 floats. |
| 3068 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3069 | SourceOffset*8+64, getContext())) |
| 3070 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3071 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3072 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3073 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3074 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3075 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3076 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3077 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3078 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3079 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3080 | } |
| 3081 | |
| 3082 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3083 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3084 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3085 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3086 | /// 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] | 3087 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3088 | /// etc). |
| 3089 | /// |
| 3090 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3091 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3092 | /// the 8-byte value references. PrefType may be null. |
| 3093 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3094 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3095 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3096 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3097 | llvm::Type *X86_64ABIInfo:: |
| 3098 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3099 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3100 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3101 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3102 | if (IROffset == 0) { |
| 3103 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3104 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3105 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3106 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3107 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3108 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3109 | // goodness in the source type is just tail padding. This is allowed to |
| 3110 | // kick in for struct {double,int} on the int, but not on |
| 3111 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3112 | // have to do this analysis on the source type because we can't depend on |
| 3113 | // unions being lowered a specific way etc. |
| 3114 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3115 | IRType->isIntegerTy(32) || |
| 3116 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3117 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3118 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3119 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3120 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3121 | SourceOffset*8+64, getContext())) |
| 3122 | return IRType; |
| 3123 | } |
| 3124 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3125 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3126 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3127 | // 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] | 3128 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3129 | if (IROffset < SL->getSizeInBytes()) { |
| 3130 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3131 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3132 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3133 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3134 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3135 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3136 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3137 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3138 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3139 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3140 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3141 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3142 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3143 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3144 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3145 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3146 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3147 | // 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] | 3148 | unsigned TySizeInBytes = |
| 3149 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3150 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3151 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3152 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3153 | // It is always safe to classify this as an integer type up to i64 that |
| 3154 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3155 | return llvm::IntegerType::get(getVMContext(), |
| 3156 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3159 | |
| 3160 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3161 | /// be used as elements of a two register pair to pass or return, return a |
| 3162 | /// first class aggregate to represent them. For example, if the low part of |
| 3163 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3164 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3165 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3166 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3167 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3168 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3169 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3170 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3171 | // the second element at offset 8. Check for this: |
| 3172 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3173 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3174 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3175 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3176 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3177 | // To handle this, we have to increase the size of the low part so that the |
| 3178 | // second element will start at an 8 byte offset. We can't increase the size |
| 3179 | // of the second element because it might make us access off the end of the |
| 3180 | // struct. |
| 3181 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3182 | // There are usually two sorts of types the ABI generation code can produce |
| 3183 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3184 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3185 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3186 | // Promote these to a larger type. |
| 3187 | if (Lo->isFloatTy()) |
| 3188 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3189 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3190 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3191 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3192 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3193 | } |
| 3194 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3195 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3196 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3197 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3198 | // Verify that the second element is at an 8-byte offset. |
| 3199 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3200 | "Invalid x86-64 argument pair!"); |
| 3201 | return Result; |
| 3202 | } |
| 3203 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3204 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3205 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3206 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3207 | // classification algorithm. |
| 3208 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3209 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3210 | |
| 3211 | // Check some invariants. |
| 3212 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3213 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3214 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3215 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3216 | switch (Lo) { |
| 3217 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3218 | if (Hi == NoClass) |
| 3219 | return ABIArgInfo::getIgnore(); |
| 3220 | // If the low part is just padding, it takes no register, leave ResType |
| 3221 | // null. |
| 3222 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3223 | "Unknown missing lo part"); |
| 3224 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3225 | |
| 3226 | case SSEUp: |
| 3227 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3228 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3229 | |
| 3230 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3231 | // hidden argument. |
| 3232 | case Memory: |
| 3233 | return getIndirectReturnResult(RetTy); |
| 3234 | |
| 3235 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3236 | // available register of the sequence %rax, %rdx is used. |
| 3237 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3238 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3239 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3240 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3241 | // so that the parameter gets the right LLVM IR attributes. |
| 3242 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3243 | // Treat an enum type as its underlying type. |
| 3244 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3245 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3246 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3247 | if (RetTy->isIntegralOrEnumerationType() && |
| 3248 | RetTy->isPromotableIntegerType()) |
| 3249 | return ABIArgInfo::getExtend(); |
| 3250 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3251 | break; |
| 3252 | |
| 3253 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3254 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3255 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3256 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3257 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3258 | |
| 3259 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3260 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3261 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3262 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3263 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3264 | |
| 3265 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3266 | // part of the value is returned in %st0 and the imaginary part in |
| 3267 | // %st1. |
| 3268 | case ComplexX87: |
| 3269 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3270 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3271 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3272 | break; |
| 3273 | } |
| 3274 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3275 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3276 | switch (Hi) { |
| 3277 | // Memory was handled previously and X87 should |
| 3278 | // never occur as a hi class. |
| 3279 | case Memory: |
| 3280 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3281 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3282 | |
| 3283 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3284 | case NoClass: |
| 3285 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3286 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3287 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3288 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3289 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3290 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3291 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3292 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3293 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3294 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3295 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3296 | break; |
| 3297 | |
| 3298 | // 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] | 3299 | // is passed in the next available eightbyte chunk if the last used |
| 3300 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3301 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3302 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3303 | case SSEUp: |
| 3304 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3305 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3306 | break; |
| 3307 | |
| 3308 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3309 | // returned together with the previous X87 value in %st0. |
| 3310 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3311 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3312 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3313 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3314 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3315 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3316 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3317 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3318 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3319 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3320 | break; |
| 3321 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3322 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3323 | // 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] | 3324 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3325 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3326 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3327 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3328 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3329 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3332 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3333 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3334 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3335 | const |
| 3336 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3337 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3338 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3339 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3340 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3341 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3342 | // Check some invariants. |
| 3343 | // FIXME: Enforce these by construction. |
| 3344 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3345 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3346 | |
| 3347 | neededInt = 0; |
| 3348 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3349 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3350 | switch (Lo) { |
| 3351 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3352 | if (Hi == NoClass) |
| 3353 | return ABIArgInfo::getIgnore(); |
| 3354 | // If the low part is just padding, it takes no register, leave ResType |
| 3355 | // null. |
| 3356 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3357 | "Unknown missing lo part"); |
| 3358 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3359 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3360 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3361 | // on the stack. |
| 3362 | case Memory: |
| 3363 | |
| 3364 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3365 | // COMPLEX_X87, it is passed in memory. |
| 3366 | case X87: |
| 3367 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3368 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3369 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3370 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3371 | |
| 3372 | case SSEUp: |
| 3373 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3374 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3375 | |
| 3376 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3377 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3378 | // and %r9 is used. |
| 3379 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3380 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3381 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3382 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3383 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3384 | |
| 3385 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3386 | // so that the parameter gets the right LLVM IR attributes. |
| 3387 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3388 | // Treat an enum type as its underlying type. |
| 3389 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3390 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3391 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3392 | if (Ty->isIntegralOrEnumerationType() && |
| 3393 | Ty->isPromotableIntegerType()) |
| 3394 | return ABIArgInfo::getExtend(); |
| 3395 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3396 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3397 | break; |
| 3398 | |
| 3399 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3400 | // available SSE register is used, the registers are taken in the |
| 3401 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3402 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3403 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3404 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3405 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3406 | break; |
| 3407 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3408 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3409 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3410 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3411 | switch (Hi) { |
| 3412 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3413 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3414 | // which is passed in memory. |
| 3415 | case Memory: |
| 3416 | case X87: |
| 3417 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3418 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3419 | |
| 3420 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3421 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3422 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3423 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3424 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3425 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3426 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3427 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3428 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3429 | break; |
| 3430 | |
| 3431 | // X87Up generally doesn't occur here (long double is passed in |
| 3432 | // memory), except in situations involving unions. |
| 3433 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3434 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3435 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3436 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3437 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3438 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3439 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3440 | ++neededSSE; |
| 3441 | break; |
| 3442 | |
| 3443 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3444 | // 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] | 3445 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3446 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3447 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3448 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3449 | break; |
| 3450 | } |
| 3451 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3452 | // If a high part was specified, merge it together with the low part. It is |
| 3453 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3454 | // first class struct aggregate with the high and low part: {low, high} |
| 3455 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3456 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3457 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3458 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3461 | ABIArgInfo |
| 3462 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3463 | unsigned &NeededSSE) const { |
| 3464 | auto RT = Ty->getAs<RecordType>(); |
| 3465 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3466 | |
| 3467 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3468 | return getIndirectReturnResult(Ty); |
| 3469 | |
| 3470 | // Sum up bases |
| 3471 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3472 | if (CXXRD->isDynamicClass()) { |
| 3473 | NeededInt = NeededSSE = 0; |
| 3474 | return getIndirectReturnResult(Ty); |
| 3475 | } |
| 3476 | |
| 3477 | for (const auto &I : CXXRD->bases()) |
| 3478 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3479 | .isIndirect()) { |
| 3480 | NeededInt = NeededSSE = 0; |
| 3481 | return getIndirectReturnResult(Ty); |
| 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | // Sum up members |
| 3486 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3487 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3488 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3489 | .isIndirect()) { |
| 3490 | NeededInt = NeededSSE = 0; |
| 3491 | return getIndirectReturnResult(Ty); |
| 3492 | } |
| 3493 | } else { |
| 3494 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3495 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3496 | LocalNeededSSE, true) |
| 3497 | .isIndirect()) { |
| 3498 | NeededInt = NeededSSE = 0; |
| 3499 | return getIndirectReturnResult(Ty); |
| 3500 | } |
| 3501 | NeededInt += LocalNeededInt; |
| 3502 | NeededSSE += LocalNeededSSE; |
| 3503 | } |
| 3504 | } |
| 3505 | |
| 3506 | return ABIArgInfo::getDirect(); |
| 3507 | } |
| 3508 | |
| 3509 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3510 | unsigned &NeededInt, |
| 3511 | unsigned &NeededSSE) const { |
| 3512 | |
| 3513 | NeededInt = 0; |
| 3514 | NeededSSE = 0; |
| 3515 | |
| 3516 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3517 | } |
| 3518 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3519 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3520 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3521 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3522 | |
| 3523 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3524 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3525 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3526 | unsigned NeededInt, NeededSSE; |
| 3527 | |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 3528 | if (!getCXXABI().classifyReturnType(FI)) { |
| 3529 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3530 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3531 | FI.getReturnInfo() = |
| 3532 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3533 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3534 | FreeIntRegs -= NeededInt; |
| 3535 | FreeSSERegs -= NeededSSE; |
| 3536 | } else { |
| 3537 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3538 | } |
| 3539 | } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) { |
| 3540 | // Complex Long Double Type is passed in Memory when Regcall |
| 3541 | // calling convention is used. |
| 3542 | const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>(); |
| 3543 | if (getContext().getCanonicalType(CT->getElementType()) == |
| 3544 | getContext().LongDoubleTy) |
| 3545 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3546 | } else |
| 3547 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3548 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3549 | |
| 3550 | // If the return value is indirect, then the hidden argument is consuming one |
| 3551 | // integer register. |
| 3552 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3553 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3554 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3555 | // The chain argument effectively gives us another free register. |
| 3556 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3557 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3558 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3559 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3560 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3561 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3562 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3563 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3564 | it != ie; ++it, ++ArgNo) { |
| 3565 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3566 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3567 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3568 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3569 | else |
| 3570 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3571 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3572 | |
| 3573 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3574 | // eightbyte of an argument, the whole argument is passed on the |
| 3575 | // stack. If registers have already been assigned for some |
| 3576 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3577 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3578 | FreeIntRegs -= NeededInt; |
| 3579 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3580 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3581 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3582 | } |
| 3583 | } |
| 3584 | } |
| 3585 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3586 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3587 | Address VAListAddr, QualType Ty) { |
| 3588 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3589 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3590 | llvm::Value *overflow_arg_area = |
| 3591 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3592 | |
| 3593 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3594 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3595 | // It isn't stated explicitly in the standard, but in practice we use |
| 3596 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3597 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3598 | if (Align > CharUnits::fromQuantity(8)) { |
| 3599 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3600 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | // 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] | 3604 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3605 | llvm::Value *Res = |
| 3606 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3607 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3608 | |
| 3609 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3610 | // l->overflow_arg_area + sizeof(type). |
| 3611 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3612 | // an 8 byte boundary. |
| 3613 | |
| 3614 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3615 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3616 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3617 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3618 | "overflow_arg_area.next"); |
| 3619 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3620 | |
| 3621 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3622 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3625 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3626 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3627 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3628 | // struct { |
| 3629 | // i32 gp_offset; |
| 3630 | // i32 fp_offset; |
| 3631 | // i8* overflow_arg_area; |
| 3632 | // i8* reg_save_area; |
| 3633 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3634 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3635 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3636 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3637 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3638 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3639 | |
| 3640 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3641 | // in the registers. If not go to step 7. |
| 3642 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3643 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3644 | |
| 3645 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3646 | // general purpose registers needed to pass type and num_fp to hold |
| 3647 | // the number of floating point registers needed. |
| 3648 | |
| 3649 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3650 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3651 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3652 | // |
| 3653 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3654 | // register save space). |
| 3655 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3656 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3657 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3658 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3659 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3660 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3661 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3662 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3663 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3664 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3665 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3669 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3670 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3671 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3672 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3673 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3674 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3675 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3676 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3677 | } |
| 3678 | |
| 3679 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3680 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3681 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3682 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3683 | |
| 3684 | // Emit code to load the value if it was passed in registers. |
| 3685 | |
| 3686 | CGF.EmitBlock(InRegBlock); |
| 3687 | |
| 3688 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3689 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3690 | // copying to a temporary location in case the parameter is passed |
| 3691 | // in different register classes or requires an alignment greater |
| 3692 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3693 | // |
| 3694 | // FIXME: This really results in shameful code when we end up needing to |
| 3695 | // collect arguments from different places; often what should result in a |
| 3696 | // simple assembling of a structure from scattered addresses has many more |
| 3697 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3698 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3699 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3700 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3701 | "reg_save_area"); |
| 3702 | |
| 3703 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3704 | if (neededInt && neededSSE) { |
| 3705 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3706 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3707 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3708 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3709 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3710 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3711 | llvm::Type *TyLo = ST->getElementType(0); |
| 3712 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3713 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3714 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3715 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3716 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3717 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3718 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3719 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3720 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3721 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3722 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3723 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3724 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3725 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3726 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3727 | CGF.Builder.CreateStore(V, |
| 3728 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3729 | |
| 3730 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3731 | V = CGF.Builder.CreateAlignedLoad( |
| 3732 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3733 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3734 | CharUnits Offset = CharUnits::fromQuantity( |
| 3735 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3736 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3737 | |
| 3738 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3739 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3740 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3741 | CharUnits::fromQuantity(8)); |
| 3742 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3743 | |
| 3744 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3745 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3746 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3747 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3748 | CharUnits TyAlign = SizeAlign.second; |
| 3749 | |
| 3750 | // Copy into a temporary if the type is more aligned than the |
| 3751 | // register save area. |
| 3752 | if (TyAlign.getQuantity() > 8) { |
| 3753 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3754 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3755 | RegAddr = Tmp; |
| 3756 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3757 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3758 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3759 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3760 | CharUnits::fromQuantity(16)); |
| 3761 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3762 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3763 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3764 | // SSE registers are spaced 16 bytes apart in the register save |
| 3765 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3766 | // The ABI isn't explicit about this, but it seems reasonable |
| 3767 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3768 | // naturally 16-byte aligned and the prologue is expected to store |
| 3769 | // all the SSE registers to the RSA. |
| 3770 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3771 | CharUnits::fromQuantity(16)); |
| 3772 | Address RegAddrHi = |
| 3773 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3774 | CharUnits::fromQuantity(16)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3775 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3776 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3777 | llvm::Value *V; |
| 3778 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3779 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3780 | V = CGF.Builder.CreateLoad( |
| 3781 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3782 | CGF.Builder.CreateStore(V, |
| 3783 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3784 | V = CGF.Builder.CreateLoad( |
| 3785 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3786 | CGF.Builder.CreateStore(V, |
| 3787 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3788 | |
| 3789 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3790 | } |
| 3791 | |
| 3792 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3793 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3794 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3795 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3796 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3797 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3798 | gp_offset_p); |
| 3799 | } |
| 3800 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3801 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3802 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3803 | fp_offset_p); |
| 3804 | } |
| 3805 | CGF.EmitBranch(ContBlock); |
| 3806 | |
| 3807 | // Emit code to load the value if it was passed in memory. |
| 3808 | |
| 3809 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3810 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3811 | |
| 3812 | // Return the appropriate result. |
| 3813 | |
| 3814 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3815 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3816 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3817 | return ResAddr; |
| 3818 | } |
| 3819 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3820 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3821 | QualType Ty) const { |
| 3822 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3823 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3824 | CharUnits::fromQuantity(8), |
| 3825 | /*allowHigherAlign*/ false); |
| 3826 | } |
| 3827 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3828 | ABIArgInfo |
| 3829 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3830 | const ABIArgInfo ¤t) const { |
| 3831 | // Assumes vectorCall calling convention. |
| 3832 | const Type *Base = nullptr; |
| 3833 | uint64_t NumElts = 0; |
| 3834 | |
| 3835 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3836 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3837 | FreeSSERegs -= NumElts; |
| 3838 | return getDirectX86Hva(); |
| 3839 | } |
| 3840 | return current; |
| 3841 | } |
| 3842 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3843 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3844 | bool IsReturnType, bool IsVectorCall, |
| 3845 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3846 | |
| 3847 | if (Ty->isVoidType()) |
| 3848 | return ABIArgInfo::getIgnore(); |
| 3849 | |
| 3850 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3851 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3852 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3853 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3854 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3855 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3856 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3857 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3858 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3859 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3860 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3861 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3865 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3866 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3867 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3868 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3869 | const Type *Base = nullptr; |
| 3870 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3871 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3872 | // other targets. |
| 3873 | if ((IsVectorCall || IsRegCall) && |
| 3874 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3875 | if (IsRegCall) { |
| 3876 | if (FreeSSERegs >= NumElts) { |
| 3877 | FreeSSERegs -= NumElts; |
| 3878 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3879 | return ABIArgInfo::getDirect(); |
| 3880 | return ABIArgInfo::getExpand(); |
| 3881 | } |
| 3882 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3883 | } else if (IsVectorCall) { |
| 3884 | if (FreeSSERegs >= NumElts && |
| 3885 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3886 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3887 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3888 | } else if (IsReturnType) { |
| 3889 | return ABIArgInfo::getExpand(); |
| 3890 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3891 | // HVAs are delayed and reclassified in the 2nd step. |
| 3892 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3893 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3894 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3897 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3898 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3899 | // directly. |
| 3900 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3901 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3902 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3905 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3906 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3907 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3908 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3909 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3910 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3911 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3912 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3915 | // Bool type is always extended to the ABI, other builtin types are not |
| 3916 | // extended. |
| 3917 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3918 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3919 | return ABIArgInfo::getExtend(); |
| 3920 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3921 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3922 | // passes them indirectly through memory. |
| 3923 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3924 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3925 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3926 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3927 | } |
| 3928 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3929 | return ABIArgInfo::getDirect(); |
| 3930 | } |
| 3931 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3932 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 3933 | unsigned FreeSSERegs, |
| 3934 | bool IsVectorCall, |
| 3935 | bool IsRegCall) const { |
| 3936 | unsigned Count = 0; |
| 3937 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 3938 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 3939 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3940 | if (Count < VectorcallMaxParamNumAsReg) |
| 3941 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 3942 | else { |
| 3943 | // Since these cannot be passed in registers, pretend no registers |
| 3944 | // are left. |
| 3945 | unsigned ZeroSSERegsAvail = 0; |
| 3946 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 3947 | IsVectorCall, IsRegCall); |
| 3948 | } |
| 3949 | ++Count; |
| 3950 | } |
| 3951 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3952 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 3953 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3954 | } |
| 3955 | } |
| 3956 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3957 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3958 | bool IsVectorCall = |
| 3959 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3960 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3961 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3962 | unsigned FreeSSERegs = 0; |
| 3963 | if (IsVectorCall) { |
| 3964 | // We can use up to 4 SSE return registers with vectorcall. |
| 3965 | FreeSSERegs = 4; |
| 3966 | } else if (IsRegCall) { |
| 3967 | // RegCall gives us 16 SSE registers. |
| 3968 | FreeSSERegs = 16; |
| 3969 | } |
| 3970 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3971 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3972 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 3973 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3974 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3975 | if (IsVectorCall) { |
| 3976 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3977 | FreeSSERegs = 6; |
| 3978 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3979 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3980 | FreeSSERegs = 16; |
| 3981 | } |
| 3982 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3983 | if (IsVectorCall) { |
| 3984 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 3985 | } else { |
| 3986 | for (auto &I : FI.arguments()) |
| 3987 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 3988 | } |
| 3989 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3990 | } |
| 3991 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3992 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3993 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 3994 | |
| 3995 | bool IsIndirect = false; |
| 3996 | |
| 3997 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3998 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 3999 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 4000 | uint64_t Width = getContext().getTypeSize(Ty); |
| 4001 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 4002 | } |
| 4003 | |
| 4004 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4005 | CGF.getContext().getTypeInfoInChars(Ty), |
| 4006 | CharUnits::fromQuantity(8), |
| 4007 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4008 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4009 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4010 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4011 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4012 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 4013 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4014 | bool IsSoftFloatABI; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4015 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4016 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 4017 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4018 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4019 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4020 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4021 | }; |
| 4022 | |
| 4023 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4024 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4025 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 4026 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4027 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4028 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4029 | // This is recovered from gcc output. |
| 4030 | return 1; // r1 is the dedicated stack pointer |
| 4031 | } |
| 4032 | |
| 4033 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4034 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4035 | }; |
| 4036 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4037 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4038 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4039 | // TODO: this implementation is now likely redundant with |
| 4040 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4041 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4042 | QualType Ty) const { |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4043 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4044 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4045 | // TODO: Implement this. For now ignore. |
| 4046 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4047 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4050 | // struct __va_list_tag { |
| 4051 | // unsigned char gpr; |
| 4052 | // unsigned char fpr; |
| 4053 | // unsigned short reserved; |
| 4054 | // void *overflow_arg_area; |
| 4055 | // void *reg_save_area; |
| 4056 | // }; |
| 4057 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4058 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4059 | bool isInt = |
| 4060 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4061 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4062 | |
| 4063 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4064 | // with the argument-lowering code. |
| 4065 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4066 | |
| 4067 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4068 | |
| 4069 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4070 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4071 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4072 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 4073 | } else { |
| 4074 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4075 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4076 | |
| 4077 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4078 | |
| 4079 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4080 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4081 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4082 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4083 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4084 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4085 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4086 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4087 | |
| 4088 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4089 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4090 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4091 | |
| 4092 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4093 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4094 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4095 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4096 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4097 | // Case 1: consume registers. |
| 4098 | Address RegAddr = Address::invalid(); |
| 4099 | { |
| 4100 | CGF.EmitBlock(UsingRegs); |
| 4101 | |
| 4102 | Address RegSaveAreaPtr = |
| 4103 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 4104 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4105 | CharUnits::fromQuantity(8)); |
| 4106 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4107 | |
| 4108 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4109 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4110 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4111 | CharUnits::fromQuantity(32)); |
| 4112 | } |
| 4113 | |
| 4114 | // Get the address of the saved value by scaling the number of |
| 4115 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4116 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4117 | llvm::Value *RegOffset = |
| 4118 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4119 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4120 | RegAddr.getPointer(), RegOffset), |
| 4121 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4122 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4123 | |
| 4124 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4125 | NumRegs = |
| 4126 | Builder.CreateAdd(NumRegs, |
| 4127 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4128 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4129 | |
| 4130 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4131 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4132 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4133 | // Case 2: consume space in the overflow area. |
| 4134 | Address MemAddr = Address::invalid(); |
| 4135 | { |
| 4136 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4137 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4138 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4139 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4140 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4141 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4142 | |
| 4143 | CharUnits Size; |
| 4144 | if (!isIndirect) { |
| 4145 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4146 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4147 | } else { |
| 4148 | Size = CGF.getPointerSize(); |
| 4149 | } |
| 4150 | |
| 4151 | Address OverflowAreaAddr = |
| 4152 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4153 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4154 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4155 | // Round up address of argument to alignment |
| 4156 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4157 | if (Align > OverflowAreaAlign) { |
| 4158 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4159 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4160 | Align); |
| 4161 | } |
| 4162 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4163 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4164 | |
| 4165 | // Increase the overflow area. |
| 4166 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4167 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4168 | CGF.EmitBranch(Cont); |
| 4169 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4170 | |
| 4171 | CGF.EmitBlock(Cont); |
| 4172 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4173 | // Merge the cases with a phi. |
| 4174 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4175 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4176 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4177 | // Load the pointer if the argument was passed indirectly. |
| 4178 | if (isIndirect) { |
| 4179 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4180 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4181 | } |
| 4182 | |
| 4183 | return Result; |
| 4184 | } |
| 4185 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4186 | bool |
| 4187 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4188 | llvm::Value *Address) const { |
| 4189 | // This is calculated from the LLVM and GCC tables and verified |
| 4190 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4191 | |
| 4192 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4193 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4194 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4195 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4196 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4197 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4198 | |
| 4199 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4200 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4201 | |
| 4202 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4203 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4204 | |
| 4205 | // 64-76 are various 4-byte special-purpose registers: |
| 4206 | // 64: mq |
| 4207 | // 65: lr |
| 4208 | // 66: ctr |
| 4209 | // 67: ap |
| 4210 | // 68-75 cr0-7 |
| 4211 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4212 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4213 | |
| 4214 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4215 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4216 | |
| 4217 | // 109: vrsave |
| 4218 | // 110: vscr |
| 4219 | // 111: spe_acc |
| 4220 | // 112: spefscr |
| 4221 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4222 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4223 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4224 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4225 | } |
| 4226 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4227 | // PowerPC-64 |
| 4228 | |
| 4229 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4230 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4231 | class PPC64_SVR4_ABIInfo : public ABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4232 | public: |
| 4233 | enum ABIKind { |
| 4234 | ELFv1 = 0, |
| 4235 | ELFv2 |
| 4236 | }; |
| 4237 | |
| 4238 | private: |
| 4239 | static const unsigned GPRBits = 64; |
| 4240 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4241 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4242 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4243 | |
| 4244 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4245 | // will be passed in a QPX register. |
| 4246 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4247 | if (!HasQPX) |
| 4248 | return false; |
| 4249 | |
| 4250 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4251 | unsigned NumElements = VT->getNumElements(); |
| 4252 | if (NumElements == 1) |
| 4253 | return false; |
| 4254 | |
| 4255 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4256 | if (getContext().getTypeSize(Ty) <= 256) |
| 4257 | return true; |
| 4258 | } else if (VT->getElementType()-> |
| 4259 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4260 | if (getContext().getTypeSize(Ty) <= 128) |
| 4261 | return true; |
| 4262 | } |
| 4263 | } |
| 4264 | |
| 4265 | return false; |
| 4266 | } |
| 4267 | |
| 4268 | bool IsQPXVectorTy(QualType Ty) const { |
| 4269 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4270 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4271 | |
| 4272 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4273 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4274 | bool SoftFloatABI) |
| 4275 | : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
| 4276 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4277 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4278 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4279 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4280 | |
| 4281 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4282 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4283 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4284 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4285 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4286 | uint64_t Members) const override; |
| 4287 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4288 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4289 | // Example: For aggregate arguments that fit in a register, we could |
| 4290 | // use getDirectInReg (as is done below for structs containing a single |
| 4291 | // floating-point value) to avoid pushing them to memory on function |
| 4292 | // entry. This would require changing the logic in PPCISelLowering |
| 4293 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4294 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4295 | if (!getCXXABI().classifyReturnType(FI)) |
| 4296 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4297 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4298 | // We rely on the default argument classification for the most part. |
| 4299 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4300 | // 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] | 4301 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4302 | if (T) { |
| 4303 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4304 | if (IsQPXVectorTy(T) || |
| 4305 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4306 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4307 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4308 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4309 | continue; |
| 4310 | } |
| 4311 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4312 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4313 | } |
| 4314 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4315 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4316 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4317 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4318 | }; |
| 4319 | |
| 4320 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4321 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4322 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4323 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4324 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4325 | bool SoftFloatABI) |
| 4326 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4327 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4328 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4329 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4330 | // This is recovered from gcc output. |
| 4331 | return 1; // r1 is the dedicated stack pointer |
| 4332 | } |
| 4333 | |
| 4334 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4335 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4336 | }; |
| 4337 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4338 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4339 | public: |
| 4340 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4341 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4342 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4343 | // This is recovered from gcc output. |
| 4344 | return 1; // r1 is the dedicated stack pointer |
| 4345 | } |
| 4346 | |
| 4347 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4348 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4349 | }; |
| 4350 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4351 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4352 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4353 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4354 | // extended to 64 bits. |
| 4355 | bool |
| 4356 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4357 | // Treat an enum type as its underlying type. |
| 4358 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4359 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4360 | |
| 4361 | // Promotable integer types are required to be promoted by the ABI. |
| 4362 | if (Ty->isPromotableIntegerType()) |
| 4363 | return true; |
| 4364 | |
| 4365 | // In addition to the usual promotable integer types, we also need to |
| 4366 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4367 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4368 | switch (BT->getKind()) { |
| 4369 | case BuiltinType::Int: |
| 4370 | case BuiltinType::UInt: |
| 4371 | return true; |
| 4372 | default: |
| 4373 | break; |
| 4374 | } |
| 4375 | |
| 4376 | return false; |
| 4377 | } |
| 4378 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4379 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4380 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4381 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4382 | // Complex types are passed just like their elements. |
| 4383 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4384 | Ty = CTy->getElementType(); |
| 4385 | |
| 4386 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4387 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4388 | if (IsQPXVectorTy(Ty)) { |
| 4389 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4390 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4391 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4392 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4393 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4394 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4395 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4396 | |
| 4397 | // For single-element float/vector structs, we consider the whole type |
| 4398 | // to have the same alignment requirements as its single element. |
| 4399 | const Type *AlignAsType = nullptr; |
| 4400 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4401 | if (EltType) { |
| 4402 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4403 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4404 | getContext().getTypeSize(EltType) == 128) || |
| 4405 | (BT && BT->isFloatingPoint())) |
| 4406 | AlignAsType = EltType; |
| 4407 | } |
| 4408 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4409 | // Likewise for ELFv2 homogeneous aggregates. |
| 4410 | const Type *Base = nullptr; |
| 4411 | uint64_t Members = 0; |
| 4412 | if (!AlignAsType && Kind == ELFv2 && |
| 4413 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4414 | AlignAsType = Base; |
| 4415 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4416 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4417 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4418 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4419 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4420 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4421 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4422 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4423 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4424 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4425 | |
| 4426 | // Otherwise, we only need alignment for any aggregate type that |
| 4427 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4428 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4429 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4430 | return CharUnits::fromQuantity(32); |
| 4431 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4432 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4433 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4434 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4437 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4438 | /// aggregate. Base is set to the base element type, and Members is set |
| 4439 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4440 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4441 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4442 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4443 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4444 | if (NElements == 0) |
| 4445 | return false; |
| 4446 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4447 | return false; |
| 4448 | Members *= NElements; |
| 4449 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4450 | const RecordDecl *RD = RT->getDecl(); |
| 4451 | if (RD->hasFlexibleArrayMember()) |
| 4452 | return false; |
| 4453 | |
| 4454 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4455 | |
| 4456 | // If this is a C++ record, check the bases first. |
| 4457 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4458 | for (const auto &I : CXXRD->bases()) { |
| 4459 | // Ignore empty records. |
| 4460 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4461 | continue; |
| 4462 | |
| 4463 | uint64_t FldMembers; |
| 4464 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4465 | return false; |
| 4466 | |
| 4467 | Members += FldMembers; |
| 4468 | } |
| 4469 | } |
| 4470 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4471 | for (const auto *FD : RD->fields()) { |
| 4472 | // Ignore (non-zero arrays of) empty records. |
| 4473 | QualType FT = FD->getType(); |
| 4474 | while (const ConstantArrayType *AT = |
| 4475 | getContext().getAsConstantArrayType(FT)) { |
| 4476 | if (AT->getSize().getZExtValue() == 0) |
| 4477 | return false; |
| 4478 | FT = AT->getElementType(); |
| 4479 | } |
| 4480 | if (isEmptyRecord(getContext(), FT, true)) |
| 4481 | continue; |
| 4482 | |
| 4483 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4484 | if (getContext().getLangOpts().CPlusPlus && |
| 4485 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4486 | continue; |
| 4487 | |
| 4488 | uint64_t FldMembers; |
| 4489 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4490 | return false; |
| 4491 | |
| 4492 | Members = (RD->isUnion() ? |
| 4493 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4494 | } |
| 4495 | |
| 4496 | if (!Base) |
| 4497 | return false; |
| 4498 | |
| 4499 | // Ensure there is no padding. |
| 4500 | if (getContext().getTypeSize(Base) * Members != |
| 4501 | getContext().getTypeSize(Ty)) |
| 4502 | return false; |
| 4503 | } else { |
| 4504 | Members = 1; |
| 4505 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4506 | Members = 2; |
| 4507 | Ty = CT->getElementType(); |
| 4508 | } |
| 4509 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4510 | // Most ABIs only support float, double, and some vector type widths. |
| 4511 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4512 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4513 | |
| 4514 | // The base type must be the same for all members. Types that |
| 4515 | // agree in both total size and mode (float vs. vector) are |
| 4516 | // treated as being equivalent here. |
| 4517 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4518 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4519 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4520 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4521 | // so make sure to widen it explicitly. |
| 4522 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4523 | QualType EltTy = VT->getElementType(); |
| 4524 | unsigned NumElements = |
| 4525 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4526 | Base = getContext() |
| 4527 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4528 | .getTypePtr(); |
| 4529 | } |
| 4530 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4531 | |
| 4532 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4533 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4534 | return false; |
| 4535 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4536 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4537 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4538 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4539 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4540 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4541 | // double, long double, or 128-bit vectors. |
| 4542 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4543 | if (BT->getKind() == BuiltinType::Float || |
| 4544 | BT->getKind() == BuiltinType::Double || |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4545 | BT->getKind() == BuiltinType::LongDouble) { |
| 4546 | if (IsSoftFloatABI) |
| 4547 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4548 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4549 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4550 | } |
| 4551 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4552 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4553 | return true; |
| 4554 | } |
| 4555 | return false; |
| 4556 | } |
| 4557 | |
| 4558 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4559 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4560 | // Vector types require one register, floating point types require one |
| 4561 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4562 | uint32_t NumRegs = |
| 4563 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4564 | |
| 4565 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4566 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4569 | ABIArgInfo |
| 4570 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4571 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4572 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4573 | if (Ty->isAnyComplexType()) |
| 4574 | return ABIArgInfo::getDirect(); |
| 4575 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4576 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4577 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4578 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4579 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4580 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4581 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4582 | else if (Size < 128) { |
| 4583 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4584 | return ABIArgInfo::getDirect(CoerceTy); |
| 4585 | } |
| 4586 | } |
| 4587 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4588 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4589 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4590 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4591 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4592 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4593 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4594 | |
| 4595 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4596 | const Type *Base = nullptr; |
| 4597 | uint64_t Members = 0; |
| 4598 | if (Kind == ELFv2 && |
| 4599 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4600 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4601 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4602 | return ABIArgInfo::getDirect(CoerceTy); |
| 4603 | } |
| 4604 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4605 | // If an aggregate may end up fully in registers, we do not |
| 4606 | // use the ByVal method, but pass the aggregate as array. |
| 4607 | // This is usually beneficial since we avoid forcing the |
| 4608 | // back-end to store the argument to memory. |
| 4609 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4610 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4611 | llvm::Type *CoerceTy; |
| 4612 | |
| 4613 | // Types up to 8 bytes are passed as integer type (which will be |
| 4614 | // properly aligned in the argument save area doubleword). |
| 4615 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4616 | CoerceTy = |
| 4617 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4618 | // Larger types are passed as arrays, with the base type selected |
| 4619 | // according to the required alignment in the save area. |
| 4620 | else { |
| 4621 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4622 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4623 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4624 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4625 | } |
| 4626 | |
| 4627 | return ABIArgInfo::getDirect(CoerceTy); |
| 4628 | } |
| 4629 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4630 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4631 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4632 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4633 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | return (isPromotableTypeForABI(Ty) ? |
| 4637 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4638 | } |
| 4639 | |
| 4640 | ABIArgInfo |
| 4641 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4642 | if (RetTy->isVoidType()) |
| 4643 | return ABIArgInfo::getIgnore(); |
| 4644 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4645 | if (RetTy->isAnyComplexType()) |
| 4646 | return ABIArgInfo::getDirect(); |
| 4647 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4648 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4649 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4650 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4651 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4652 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4653 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4654 | else if (Size < 128) { |
| 4655 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4656 | return ABIArgInfo::getDirect(CoerceTy); |
| 4657 | } |
| 4658 | } |
| 4659 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4660 | if (isAggregateTypeForABI(RetTy)) { |
| 4661 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4662 | const Type *Base = nullptr; |
| 4663 | uint64_t Members = 0; |
| 4664 | if (Kind == ELFv2 && |
| 4665 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4666 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4667 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4668 | return ABIArgInfo::getDirect(CoerceTy); |
| 4669 | } |
| 4670 | |
| 4671 | // ELFv2 small aggregates are returned in up to two registers. |
| 4672 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4673 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4674 | if (Bits == 0) |
| 4675 | return ABIArgInfo::getIgnore(); |
| 4676 | |
| 4677 | llvm::Type *CoerceTy; |
| 4678 | if (Bits > GPRBits) { |
| 4679 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4680 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4681 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4682 | CoerceTy = |
| 4683 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4684 | return ABIArgInfo::getDirect(CoerceTy); |
| 4685 | } |
| 4686 | |
| 4687 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4688 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4689 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4690 | |
| 4691 | return (isPromotableTypeForABI(RetTy) ? |
| 4692 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4693 | } |
| 4694 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4695 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4696 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4697 | QualType Ty) const { |
| 4698 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4699 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4700 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4701 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4702 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4703 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4704 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4705 | // in separate doublewords. However, Clang expects us to produce a |
| 4706 | // pointer to a structure with the two parts packed tightly. So generate |
| 4707 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4708 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4709 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4710 | CharUnits EltSize = TypeInfo.first / 2; |
| 4711 | if (EltSize < SlotSize) { |
| 4712 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4713 | SlotSize * 2, SlotSize, |
| 4714 | SlotSize, /*AllowHigher*/ true); |
| 4715 | |
| 4716 | Address RealAddr = Addr; |
| 4717 | Address ImagAddr = RealAddr; |
| 4718 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4719 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4720 | SlotSize - EltSize); |
| 4721 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4722 | 2 * SlotSize - EltSize); |
| 4723 | } else { |
| 4724 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4725 | } |
| 4726 | |
| 4727 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4728 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4729 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4730 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4731 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4732 | |
| 4733 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4734 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4735 | /*init*/ true); |
| 4736 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4737 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4740 | // Otherwise, just use the general rule. |
| 4741 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4742 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
| 4745 | static bool |
| 4746 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4747 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4748 | // This is calculated from the LLVM and GCC tables and verified |
| 4749 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4750 | |
| 4751 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4752 | |
| 4753 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4754 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4755 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4756 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4757 | |
| 4758 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4759 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4760 | |
| 4761 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4762 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4763 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4764 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4765 | // 64: mq |
| 4766 | // 65: lr |
| 4767 | // 66: ctr |
| 4768 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4769 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4770 | |
| 4771 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4772 | // 68-75 cr0-7 |
| 4773 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4774 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4775 | |
| 4776 | // 77-108: v0-31, the 16-byte vector registers |
| 4777 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4778 | |
| 4779 | // 109: vrsave |
| 4780 | // 110: vscr |
| 4781 | // 111: spe_acc |
| 4782 | // 112: spefscr |
| 4783 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4784 | // 114: tfhar |
| 4785 | // 115: tfiar |
| 4786 | // 116: texasr |
| 4787 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4788 | |
| 4789 | return false; |
| 4790 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4791 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4792 | bool |
| 4793 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4794 | CodeGen::CodeGenFunction &CGF, |
| 4795 | llvm::Value *Address) const { |
| 4796 | |
| 4797 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4798 | } |
| 4799 | |
| 4800 | bool |
| 4801 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4802 | llvm::Value *Address) const { |
| 4803 | |
| 4804 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4805 | } |
| 4806 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4807 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4808 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4809 | //===----------------------------------------------------------------------===// |
| 4810 | |
| 4811 | namespace { |
| 4812 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4813 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4814 | public: |
| 4815 | enum ABIKind { |
| 4816 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4817 | DarwinPCS, |
| 4818 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4819 | }; |
| 4820 | |
| 4821 | private: |
| 4822 | ABIKind Kind; |
| 4823 | |
| 4824 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4825 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4826 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4827 | |
| 4828 | private: |
| 4829 | ABIKind getABIKind() const { return Kind; } |
| 4830 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4831 | |
| 4832 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4833 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4834 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4835 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4836 | uint64_t Members) const override; |
| 4837 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4838 | bool isIllegalVectorType(QualType Ty) const; |
| 4839 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4840 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4841 | if (!getCXXABI().classifyReturnType(FI)) |
| 4842 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4843 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4844 | for (auto &it : FI.arguments()) |
| 4845 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4846 | } |
| 4847 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4848 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4849 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4850 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4851 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4852 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4853 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4854 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4855 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4856 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 4857 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4858 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4859 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4860 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4861 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4862 | QualType Ty) const override; |
| 4863 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4864 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 4865 | ArrayRef<llvm::Type*> scalars, |
| 4866 | bool asReturnValue) const override { |
| 4867 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4868 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 4869 | bool isSwiftErrorInRegister() const override { |
| 4870 | return true; |
| 4871 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 4872 | |
| 4873 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 4874 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4875 | }; |
| 4876 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4877 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4878 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4879 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4880 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4881 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4882 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Saleem Abdulrasool | 40db477 | 2017-02-11 23:03:13 +0000 | [diff] [blame] | 4883 | return "mov\tfp, fp\t\t# marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4886 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4887 | return 31; |
| 4888 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4889 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4890 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4891 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 4892 | |
| 4893 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 4894 | public: |
| 4895 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 4896 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 4897 | |
| 4898 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 4899 | llvm::SmallString<24> &Opt) const override { |
| 4900 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 4901 | } |
| 4902 | |
| 4903 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 4904 | llvm::SmallString<32> &Opt) const override { |
| 4905 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 4906 | } |
| 4907 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4908 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4909 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4910 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4911 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4912 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4913 | // Handle illegal vector types here. |
| 4914 | if (isIllegalVectorType(Ty)) { |
| 4915 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4916 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 4917 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4918 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 4919 | return ABIArgInfo::getDirect(ResType); |
| 4920 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4921 | if (Size <= 32) { |
| 4922 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4923 | return ABIArgInfo::getDirect(ResType); |
| 4924 | } |
| 4925 | if (Size == 64) { |
| 4926 | llvm::Type *ResType = |
| 4927 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4928 | return ABIArgInfo::getDirect(ResType); |
| 4929 | } |
| 4930 | if (Size == 128) { |
| 4931 | llvm::Type *ResType = |
| 4932 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4933 | return ABIArgInfo::getDirect(ResType); |
| 4934 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4935 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4936 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4937 | |
| 4938 | if (!isAggregateTypeForABI(Ty)) { |
| 4939 | // Treat an enum type as its underlying type. |
| 4940 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4941 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4942 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4943 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4944 | ? ABIArgInfo::getExtend() |
| 4945 | : ABIArgInfo::getDirect()); |
| 4946 | } |
| 4947 | |
| 4948 | // Structures with either a non-trivial destructor or a non-trivial |
| 4949 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4950 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4951 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4952 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4956 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4957 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4958 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 4959 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4960 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4961 | return ABIArgInfo::getIgnore(); |
| 4962 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4963 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 4964 | // 0. |
| 4965 | if (IsEmpty && Size == 0) |
| 4966 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4967 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4968 | } |
| 4969 | |
| 4970 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4971 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4972 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4973 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4974 | return ABIArgInfo::getDirect( |
| 4975 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
| 4978 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4979 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 4980 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 4981 | // same size and alignment. |
| 4982 | if (getTarget().isRenderScriptTarget()) { |
| 4983 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 4984 | } |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4985 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 4986 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4987 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4988 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4989 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4990 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4991 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4992 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4993 | } |
| 4994 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4995 | } |
| 4996 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4997 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4998 | } |
| 4999 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5000 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5001 | if (RetTy->isVoidType()) |
| 5002 | return ABIArgInfo::getIgnore(); |
| 5003 | |
| 5004 | // Large vector types should be returned via memory. |
| 5005 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5006 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5007 | |
| 5008 | if (!isAggregateTypeForABI(RetTy)) { |
| 5009 | // Treat an enum type as its underlying type. |
| 5010 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5011 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5012 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5013 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 5014 | ? ABIArgInfo::getExtend() |
| 5015 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5018 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5019 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5020 | return ABIArgInfo::getIgnore(); |
| 5021 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5022 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5023 | uint64_t Members = 0; |
| 5024 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5025 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 5026 | return ABIArgInfo::getDirect(); |
| 5027 | |
| 5028 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5029 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5030 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5031 | // same size and alignment. |
| 5032 | if (getTarget().isRenderScriptTarget()) { |
| 5033 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5034 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5035 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5036 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5037 | |
| 5038 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5039 | // For aggregates with 16-byte alignment, we use i128. |
| 5040 | if (Alignment < 128 && Size == 128) { |
| 5041 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5042 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5043 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5044 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5045 | } |
| 5046 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5047 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5050 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5051 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5052 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5053 | // Check whether VT is legal. |
| 5054 | unsigned NumElements = VT->getNumElements(); |
| 5055 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5056 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5057 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5058 | return true; |
| 5059 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5060 | } |
| 5061 | return false; |
| 5062 | } |
| 5063 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5064 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5065 | llvm::Type *eltTy, |
| 5066 | unsigned elts) const { |
| 5067 | if (!llvm::isPowerOf2_32(elts)) |
| 5068 | return false; |
| 5069 | if (totalSize.getQuantity() != 8 && |
| 5070 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5071 | return false; |
| 5072 | return true; |
| 5073 | } |
| 5074 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5075 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5076 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5077 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5078 | // but with the difference that any floating-point type is allowed, |
| 5079 | // including __fp16. |
| 5080 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5081 | if (BT->isFloatingPoint()) |
| 5082 | return true; |
| 5083 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5084 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5085 | if (VecSize == 64 || VecSize == 128) |
| 5086 | return true; |
| 5087 | } |
| 5088 | return false; |
| 5089 | } |
| 5090 | |
| 5091 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5092 | uint64_t Members) const { |
| 5093 | return Members <= 4; |
| 5094 | } |
| 5095 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5096 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5097 | QualType Ty, |
| 5098 | CodeGenFunction &CGF) const { |
| 5099 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5100 | bool IsIndirect = AI.isIndirect(); |
| 5101 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5102 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5103 | if (IsIndirect) |
| 5104 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5105 | else if (AI.getCoerceToType()) |
| 5106 | BaseTy = AI.getCoerceToType(); |
| 5107 | |
| 5108 | unsigned NumRegs = 1; |
| 5109 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5110 | BaseTy = ArrTy->getElementType(); |
| 5111 | NumRegs = ArrTy->getNumElements(); |
| 5112 | } |
| 5113 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5114 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5115 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5116 | // Standard, section B.4: |
| 5117 | // |
| 5118 | // struct { |
| 5119 | // void *__stack; |
| 5120 | // void *__gr_top; |
| 5121 | // void *__vr_top; |
| 5122 | // int __gr_offs; |
| 5123 | // int __vr_offs; |
| 5124 | // }; |
| 5125 | |
| 5126 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5127 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5128 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5129 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5130 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5131 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5132 | CharUnits TyAlign = TyInfo.second; |
| 5133 | |
| 5134 | Address reg_offs_p = Address::invalid(); |
| 5135 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5136 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5137 | CharUnits reg_top_offset; |
| 5138 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5139 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5140 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5141 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5142 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5143 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5144 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5145 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5146 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5147 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5148 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5149 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5150 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5151 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 5152 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5153 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5154 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5155 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5156 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | //======================================= |
| 5160 | // Find out where argument was passed |
| 5161 | //======================================= |
| 5162 | |
| 5163 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5164 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5165 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5166 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5167 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5168 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5169 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5170 | |
| 5171 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5172 | |
| 5173 | // 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] | 5174 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5175 | CGF.EmitBlock(MaybeRegBlock); |
| 5176 | |
| 5177 | // Integer arguments may need to correct register alignment (for example a |
| 5178 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5179 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5180 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5181 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5182 | |
| 5183 | reg_offs = CGF.Builder.CreateAdd( |
| 5184 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5185 | "align_regoffs"); |
| 5186 | reg_offs = CGF.Builder.CreateAnd( |
| 5187 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5188 | "aligned_regoffs"); |
| 5189 | } |
| 5190 | |
| 5191 | // 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] | 5192 | // The fact that this is done unconditionally reflects the fact that |
| 5193 | // allocating an argument to the stack also uses up all the remaining |
| 5194 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5195 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5196 | NewOffset = CGF.Builder.CreateAdd( |
| 5197 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5198 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5199 | |
| 5200 | // Now we're in a position to decide whether this argument really was in |
| 5201 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5202 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5203 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5204 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5205 | |
| 5206 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5207 | |
| 5208 | //======================================= |
| 5209 | // Argument was in registers |
| 5210 | //======================================= |
| 5211 | |
| 5212 | // Now we emit the code for if the argument was originally passed in |
| 5213 | // registers. First start the appropriate block: |
| 5214 | CGF.EmitBlock(InRegBlock); |
| 5215 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5216 | llvm::Value *reg_top = nullptr; |
| 5217 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 5218 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5219 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5220 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5221 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5222 | Address RegAddr = Address::invalid(); |
| 5223 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5224 | |
| 5225 | if (IsIndirect) { |
| 5226 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5227 | // stored registers or on the stack will actually be a struct **. |
| 5228 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5229 | } |
| 5230 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5231 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5232 | uint64_t NumMembers = 0; |
| 5233 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5234 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5235 | // Homogeneous aggregates passed in registers will have their elements split |
| 5236 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5237 | // qN+1, ...). We reload and store into a temporary local variable |
| 5238 | // contiguously. |
| 5239 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5240 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5241 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5242 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5243 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5244 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5245 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5246 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5247 | int Offset = 0; |
| 5248 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5249 | BaseTyInfo.first.getQuantity() < 16) |
| 5250 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5251 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5252 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5253 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5254 | Address LoadAddr = |
| 5255 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5256 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5257 | |
| 5258 | Address StoreAddr = |
| 5259 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5260 | |
| 5261 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5262 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5263 | } |
| 5264 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5265 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5266 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5267 | // Otherwise the object is contiguous in memory. |
| 5268 | |
| 5269 | // It might be right-aligned in its slot. |
| 5270 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5271 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5272 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5273 | TyInfo.first < SlotSize) { |
| 5274 | CharUnits Offset = SlotSize - TyInfo.first; |
| 5275 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5278 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
| 5281 | CGF.EmitBranch(ContBlock); |
| 5282 | |
| 5283 | //======================================= |
| 5284 | // Argument was on the stack |
| 5285 | //======================================= |
| 5286 | CGF.EmitBlock(OnStackBlock); |
| 5287 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5288 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 5289 | CharUnits::Zero(), "stack_p"); |
| 5290 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5291 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5292 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5293 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5294 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5295 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5296 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5297 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5298 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5299 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5300 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5301 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5302 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5303 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5304 | "align_stack"); |
| 5305 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5306 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5307 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5308 | Address OnStackAddr(OnStackPtr, |
| 5309 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5310 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5311 | // All stack slots are multiples of 8 bytes. |
| 5312 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5313 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5314 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5315 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5316 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5317 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5318 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5319 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5320 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5321 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5322 | |
| 5323 | // Write the new value of __stack for the next call to va_arg |
| 5324 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5325 | |
| 5326 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5327 | TyInfo.first < StackSlotSize) { |
| 5328 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 5329 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5330 | } |
| 5331 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5332 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5333 | |
| 5334 | CGF.EmitBranch(ContBlock); |
| 5335 | |
| 5336 | //======================================= |
| 5337 | // Tidy up |
| 5338 | //======================================= |
| 5339 | CGF.EmitBlock(ContBlock); |
| 5340 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5341 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5342 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5343 | |
| 5344 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5345 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 5346 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5347 | |
| 5348 | return ResAddr; |
| 5349 | } |
| 5350 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5351 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5352 | CodeGenFunction &CGF) const { |
| 5353 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5354 | // illegal vector types. Lower VAArg here for these cases and use |
| 5355 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5356 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5357 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5358 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5359 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5360 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5361 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5362 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5363 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5364 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5365 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5366 | } |
| 5367 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5368 | // The size of the actual thing passed, which might end up just |
| 5369 | // being a pointer for indirect types. |
| 5370 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5371 | |
| 5372 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5373 | // aggregates should be passed indirectly. |
| 5374 | bool IsIndirect = false; |
| 5375 | if (TyInfo.first.getQuantity() > 16) { |
| 5376 | const Type *Base = nullptr; |
| 5377 | uint64_t Members = 0; |
| 5378 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5381 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5382 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5383 | } |
| 5384 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5385 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5386 | QualType Ty) const { |
| 5387 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5388 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5389 | CharUnits::fromQuantity(8), |
| 5390 | /*allowHigherAlign*/ false); |
| 5391 | } |
| 5392 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5393 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5394 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5395 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5396 | |
| 5397 | namespace { |
| 5398 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5399 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5400 | public: |
| 5401 | enum ABIKind { |
| 5402 | APCS = 0, |
| 5403 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5404 | AAPCS_VFP = 2, |
| 5405 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5406 | }; |
| 5407 | |
| 5408 | private: |
| 5409 | ABIKind Kind; |
| 5410 | |
| 5411 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5412 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5413 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5414 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5415 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5416 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5417 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5418 | switch (getTarget().getTriple().getEnvironment()) { |
| 5419 | case llvm::Triple::Android: |
| 5420 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5421 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5422 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5423 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5424 | case llvm::Triple::MuslEABI: |
| 5425 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5426 | return true; |
| 5427 | default: |
| 5428 | return false; |
| 5429 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5432 | bool isEABIHF() const { |
| 5433 | switch (getTarget().getTriple().getEnvironment()) { |
| 5434 | case llvm::Triple::EABIHF: |
| 5435 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5436 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5437 | return true; |
| 5438 | default: |
| 5439 | return false; |
| 5440 | } |
| 5441 | } |
| 5442 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5443 | ABIKind getABIKind() const { return Kind; } |
| 5444 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5445 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 5446 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5447 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5448 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5449 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5450 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5451 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5452 | uint64_t Members) const override; |
| 5453 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5454 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5455 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5456 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5457 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5458 | |
| 5459 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5460 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5461 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5462 | |
| 5463 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 5464 | ArrayRef<llvm::Type*> scalars, |
| 5465 | bool asReturnValue) const override { |
| 5466 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5467 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5468 | bool isSwiftErrorInRegister() const override { |
| 5469 | return true; |
| 5470 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5471 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5472 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5473 | }; |
| 5474 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5475 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5476 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5477 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5478 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5479 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5480 | const ARMABIInfo &getABIInfo() const { |
| 5481 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5482 | } |
| 5483 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5484 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5485 | return 13; |
| 5486 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5487 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5488 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5489 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 5490 | } |
| 5491 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5492 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5493 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5494 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5495 | |
| 5496 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5497 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5498 | return false; |
| 5499 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5500 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5501 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5502 | if (getABIInfo().isEABI()) return 88; |
| 5503 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5504 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5505 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5506 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5507 | CodeGen::CodeGenModule &CGM, |
| 5508 | ForDefinition_t IsForDefinition) const override { |
| 5509 | if (!IsForDefinition) |
| 5510 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5511 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5512 | if (!FD) |
| 5513 | return; |
| 5514 | |
| 5515 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5516 | if (!Attr) |
| 5517 | return; |
| 5518 | |
| 5519 | const char *Kind; |
| 5520 | switch (Attr->getInterrupt()) { |
| 5521 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5522 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5523 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5524 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5525 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5526 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5527 | } |
| 5528 | |
| 5529 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5530 | |
| 5531 | Fn->addFnAttr("interrupt", Kind); |
| 5532 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5533 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5534 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5535 | return; |
| 5536 | |
| 5537 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5538 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5539 | // the backend to perform a realignment as part of the function prologue. |
| 5540 | llvm::AttrBuilder B; |
| 5541 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5542 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5543 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5544 | }; |
| 5545 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5546 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5547 | public: |
| 5548 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5549 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5550 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5551 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5552 | CodeGen::CodeGenModule &CGM, |
| 5553 | ForDefinition_t IsForDefinition) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5554 | |
| 5555 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5556 | llvm::SmallString<24> &Opt) const override { |
| 5557 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5558 | } |
| 5559 | |
| 5560 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5561 | llvm::SmallString<32> &Opt) const override { |
| 5562 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5563 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5564 | }; |
| 5565 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5566 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5567 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 5568 | ForDefinition_t IsForDefinition) const { |
| 5569 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition); |
| 5570 | if (!IsForDefinition) |
| 5571 | return; |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5572 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 5573 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5574 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5575 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5576 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5577 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5578 | FI.getReturnInfo() = |
| 5579 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5580 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5581 | for (auto &I : FI.arguments()) |
| 5582 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5583 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5584 | // Always honor user-specified calling convention. |
| 5585 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5586 | return; |
| 5587 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5588 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5589 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5590 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5591 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5592 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5593 | /// Return the default calling convention that LLVM will use. |
| 5594 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5595 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5596 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5597 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5598 | else if (isEABI()) |
| 5599 | return llvm::CallingConv::ARM_AAPCS; |
| 5600 | else |
| 5601 | return llvm::CallingConv::ARM_APCS; |
| 5602 | } |
| 5603 | |
| 5604 | /// Return the calling convention that our ABI would like us to use |
| 5605 | /// as the C calling convention. |
| 5606 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5607 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5608 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5609 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5610 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5611 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5612 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5613 | llvm_unreachable("bad ABI kind"); |
| 5614 | } |
| 5615 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5616 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5617 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5618 | |
| 5619 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5620 | // they'd just match what LLVM will infer from the triple. |
| 5621 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5622 | if (abiCC != getLLVMDefaultCC()) |
| 5623 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5624 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5625 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 5626 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 5627 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
Peter Smith | 32e2675 | 2017-07-27 10:43:53 +0000 | [diff] [blame] | 5628 | |
| 5629 | // The Run-time ABI for the ARM Architecture section 4.1.2 requires |
| 5630 | // AEABI-complying FP helper functions to use the base AAPCS. |
| 5631 | // These AEABI functions are expanded in the ARM llvm backend, all the builtin |
| 5632 | // support functions emitted by clang such as the _Complex helpers follow the |
| 5633 | // abiCC. |
| 5634 | if (abiCC != getLLVMDefaultCC()) |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5635 | BuiltinCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5636 | } |
| 5637 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5638 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5639 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5640 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5641 | // A single-precision floating-point type (including promoted |
| 5642 | // half-precision types); A double-precision floating-point type; |
| 5643 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5644 | // with a Base Type of a single- or double-precision floating-point type, |
| 5645 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5646 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5647 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5648 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5649 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5650 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5651 | // Handle illegal vector types here. |
| 5652 | if (isIllegalVectorType(Ty)) { |
| 5653 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5654 | if (Size <= 32) { |
| 5655 | llvm::Type *ResType = |
| 5656 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5657 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5658 | } |
| 5659 | if (Size == 64) { |
| 5660 | llvm::Type *ResType = llvm::VectorType::get( |
| 5661 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5662 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5663 | } |
| 5664 | if (Size == 128) { |
| 5665 | llvm::Type *ResType = llvm::VectorType::get( |
| 5666 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5667 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5668 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5669 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5670 | } |
| 5671 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5672 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 5673 | // unspecified. This is not done for OpenCL as it handles the half type |
| 5674 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5675 | if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5676 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5677 | llvm::Type::getFloatTy(getVMContext()) : |
| 5678 | llvm::Type::getInt32Ty(getVMContext()); |
| 5679 | return ABIArgInfo::getDirect(ResType); |
| 5680 | } |
| 5681 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5682 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5683 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5684 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5685 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5686 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5687 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5688 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5689 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5690 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5691 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5692 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5693 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5694 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5695 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5696 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5697 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5698 | return ABIArgInfo::getIgnore(); |
| 5699 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5700 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5701 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5702 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5703 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5704 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5705 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5706 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5707 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5708 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5709 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5710 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5711 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5712 | // this convention even for a variadic function: the backend will use GPRs |
| 5713 | // if needed. |
| 5714 | const Type *Base = nullptr; |
| 5715 | uint64_t Members = 0; |
| 5716 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5717 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5718 | llvm::Type *Ty = |
| 5719 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5720 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5721 | } |
| 5722 | } |
| 5723 | |
| 5724 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5725 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5726 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5727 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5728 | // and a pointer is passed. |
| 5729 | return ABIArgInfo::getIndirect( |
| 5730 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5731 | } |
| 5732 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5733 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5734 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5735 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5736 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5737 | uint64_t ABIAlign = 4; |
| 5738 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5739 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5740 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5741 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5742 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5743 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5744 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5745 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5746 | /*ByVal=*/true, |
| 5747 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5748 | } |
| 5749 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5750 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5751 | // same size and alignment. |
| 5752 | if (getTarget().isRenderScriptTarget()) { |
| 5753 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5754 | } |
| 5755 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5756 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5757 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5758 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5759 | // FIXME: Try to match the types of the arguments more accurately where |
| 5760 | // we can. |
| 5761 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5762 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5763 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5764 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5765 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5766 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5767 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5768 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5769 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5770 | } |
| 5771 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5772 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5773 | llvm::LLVMContext &VMContext) { |
| 5774 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5775 | // is called integer-like if its size is less than or equal to one word, and |
| 5776 | // the offset of each of its addressable sub-fields is zero. |
| 5777 | |
| 5778 | uint64_t Size = Context.getTypeSize(Ty); |
| 5779 | |
| 5780 | // Check that the type fits in a word. |
| 5781 | if (Size > 32) |
| 5782 | return false; |
| 5783 | |
| 5784 | // FIXME: Handle vector types! |
| 5785 | if (Ty->isVectorType()) |
| 5786 | return false; |
| 5787 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5788 | // Float types are never treated as "integer like". |
| 5789 | if (Ty->isRealFloatingType()) |
| 5790 | return false; |
| 5791 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5792 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5793 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5794 | return true; |
| 5795 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5796 | // Small complex integer types are "integer like". |
| 5797 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5798 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5799 | |
| 5800 | // Single element and zero sized arrays should be allowed, by the definition |
| 5801 | // above, but they are not. |
| 5802 | |
| 5803 | // Otherwise, it must be a record type. |
| 5804 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5805 | if (!RT) return false; |
| 5806 | |
| 5807 | // Ignore records with flexible arrays. |
| 5808 | const RecordDecl *RD = RT->getDecl(); |
| 5809 | if (RD->hasFlexibleArrayMember()) |
| 5810 | return false; |
| 5811 | |
| 5812 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5813 | // like". |
| 5814 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5815 | |
| 5816 | bool HadField = false; |
| 5817 | unsigned idx = 0; |
| 5818 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5819 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5820 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5821 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5822 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5823 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5824 | // struct { int : 0; int x } |
| 5825 | // is non-integer like according to gcc. |
| 5826 | if (FD->isBitField()) { |
| 5827 | if (!RD->isUnion()) |
| 5828 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5829 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5830 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5831 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5832 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5833 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5834 | } |
| 5835 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5836 | // Check if this field is at offset 0. |
| 5837 | if (Layout.getFieldOffset(idx) != 0) |
| 5838 | return false; |
| 5839 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5840 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5841 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5842 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5843 | // Only allow at most one field in a structure. This doesn't match the |
| 5844 | // wording above, but follows gcc in situations with a field following an |
| 5845 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5846 | if (!RD->isUnion()) { |
| 5847 | if (HadField) |
| 5848 | return false; |
| 5849 | |
| 5850 | HadField = true; |
| 5851 | } |
| 5852 | } |
| 5853 | |
| 5854 | return true; |
| 5855 | } |
| 5856 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5857 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5858 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5859 | bool IsEffectivelyAAPCS_VFP = |
| 5860 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5861 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5862 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5863 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5864 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5865 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5866 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5867 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5868 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5869 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5870 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5871 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5872 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5873 | if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5874 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5875 | llvm::Type::getFloatTy(getVMContext()) : |
| 5876 | llvm::Type::getInt32Ty(getVMContext()); |
| 5877 | return ABIArgInfo::getDirect(ResType); |
| 5878 | } |
| 5879 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5880 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5881 | // Treat an enum type as its underlying type. |
| 5882 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5883 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5884 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5885 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5886 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5887 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5888 | |
| 5889 | // Are we following APCS? |
| 5890 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5891 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5892 | return ABIArgInfo::getIgnore(); |
| 5893 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5894 | // Complex types are all returned as packed integers. |
| 5895 | // |
| 5896 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5897 | // correctly. |
| 5898 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5899 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5900 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5901 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5902 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5903 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5904 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5905 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5906 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5907 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5908 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5909 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5910 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5911 | } |
| 5912 | |
| 5913 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5914 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5915 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5916 | |
| 5917 | // Otherwise this is an AAPCS variant. |
| 5918 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5919 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5920 | return ABIArgInfo::getIgnore(); |
| 5921 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5922 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5923 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5924 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5925 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5926 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5927 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5928 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5929 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5930 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5931 | } |
| 5932 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5933 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5934 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5935 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5936 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5937 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 5938 | // same size and alignment. |
| 5939 | if (getTarget().isRenderScriptTarget()) { |
| 5940 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5941 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5942 | if (getDataLayout().isBigEndian()) |
| 5943 | // 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] | 5944 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5945 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5946 | // Return in the smallest viable integer type. |
| 5947 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5948 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5949 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5950 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5951 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5952 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5953 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5954 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5955 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5956 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5957 | } |
| 5958 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5959 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5960 | } |
| 5961 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5962 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5963 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 5964 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 5965 | if (isAndroid()) { |
| 5966 | // Android shipped using Clang 3.1, which supported a slightly different |
| 5967 | // vector ABI. The primary differences were that 3-element vector types |
| 5968 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 5969 | // accepts that legacy behavior for Android only. |
| 5970 | // Check whether VT is legal. |
| 5971 | unsigned NumElements = VT->getNumElements(); |
| 5972 | // NumElements should be power of 2 or equal to 3. |
| 5973 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 5974 | return true; |
| 5975 | } else { |
| 5976 | // Check whether VT is legal. |
| 5977 | unsigned NumElements = VT->getNumElements(); |
| 5978 | uint64_t Size = getContext().getTypeSize(VT); |
| 5979 | // NumElements should be power of 2. |
| 5980 | if (!llvm::isPowerOf2_32(NumElements)) |
| 5981 | return true; |
| 5982 | // Size should be greater than 32 bits. |
| 5983 | return Size <= 32; |
| 5984 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5985 | } |
| 5986 | return false; |
| 5987 | } |
| 5988 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5989 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 5990 | llvm::Type *eltTy, |
| 5991 | unsigned numElts) const { |
| 5992 | if (!llvm::isPowerOf2_32(numElts)) |
| 5993 | return false; |
| 5994 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 5995 | if (size > 64) |
| 5996 | return false; |
| 5997 | if (vectorSize.getQuantity() != 8 && |
| 5998 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 5999 | return false; |
| 6000 | return true; |
| 6001 | } |
| 6002 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 6003 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 6004 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 6005 | // double, or 64-bit or 128-bit vectors. |
| 6006 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 6007 | if (BT->getKind() == BuiltinType::Float || |
| 6008 | BT->getKind() == BuiltinType::Double || |
| 6009 | BT->getKind() == BuiltinType::LongDouble) |
| 6010 | return true; |
| 6011 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 6012 | unsigned VecSize = getContext().getTypeSize(VT); |
| 6013 | if (VecSize == 64 || VecSize == 128) |
| 6014 | return true; |
| 6015 | } |
| 6016 | return false; |
| 6017 | } |
| 6018 | |
| 6019 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 6020 | uint64_t Members) const { |
| 6021 | return Members <= 4; |
| 6022 | } |
| 6023 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6024 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6025 | QualType Ty) const { |
| 6026 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6027 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6028 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6029 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6030 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 6031 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 6032 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6033 | } |
| 6034 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6035 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 6036 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6037 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6038 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6039 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6040 | const Type *Base = nullptr; |
| 6041 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6042 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 6043 | IsIndirect = true; |
| 6044 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6045 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6046 | // allocated by the caller. |
| 6047 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 6048 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6049 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6050 | IsIndirect = true; |
| 6051 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6052 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6053 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6054 | // 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] | 6055 | // Our callers should be prepared to handle an under-aligned address. |
| 6056 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6057 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6058 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6059 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6060 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6061 | // ARMv7k allows type alignment up to 16 bytes. |
| 6062 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6063 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6064 | } else { |
| 6065 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6066 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6067 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6068 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6069 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6070 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6071 | } |
| 6072 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6073 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6074 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6075 | //===----------------------------------------------------------------------===// |
| 6076 | |
| 6077 | namespace { |
| 6078 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6079 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6080 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6081 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6082 | |
| 6083 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6084 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6085 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6086 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6087 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6088 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6089 | }; |
| 6090 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6091 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6092 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6093 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6094 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6095 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6096 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6097 | CodeGen::CodeGenModule &M, |
| 6098 | ForDefinition_t IsForDefinition) const override; |
| 6099 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6100 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6101 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6102 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6103 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6104 | }; |
| 6105 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6106 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6107 | if (RetTy->isVoidType()) |
| 6108 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6109 | |
| 6110 | // note: this is different from default ABI |
| 6111 | if (!RetTy->isScalarType()) |
| 6112 | return ABIArgInfo::getDirect(); |
| 6113 | |
| 6114 | // Treat an enum type as its underlying type. |
| 6115 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6116 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6117 | |
| 6118 | return (RetTy->isPromotableIntegerType() ? |
| 6119 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6120 | } |
| 6121 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6122 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6123 | // Treat an enum type as its underlying type. |
| 6124 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6125 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6126 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6127 | // Return aggregates type as indirect by value |
| 6128 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6129 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6130 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6131 | return (Ty->isPromotableIntegerType() ? |
| 6132 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6135 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6136 | if (!getCXXABI().classifyReturnType(FI)) |
| 6137 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6138 | for (auto &I : FI.arguments()) |
| 6139 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6140 | |
| 6141 | // Always honor user-specified calling convention. |
| 6142 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6143 | return; |
| 6144 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6145 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6146 | } |
| 6147 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6148 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6149 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6150 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6151 | } |
| 6152 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6153 | void NVPTXTargetCodeGenInfo::setTargetAttributes( |
| 6154 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 6155 | ForDefinition_t IsForDefinition) const { |
| 6156 | if (!IsForDefinition) |
| 6157 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6158 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6159 | if (!FD) return; |
| 6160 | |
| 6161 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6162 | |
| 6163 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6164 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6165 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6166 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6167 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6168 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6169 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6170 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6171 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6172 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6173 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6174 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6175 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6176 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6177 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6178 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6179 | // __global__ functions cannot be called from the device, we do not |
| 6180 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6181 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6182 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6183 | addNVVMMetadata(F, "kernel", 1); |
| 6184 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6185 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6186 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6187 | llvm::APSInt MaxThreads(32); |
| 6188 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6189 | if (MaxThreads > 0) |
| 6190 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6191 | |
| 6192 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6193 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6194 | // we don't have to add a PTX directive. |
| 6195 | if (Attr->getMinBlocks()) { |
| 6196 | llvm::APSInt MinBlocks(32); |
| 6197 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6198 | if (MinBlocks > 0) |
| 6199 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6200 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6201 | } |
| 6202 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6203 | } |
| 6204 | } |
| 6205 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6206 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6207 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6208 | llvm::Module *M = F->getParent(); |
| 6209 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6210 | |
| 6211 | // Get "nvvm.annotations" metadata node |
| 6212 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6213 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6214 | llvm::Metadata *MDVals[] = { |
| 6215 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6216 | llvm::ConstantAsMetadata::get( |
| 6217 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6218 | // Append metadata to nvvm.annotations |
| 6219 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6220 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6221 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6222 | |
| 6223 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6224 | // SystemZ ABI Implementation |
| 6225 | //===----------------------------------------------------------------------===// |
| 6226 | |
| 6227 | namespace { |
| 6228 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6229 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6230 | bool HasVector; |
| 6231 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6232 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6233 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6234 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6235 | |
| 6236 | bool isPromotableIntegerType(QualType Ty) const; |
| 6237 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6238 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6239 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6240 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6241 | |
| 6242 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6243 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6244 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6245 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6246 | if (!getCXXABI().classifyReturnType(FI)) |
| 6247 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6248 | for (auto &I : FI.arguments()) |
| 6249 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6250 | } |
| 6251 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6252 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6253 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6254 | |
| 6255 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 6256 | ArrayRef<llvm::Type*> scalars, |
| 6257 | bool asReturnValue) const override { |
| 6258 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6259 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6260 | bool isSwiftErrorInRegister() const override { |
| 6261 | return true; |
| 6262 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6263 | }; |
| 6264 | |
| 6265 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6266 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6267 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6268 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6269 | }; |
| 6270 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6271 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6272 | |
| 6273 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6274 | // Treat an enum type as its underlying type. |
| 6275 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6276 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6277 | |
| 6278 | // Promotable integer types are required to be promoted by the ABI. |
| 6279 | if (Ty->isPromotableIntegerType()) |
| 6280 | return true; |
| 6281 | |
| 6282 | // 32-bit values must also be promoted. |
| 6283 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6284 | switch (BT->getKind()) { |
| 6285 | case BuiltinType::Int: |
| 6286 | case BuiltinType::UInt: |
| 6287 | return true; |
| 6288 | default: |
| 6289 | return false; |
| 6290 | } |
| 6291 | return false; |
| 6292 | } |
| 6293 | |
| 6294 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6295 | return (Ty->isAnyComplexType() || |
| 6296 | Ty->isVectorType() || |
| 6297 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6298 | } |
| 6299 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6300 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6301 | return (HasVector && |
| 6302 | Ty->isVectorType() && |
| 6303 | getContext().getTypeSize(Ty) <= 128); |
| 6304 | } |
| 6305 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6306 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6307 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6308 | switch (BT->getKind()) { |
| 6309 | case BuiltinType::Float: |
| 6310 | case BuiltinType::Double: |
| 6311 | return true; |
| 6312 | default: |
| 6313 | return false; |
| 6314 | } |
| 6315 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6316 | return false; |
| 6317 | } |
| 6318 | |
| 6319 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6320 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6321 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6322 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6323 | |
| 6324 | // If this is a C++ record, check the bases first. |
| 6325 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6326 | for (const auto &I : CXXRD->bases()) { |
| 6327 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6328 | |
| 6329 | // Empty bases don't affect things either way. |
| 6330 | if (isEmptyRecord(getContext(), Base, true)) |
| 6331 | continue; |
| 6332 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6333 | if (!Found.isNull()) |
| 6334 | return Ty; |
| 6335 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6336 | } |
| 6337 | |
| 6338 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6339 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6340 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6341 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6342 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6343 | if (getContext().getLangOpts().CPlusPlus && |
| 6344 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 6345 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6346 | |
| 6347 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6348 | // Nested structures still do though. |
| 6349 | if (!Found.isNull()) |
| 6350 | return Ty; |
| 6351 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
| 6354 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6355 | // 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] | 6356 | if (!Found.isNull()) |
| 6357 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6358 | } |
| 6359 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6360 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6361 | } |
| 6362 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6363 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6364 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6365 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6366 | // struct { |
| 6367 | // i64 __gpr; |
| 6368 | // i64 __fpr; |
| 6369 | // i8 *__overflow_arg_area; |
| 6370 | // i8 *__reg_save_area; |
| 6371 | // }; |
| 6372 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6373 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6374 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6375 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6376 | Ty = getContext().getCanonicalType(Ty); |
| 6377 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6378 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6379 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6380 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6381 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6382 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6383 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6384 | CharUnits UnpaddedSize; |
| 6385 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6386 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6387 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6388 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6389 | } else { |
| 6390 | if (AI.getCoerceToType()) |
| 6391 | ArgTy = AI.getCoerceToType(); |
| 6392 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6393 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6394 | UnpaddedSize = TyInfo.first; |
| 6395 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6396 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6397 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6398 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6399 | PaddedSize = CharUnits::fromQuantity(16); |
| 6400 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6401 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6402 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6403 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6404 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6405 | llvm::Value *PaddedSizeV = |
| 6406 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6407 | |
| 6408 | if (IsVector) { |
| 6409 | // Work out the address of a vector argument on the stack. |
| 6410 | // Vector arguments are always passed in the high bits of a |
| 6411 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6412 | Address OverflowArgAreaPtr = |
| 6413 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6414 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6415 | Address OverflowArgArea = |
| 6416 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6417 | TyInfo.second); |
| 6418 | Address MemAddr = |
| 6419 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6420 | |
| 6421 | // Update overflow_arg_area_ptr pointer |
| 6422 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6423 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6424 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6425 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6426 | |
| 6427 | return MemAddr; |
| 6428 | } |
| 6429 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6430 | assert(PaddedSize.getQuantity() == 8); |
| 6431 | |
| 6432 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6433 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6434 | if (InFPRs) { |
| 6435 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6436 | RegCountField = 1; // __fpr |
| 6437 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6438 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6439 | } else { |
| 6440 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6441 | RegCountField = 0; // __gpr |
| 6442 | RegSaveIndex = 2; // save offset for r2 |
| 6443 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6444 | } |
| 6445 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6446 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 6447 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 6448 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6449 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6450 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6451 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6452 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6453 | |
| 6454 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6455 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6456 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6457 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6458 | |
| 6459 | // Emit code to load the value if it was passed in registers. |
| 6460 | CGF.EmitBlock(InRegBlock); |
| 6461 | |
| 6462 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6463 | llvm::Value *ScaledRegCount = |
| 6464 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6465 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6466 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6467 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6468 | llvm::Value *RegOffset = |
| 6469 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6470 | Address RegSaveAreaPtr = |
| 6471 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 6472 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6473 | llvm::Value *RegSaveArea = |
| 6474 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6475 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6476 | "raw_reg_addr"), |
| 6477 | PaddedSize); |
| 6478 | Address RegAddr = |
| 6479 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6480 | |
| 6481 | // Update the register count |
| 6482 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6483 | llvm::Value *NewRegCount = |
| 6484 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6485 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6486 | CGF.EmitBranch(ContBlock); |
| 6487 | |
| 6488 | // Emit code to load the value if it was passed in memory. |
| 6489 | CGF.EmitBlock(InMemBlock); |
| 6490 | |
| 6491 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6492 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 6493 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 6494 | Address OverflowArgArea = |
| 6495 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6496 | PaddedSize); |
| 6497 | Address RawMemAddr = |
| 6498 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6499 | Address MemAddr = |
| 6500 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6501 | |
| 6502 | // Update overflow_arg_area_ptr pointer |
| 6503 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6504 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6505 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6506 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6507 | CGF.EmitBranch(ContBlock); |
| 6508 | |
| 6509 | // Return the appropriate result. |
| 6510 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6511 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6512 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6513 | |
| 6514 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6515 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6516 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6517 | |
| 6518 | return ResAddr; |
| 6519 | } |
| 6520 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6521 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6522 | if (RetTy->isVoidType()) |
| 6523 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6524 | if (isVectorArgumentType(RetTy)) |
| 6525 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6526 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6527 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6528 | return (isPromotableIntegerType(RetTy) ? |
| 6529 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6530 | } |
| 6531 | |
| 6532 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6533 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6534 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6535 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6536 | |
| 6537 | // Integers and enums are extended to full register width. |
| 6538 | if (isPromotableIntegerType(Ty)) |
| 6539 | return ABIArgInfo::getExtend(); |
| 6540 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6541 | // Handle vector types and vector-like structure types. Note that |
| 6542 | // as opposed to float-like structure types, we do not allow any |
| 6543 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6544 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6545 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6546 | if (isVectorArgumentType(SingleElementTy) && |
| 6547 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6548 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6549 | |
| 6550 | // 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] | 6551 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6552 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6553 | |
| 6554 | // Handle small structures. |
| 6555 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6556 | // Structures with flexible arrays have variable length, so really |
| 6557 | // fail the size test above. |
| 6558 | const RecordDecl *RD = RT->getDecl(); |
| 6559 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6560 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6561 | |
| 6562 | // The structure is passed as an unextended integer, a float, or a double. |
| 6563 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6564 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6565 | assert(Size == 32 || Size == 64); |
| 6566 | if (Size == 32) |
| 6567 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6568 | else |
| 6569 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6570 | } else |
| 6571 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6572 | return ABIArgInfo::getDirect(PassTy); |
| 6573 | } |
| 6574 | |
| 6575 | // Non-structure compounds are passed indirectly. |
| 6576 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6577 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6578 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6579 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6580 | } |
| 6581 | |
| 6582 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6583 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6584 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6585 | |
| 6586 | namespace { |
| 6587 | |
| 6588 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6589 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6590 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6591 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6592 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6593 | CodeGen::CodeGenModule &M, |
| 6594 | ForDefinition_t IsForDefinition) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6595 | }; |
| 6596 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6597 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6598 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6599 | void MSP430TargetCodeGenInfo::setTargetAttributes( |
| 6600 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 6601 | ForDefinition_t IsForDefinition) const { |
| 6602 | if (!IsForDefinition) |
| 6603 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6604 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6605 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 6606 | // Handle 'interrupt' attribute: |
| 6607 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6608 | |
| 6609 | // Step 1: Set ISR calling convention. |
| 6610 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 6611 | |
| 6612 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6613 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6614 | |
| 6615 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 6616 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 6617 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 6618 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6619 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6620 | } |
| 6621 | } |
| 6622 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6623 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6624 | // MIPS ABI Implementation. This works for both little-endian and |
| 6625 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6626 | //===----------------------------------------------------------------------===// |
| 6627 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6628 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6629 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6630 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6631 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6632 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6633 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6634 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6635 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6636 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6637 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6638 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6639 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6640 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6641 | |
| 6642 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6643 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6644 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6645 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6646 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6647 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6648 | }; |
| 6649 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6650 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6651 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6652 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6653 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6654 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6655 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6656 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6657 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6658 | return 29; |
| 6659 | } |
| 6660 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6661 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6662 | CodeGen::CodeGenModule &CGM, |
| 6663 | ForDefinition_t IsForDefinition) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6664 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6665 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6666 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6667 | |
| 6668 | if (FD->hasAttr<MipsLongCallAttr>()) |
| 6669 | Fn->addFnAttr("long-call"); |
| 6670 | else if (FD->hasAttr<MipsShortCallAttr>()) |
| 6671 | Fn->addFnAttr("short-call"); |
| 6672 | |
| 6673 | // Other attributes do not have a meaning for declarations. |
| 6674 | if (!IsForDefinition) |
| 6675 | return; |
| 6676 | |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6677 | if (FD->hasAttr<Mips16Attr>()) { |
| 6678 | Fn->addFnAttr("mips16"); |
| 6679 | } |
| 6680 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6681 | Fn->addFnAttr("nomips16"); |
| 6682 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6683 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6684 | if (FD->hasAttr<MicroMipsAttr>()) |
| 6685 | Fn->addFnAttr("micromips"); |
| 6686 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 6687 | Fn->addFnAttr("nomicromips"); |
| 6688 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6689 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6690 | if (!Attr) |
| 6691 | return; |
| 6692 | |
| 6693 | const char *Kind; |
| 6694 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6695 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6696 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6697 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6698 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6699 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6700 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6701 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6702 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6703 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6704 | } |
| 6705 | |
| 6706 | Fn->addFnAttr("interrupt", Kind); |
| 6707 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6708 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6709 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6710 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6711 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6712 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6713 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6714 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6715 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6716 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6717 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6718 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6719 | void MipsABIInfo::CoerceToIntArgs( |
| 6720 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6721 | llvm::IntegerType *IntTy = |
| 6722 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6723 | |
| 6724 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6725 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6726 | ArgList.push_back(IntTy); |
| 6727 | |
| 6728 | // If necessary, add one more integer type to ArgList. |
| 6729 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6730 | |
| 6731 | if (R) |
| 6732 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6733 | } |
| 6734 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6735 | // In N32/64, an aligned double precision floating point field is passed in |
| 6736 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6737 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6738 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6739 | |
| 6740 | if (IsO32) { |
| 6741 | CoerceToIntArgs(TySize, ArgList); |
| 6742 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6743 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6744 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6745 | if (Ty->isComplexType()) |
| 6746 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6747 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6748 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6749 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6750 | // Unions/vectors are passed in integer registers. |
| 6751 | if (!RT || !RT->isStructureOrClassType()) { |
| 6752 | CoerceToIntArgs(TySize, ArgList); |
| 6753 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6754 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6755 | |
| 6756 | const RecordDecl *RD = RT->getDecl(); |
| 6757 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6758 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6759 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6760 | uint64_t LastOffset = 0; |
| 6761 | unsigned idx = 0; |
| 6762 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6763 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6764 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6765 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6766 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6767 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6768 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6769 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6770 | |
| 6771 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6772 | continue; |
| 6773 | |
| 6774 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6775 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6776 | continue; |
| 6777 | |
| 6778 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6779 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6780 | ArgList.push_back(I64); |
| 6781 | |
| 6782 | // Add double type. |
| 6783 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6784 | LastOffset = Offset + 64; |
| 6785 | } |
| 6786 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6787 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6788 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6789 | |
| 6790 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6791 | } |
| 6792 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6793 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6794 | uint64_t Offset) const { |
| 6795 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6796 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6797 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6798 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6799 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6800 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6801 | ABIArgInfo |
| 6802 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6803 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6804 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6805 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6806 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6807 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6808 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6809 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6810 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6811 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6812 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6813 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6814 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6815 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6816 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6817 | return ABIArgInfo::getIgnore(); |
| 6818 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6819 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6820 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6821 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6822 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6823 | |
Petar Jovanovic | 6f4cdb8 | 2017-05-10 14:28:18 +0000 | [diff] [blame] | 6824 | // Use indirect if the aggregate cannot fit into registers for |
| 6825 | // passing arguments according to the ABI |
| 6826 | unsigned Threshold = IsO32 ? 16 : 64; |
| 6827 | |
| 6828 | if(getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(Threshold)) |
| 6829 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align), true, |
| 6830 | getContext().getTypeAlign(Ty) / 8 > Align); |
| 6831 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6832 | // If we have reached here, aggregates are passed directly by coercing to |
| 6833 | // another structure type. Padding is inserted if the offset of the |
| 6834 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6835 | ABIArgInfo ArgInfo = |
| 6836 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6837 | getPaddingType(OrigOffset, CurrOffset)); |
| 6838 | ArgInfo.setInReg(true); |
| 6839 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6840 | } |
| 6841 | |
| 6842 | // Treat an enum type as its underlying type. |
| 6843 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6844 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6845 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6846 | // All integral types are promoted to the GPR width. |
| 6847 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6848 | return ABIArgInfo::getExtend(); |
| 6849 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6850 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6851 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6852 | } |
| 6853 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6854 | llvm::Type* |
| 6855 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6856 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6857 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6858 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6859 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6860 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6861 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6862 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6863 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6864 | // N32/64 returns struct/classes in floating point registers if the |
| 6865 | // following conditions are met: |
| 6866 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6867 | // 2. The struct/class has one or two fields all of which are floating |
| 6868 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6869 | // 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] | 6870 | // |
| 6871 | // Any other composite results are returned in integer registers. |
| 6872 | // |
| 6873 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6874 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6875 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6876 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6877 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6878 | if (!BT || !BT->isFloatingPoint()) |
| 6879 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6880 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6881 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6882 | } |
| 6883 | |
| 6884 | if (b == e) |
| 6885 | return llvm::StructType::get(getVMContext(), RTList, |
| 6886 | RD->hasAttr<PackedAttr>()); |
| 6887 | |
| 6888 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6889 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6890 | } |
| 6891 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6892 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6893 | return llvm::StructType::get(getVMContext(), RTList); |
| 6894 | } |
| 6895 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6896 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6897 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6898 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6899 | if (RetTy->isVoidType()) |
| 6900 | return ABIArgInfo::getIgnore(); |
| 6901 | |
| 6902 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6903 | // However, N32/N64 ignores zero sized return values. |
| 6904 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6905 | return ABIArgInfo::getIgnore(); |
| 6906 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6907 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6908 | if (Size <= 128) { |
| 6909 | if (RetTy->isAnyComplexType()) |
| 6910 | return ABIArgInfo::getDirect(); |
| 6911 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6912 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6913 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6914 | if (!IsO32 || |
| 6915 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6916 | ABIArgInfo ArgInfo = |
| 6917 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6918 | ArgInfo.setInReg(true); |
| 6919 | return ArgInfo; |
| 6920 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6921 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6922 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6923 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6924 | } |
| 6925 | |
| 6926 | // Treat an enum type as its underlying type. |
| 6927 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6928 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6929 | |
| 6930 | return (RetTy->isPromotableIntegerType() ? |
| 6931 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6932 | } |
| 6933 | |
| 6934 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6935 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6936 | if (!getCXXABI().classifyReturnType(FI)) |
| 6937 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6938 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6939 | // 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] | 6940 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6941 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6942 | for (auto &I : FI.arguments()) |
| 6943 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6946 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6947 | QualType OrigTy) const { |
| 6948 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6949 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6950 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6951 | // 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] | 6952 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6953 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6954 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6955 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6956 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6957 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6958 | DidPromote = true; |
| 6959 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6960 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6961 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6962 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6963 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6964 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6965 | // The alignment of things in the argument area is never larger than |
| 6966 | // StackAlignInBytes. |
| 6967 | TyInfo.second = |
| 6968 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6969 | |
| 6970 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6971 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6972 | |
| 6973 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6974 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6975 | |
| 6976 | |
| 6977 | // If there was a promotion, "unpromote" into a temporary. |
| 6978 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6979 | if (DidPromote) { |
| 6980 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6981 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6982 | |
| 6983 | // Truncate down to the right width. |
| 6984 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6985 | : CGF.IntPtrTy); |
| 6986 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6987 | if (OrigTy->isPointerType()) |
| 6988 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6989 | |
| 6990 | CGF.Builder.CreateStore(V, Temp); |
| 6991 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6992 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6993 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6994 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6997 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6998 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6999 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7000 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 7001 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 7002 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7003 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7004 | return false; |
| 7005 | } |
| 7006 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7007 | bool |
| 7008 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7009 | llvm::Value *Address) const { |
| 7010 | // This information comes from gcc's implementation, which seems to |
| 7011 | // as canonical as it gets. |
| 7012 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7013 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 7014 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7015 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7016 | |
| 7017 | // 0-31 are the general purpose registers, $0 - $31. |
| 7018 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 7019 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 7020 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7021 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7022 | |
| 7023 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 7024 | // They are one bit wide and ignored here. |
| 7025 | |
| 7026 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 7027 | // (coprocessor 1 is the FP unit) |
| 7028 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 7029 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 7030 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7031 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7032 | return false; |
| 7033 | } |
| 7034 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7035 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7036 | // AVR ABI Implementation. |
| 7037 | //===----------------------------------------------------------------------===// |
| 7038 | |
| 7039 | namespace { |
| 7040 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7041 | public: |
| 7042 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7043 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 7044 | |
| 7045 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7046 | CodeGen::CodeGenModule &CGM, |
| 7047 | ForDefinition_t IsForDefinition) const override { |
| 7048 | if (!IsForDefinition) |
| 7049 | return; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7050 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 7051 | if (!FD) return; |
| 7052 | auto *Fn = cast<llvm::Function>(GV); |
| 7053 | |
| 7054 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7055 | Fn->addFnAttr("interrupt"); |
| 7056 | |
| 7057 | if (FD->getAttr<AVRSignalAttr>()) |
| 7058 | Fn->addFnAttr("signal"); |
| 7059 | } |
| 7060 | }; |
| 7061 | } |
| 7062 | |
| 7063 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7064 | // 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] | 7065 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7066 | // handling. |
| 7067 | //===----------------------------------------------------------------------===// |
| 7068 | |
| 7069 | namespace { |
| 7070 | |
| 7071 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7072 | public: |
| 7073 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7074 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7075 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7076 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7077 | CodeGen::CodeGenModule &M, |
| 7078 | ForDefinition_t IsForDefinition) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7079 | }; |
| 7080 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7081 | void TCETargetCodeGenInfo::setTargetAttributes( |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7082 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 7083 | ForDefinition_t IsForDefinition) const { |
| 7084 | if (!IsForDefinition) |
| 7085 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7086 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7087 | if (!FD) return; |
| 7088 | |
| 7089 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7090 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7091 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7092 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7093 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7094 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7095 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7096 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7097 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7098 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7099 | llvm::NamedMDNode *OpenCLMetadata = |
| 7100 | M.getModule().getOrInsertNamedMetadata( |
| 7101 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7102 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7103 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7104 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7105 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7106 | Operands.push_back( |
| 7107 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7108 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7109 | Operands.push_back( |
| 7110 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7111 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7112 | Operands.push_back( |
| 7113 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7114 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7115 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7116 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7117 | // (false) for implementing the work_group_size_hint attr later. |
| 7118 | // 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] | 7119 | Operands.push_back( |
| 7120 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7121 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7122 | } |
| 7123 | } |
| 7124 | } |
| 7125 | } |
| 7126 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7127 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7128 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7129 | //===----------------------------------------------------------------------===// |
| 7130 | // Hexagon ABI Implementation |
| 7131 | //===----------------------------------------------------------------------===// |
| 7132 | |
| 7133 | namespace { |
| 7134 | |
| 7135 | class HexagonABIInfo : public ABIInfo { |
| 7136 | |
| 7137 | |
| 7138 | public: |
| 7139 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7140 | |
| 7141 | private: |
| 7142 | |
| 7143 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7144 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7145 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7146 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7147 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7148 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7149 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7150 | }; |
| 7151 | |
| 7152 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7153 | public: |
| 7154 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7155 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7156 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7157 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7158 | return 29; |
| 7159 | } |
| 7160 | }; |
| 7161 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7162 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7163 | |
| 7164 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7165 | if (!getCXXABI().classifyReturnType(FI)) |
| 7166 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7167 | for (auto &I : FI.arguments()) |
| 7168 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7169 | } |
| 7170 | |
| 7171 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7172 | if (!isAggregateTypeForABI(Ty)) { |
| 7173 | // Treat an enum type as its underlying type. |
| 7174 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7175 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7176 | |
| 7177 | return (Ty->isPromotableIntegerType() ? |
| 7178 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 7179 | } |
| 7180 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7181 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7182 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7183 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7184 | // Ignore empty records. |
| 7185 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7186 | return ABIArgInfo::getIgnore(); |
| 7187 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7188 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7189 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7190 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7191 | // Pass in the smallest viable integer type. |
| 7192 | else if (Size > 32) |
| 7193 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7194 | else if (Size > 16) |
| 7195 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7196 | else if (Size > 8) |
| 7197 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7198 | else |
| 7199 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7200 | } |
| 7201 | |
| 7202 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7203 | if (RetTy->isVoidType()) |
| 7204 | return ABIArgInfo::getIgnore(); |
| 7205 | |
| 7206 | // Large vector types should be returned via memory. |
| 7207 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7208 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7209 | |
| 7210 | if (!isAggregateTypeForABI(RetTy)) { |
| 7211 | // Treat an enum type as its underlying type. |
| 7212 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7213 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7214 | |
| 7215 | return (RetTy->isPromotableIntegerType() ? |
| 7216 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 7217 | } |
| 7218 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7219 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7220 | return ABIArgInfo::getIgnore(); |
| 7221 | |
| 7222 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7223 | // are returned indirectly. |
| 7224 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7225 | if (Size <= 64) { |
| 7226 | // Return in the smallest viable integer type. |
| 7227 | if (Size <= 8) |
| 7228 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7229 | if (Size <= 16) |
| 7230 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7231 | if (Size <= 32) |
| 7232 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7233 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7234 | } |
| 7235 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7236 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7237 | } |
| 7238 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7239 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7240 | QualType Ty) const { |
| 7241 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7242 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7243 | getContext().getTypeInfoInChars(Ty), |
| 7244 | CharUnits::fromQuantity(4), |
| 7245 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7246 | } |
| 7247 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7248 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7249 | // Lanai ABI Implementation |
| 7250 | //===----------------------------------------------------------------------===// |
| 7251 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7252 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7253 | class LanaiABIInfo : public DefaultABIInfo { |
| 7254 | public: |
| 7255 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7256 | |
| 7257 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7258 | |
| 7259 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7260 | CCState State(FI.getCallingConvention()); |
| 7261 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7262 | // regparm attribute set. |
| 7263 | if (FI.getHasRegParm()) { |
| 7264 | State.FreeRegs = FI.getRegParm(); |
| 7265 | } else { |
| 7266 | State.FreeRegs = 4; |
| 7267 | } |
| 7268 | |
| 7269 | if (!getCXXABI().classifyReturnType(FI)) |
| 7270 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7271 | for (auto &I : FI.arguments()) |
| 7272 | I.info = classifyArgumentType(I.type, State); |
| 7273 | } |
| 7274 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7275 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7276 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7277 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7278 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7279 | |
| 7280 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7281 | unsigned Size = getContext().getTypeSize(Ty); |
| 7282 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7283 | |
| 7284 | if (SizeInRegs == 0) |
| 7285 | return false; |
| 7286 | |
| 7287 | if (SizeInRegs > State.FreeRegs) { |
| 7288 | State.FreeRegs = 0; |
| 7289 | return false; |
| 7290 | } |
| 7291 | |
| 7292 | State.FreeRegs -= SizeInRegs; |
| 7293 | |
| 7294 | return true; |
| 7295 | } |
| 7296 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7297 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7298 | CCState &State) const { |
| 7299 | if (!ByVal) { |
| 7300 | if (State.FreeRegs) { |
| 7301 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7302 | return getNaturalAlignIndirectInReg(Ty); |
| 7303 | } |
| 7304 | return getNaturalAlignIndirect(Ty, false); |
| 7305 | } |
| 7306 | |
| 7307 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7308 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7309 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7310 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7311 | /*Realign=*/TypeAlign > |
| 7312 | MinABIStackAlignInBytes); |
| 7313 | } |
| 7314 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7315 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7316 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7317 | // Check with the C++ ABI first. |
| 7318 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7319 | if (RT) { |
| 7320 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7321 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7322 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7323 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7324 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7325 | } |
| 7326 | } |
| 7327 | |
| 7328 | if (isAggregateTypeForABI(Ty)) { |
| 7329 | // Structures with flexible arrays are always indirect. |
| 7330 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7331 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7332 | |
| 7333 | // Ignore empty structs/unions. |
| 7334 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7335 | return ABIArgInfo::getIgnore(); |
| 7336 | |
| 7337 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7338 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7339 | if (SizeInRegs <= State.FreeRegs) { |
| 7340 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7341 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7342 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7343 | State.FreeRegs -= SizeInRegs; |
| 7344 | return ABIArgInfo::getDirectInReg(Result); |
| 7345 | } else { |
| 7346 | State.FreeRegs = 0; |
| 7347 | } |
| 7348 | return getIndirectResult(Ty, true, State); |
| 7349 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7350 | |
| 7351 | // Treat an enum type as its underlying type. |
| 7352 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7353 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7354 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7355 | bool InReg = shouldUseInReg(Ty, State); |
| 7356 | if (Ty->isPromotableIntegerType()) { |
| 7357 | if (InReg) |
| 7358 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7359 | return ABIArgInfo::getExtend(); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7360 | } |
| 7361 | if (InReg) |
| 7362 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7363 | return ABIArgInfo::getDirect(); |
| 7364 | } |
| 7365 | |
| 7366 | namespace { |
| 7367 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7368 | public: |
| 7369 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7370 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7371 | }; |
| 7372 | } |
| 7373 | |
| 7374 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7375 | // AMDGPU ABI Implementation |
| 7376 | //===----------------------------------------------------------------------===// |
| 7377 | |
| 7378 | namespace { |
| 7379 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7380 | class AMDGPUABIInfo final : public DefaultABIInfo { |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7381 | private: |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame^] | 7382 | static const unsigned MaxNumRegsForArgsRet = 16; |
| 7383 | |
| 7384 | bool shouldReturnTypeInRegister(QualType Ty, |
| 7385 | ASTContext &Context) const; |
| 7386 | unsigned numRegsForType(QualType Ty) const; |
| 7387 | |
| 7388 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 7389 | bool isHomogeneousAggregateSmallEnough(const Type *Base, |
| 7390 | uint64_t Members) const override; |
| 7391 | |
| 7392 | public: |
| 7393 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : |
| 7394 | DefaultABIInfo(CGT) {} |
| 7395 | |
| 7396 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7397 | ABIArgInfo classifyKernelArgumentType(QualType Ty) const; |
| 7398 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7399 | |
| 7400 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7401 | }; |
| 7402 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame^] | 7403 | bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 7404 | return true; |
| 7405 | } |
| 7406 | |
| 7407 | bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( |
| 7408 | const Type *Base, uint64_t Members) const { |
| 7409 | uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; |
| 7410 | |
| 7411 | // Homogeneous Aggregates may occupy at most 16 registers. |
| 7412 | return Members * NumRegs <= MaxNumRegsForArgsRet; |
| 7413 | } |
| 7414 | |
| 7415 | /// Check whether the type is small enough to consider passing directly in |
| 7416 | /// registers. |
| 7417 | bool AMDGPUABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 7418 | ASTContext &Ctx) const { |
| 7419 | return ((Ctx.getTypeSize(Ty) + 31) / 32) <= MaxNumRegsForArgsRet; |
| 7420 | } |
| 7421 | |
| 7422 | /// Estimate number of registers the type will use when passed in registers. |
| 7423 | unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { |
| 7424 | unsigned NumRegs = 0; |
| 7425 | |
| 7426 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 7427 | // Compute from the number of elements. The reported size is based on the |
| 7428 | // in-memory size, which includes the padding 4th element for 3-vectors. |
| 7429 | QualType EltTy = VT->getElementType(); |
| 7430 | unsigned EltSize = getContext().getTypeSize(EltTy); |
| 7431 | |
| 7432 | // 16-bit element vectors should be passed as packed. |
| 7433 | if (EltSize == 16) |
| 7434 | return (VT->getNumElements() + 1) / 2; |
| 7435 | |
| 7436 | unsigned EltNumRegs = (EltSize + 31) / 32; |
| 7437 | return EltNumRegs * VT->getNumElements(); |
| 7438 | } |
| 7439 | |
| 7440 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7441 | const RecordDecl *RD = RT->getDecl(); |
| 7442 | assert(!RD->hasFlexibleArrayMember()); |
| 7443 | |
| 7444 | for (const FieldDecl *Field : RD->fields()) { |
| 7445 | QualType FieldTy = Field->getType(); |
| 7446 | NumRegs += numRegsForType(FieldTy); |
| 7447 | } |
| 7448 | |
| 7449 | return NumRegs; |
| 7450 | } |
| 7451 | |
| 7452 | return (getContext().getTypeSize(Ty) + 31) / 32; |
| 7453 | } |
| 7454 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7455 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame^] | 7456 | llvm::CallingConv::ID CC = FI.getCallingConvention(); |
| 7457 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7458 | if (!getCXXABI().classifyReturnType(FI)) |
| 7459 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7460 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame^] | 7461 | unsigned NumRegsLeft = MaxNumRegsForArgsRet; |
| 7462 | for (auto &Arg : FI.arguments()) { |
| 7463 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) { |
| 7464 | Arg.info = classifyKernelArgumentType(Arg.type); |
| 7465 | } else { |
| 7466 | Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); |
| 7467 | } |
| 7468 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7469 | } |
| 7470 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame^] | 7471 | ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { |
| 7472 | if (isAggregateTypeForABI(RetTy)) { |
| 7473 | // Records with non-trivial destructors/copy-constructors should not be |
| 7474 | // returned by value. |
| 7475 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 7476 | // Ignore empty structs/unions. |
| 7477 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7478 | return ABIArgInfo::getIgnore(); |
| 7479 | |
| 7480 | // Lower single-element structs to just return a regular value. |
| 7481 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 7482 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7483 | |
| 7484 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
| 7485 | const RecordDecl *RD = RT->getDecl(); |
| 7486 | if (RD->hasFlexibleArrayMember()) |
| 7487 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7488 | } |
| 7489 | |
| 7490 | // Pack aggregates <= 4 bytes into single VGPR or pair. |
| 7491 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7492 | if (Size <= 16) |
| 7493 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7494 | |
| 7495 | if (Size <= 32) |
| 7496 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7497 | |
| 7498 | if (Size <= 64) { |
| 7499 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7500 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7501 | } |
| 7502 | |
| 7503 | if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) |
| 7504 | return ABIArgInfo::getDirect(); |
| 7505 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7506 | } |
| 7507 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame^] | 7508 | // Otherwise just do the default thing. |
| 7509 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7510 | } |
| 7511 | |
| 7512 | /// For kernels all parameters are really passed in a special buffer. It doesn't |
| 7513 | /// make sense to pass anything byval, so everything must be direct. |
| 7514 | ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { |
| 7515 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7516 | |
| 7517 | // TODO: Can we omit empty structs? |
| 7518 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7519 | // Coerce single element structs to its element. |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame^] | 7520 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7521 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7522 | |
| 7523 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7524 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7525 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 7526 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 7527 | } |
| 7528 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame^] | 7529 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, |
| 7530 | unsigned &NumRegsLeft) const { |
| 7531 | assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); |
| 7532 | |
| 7533 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7534 | |
| 7535 | if (isAggregateTypeForABI(Ty)) { |
| 7536 | // Records with non-trivial destructors/copy-constructors should not be |
| 7537 | // passed by value. |
| 7538 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7539 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7540 | |
| 7541 | // Ignore empty structs/unions. |
| 7542 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7543 | return ABIArgInfo::getIgnore(); |
| 7544 | |
| 7545 | // Lower single-element structs to just pass a regular value. TODO: We |
| 7546 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 7547 | // though watch out for things like bitfields. |
| 7548 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7549 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7550 | |
| 7551 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7552 | const RecordDecl *RD = RT->getDecl(); |
| 7553 | if (RD->hasFlexibleArrayMember()) |
| 7554 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7555 | } |
| 7556 | |
| 7557 | // Pack aggregates <= 8 bytes into single VGPR or pair. |
| 7558 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7559 | if (Size <= 64) { |
| 7560 | unsigned NumRegs = (Size + 31) / 32; |
| 7561 | NumRegsLeft -= std::min(NumRegsLeft, NumRegs); |
| 7562 | |
| 7563 | if (Size <= 16) |
| 7564 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7565 | |
| 7566 | if (Size <= 32) |
| 7567 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7568 | |
| 7569 | // XXX: Should this be i64 instead, and should the limit increase? |
| 7570 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7571 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7572 | } |
| 7573 | |
| 7574 | if (NumRegsLeft > 0) { |
| 7575 | unsigned NumRegs = numRegsForType(Ty); |
| 7576 | if (NumRegsLeft >= NumRegs) { |
| 7577 | NumRegsLeft -= NumRegs; |
| 7578 | return ABIArgInfo::getDirect(); |
| 7579 | } |
| 7580 | } |
| 7581 | } |
| 7582 | |
| 7583 | // Otherwise just do the default thing. |
| 7584 | ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); |
| 7585 | if (!ArgInfo.isIndirect()) { |
| 7586 | unsigned NumRegs = numRegsForType(Ty); |
| 7587 | NumRegsLeft -= std::min(NumRegs, NumRegsLeft); |
| 7588 | } |
| 7589 | |
| 7590 | return ArgInfo; |
| 7591 | } |
| 7592 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7593 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7594 | public: |
| 7595 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7596 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7597 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7598 | CodeGen::CodeGenModule &M, |
| 7599 | ForDefinition_t IsForDefinition) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7600 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7601 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7602 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7603 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7604 | |
| 7605 | unsigned getASTAllocaAddressSpace() const override { |
| 7606 | return LangAS::FirstTargetAddressSpace + |
| 7607 | getABIInfo().getDataLayout().getAllocaAddrSpace(); |
| 7608 | } |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7609 | unsigned getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7610 | const VarDecl *D) const override; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7611 | llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S, |
| 7612 | llvm::LLVMContext &C) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7613 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7614 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7615 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7616 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7617 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 7618 | ForDefinition_t IsForDefinition) const { |
| 7619 | if (!IsForDefinition) |
| 7620 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7621 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7622 | if (!FD) |
| 7623 | return; |
| 7624 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7625 | llvm::Function *F = cast<llvm::Function>(GV); |
| 7626 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7627 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 7628 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
| 7629 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 7630 | if (ReqdWGS || FlatWGS) { |
| 7631 | unsigned Min = FlatWGS ? FlatWGS->getMin() : 0; |
| 7632 | unsigned Max = FlatWGS ? FlatWGS->getMax() : 0; |
| 7633 | if (ReqdWGS && Min == 0 && Max == 0) |
| 7634 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7635 | |
| 7636 | if (Min != 0) { |
| 7637 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 7638 | |
| 7639 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 7640 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 7641 | } else |
| 7642 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7643 | } |
| 7644 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7645 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 7646 | unsigned Min = Attr->getMin(); |
| 7647 | unsigned Max = Attr->getMax(); |
| 7648 | |
| 7649 | if (Min != 0) { |
| 7650 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 7651 | |
| 7652 | std::string AttrVal = llvm::utostr(Min); |
| 7653 | if (Max != 0) |
| 7654 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 7655 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 7656 | } else |
| 7657 | assert(Max == 0 && "Max must be zero"); |
| 7658 | } |
| 7659 | |
| 7660 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7661 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7662 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7663 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7664 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 7665 | } |
| 7666 | |
| 7667 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7668 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 7669 | |
| 7670 | if (NumVGPR != 0) |
| 7671 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7672 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7673 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7674 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7675 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7676 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7677 | } |
| 7678 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7679 | // Currently LLVM assumes null pointers always have value 0, |
| 7680 | // which results in incorrectly transformed IR. Therefore, instead of |
| 7681 | // emitting null pointers in private and local address spaces, a null |
| 7682 | // pointer in generic address space is emitted which is casted to a |
| 7683 | // pointer in local or private address space. |
| 7684 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 7685 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 7686 | QualType QT) const { |
| 7687 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 7688 | return llvm::ConstantPointerNull::get(PT); |
| 7689 | |
| 7690 | auto &Ctx = CGM.getContext(); |
| 7691 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 7692 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 7693 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 7694 | llvm::ConstantPointerNull::get(NPT), PT); |
| 7695 | } |
| 7696 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7697 | unsigned |
| 7698 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7699 | const VarDecl *D) const { |
| 7700 | assert(!CGM.getLangOpts().OpenCL && |
| 7701 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 7702 | "Address space agnostic languages only"); |
| 7703 | unsigned DefaultGlobalAS = |
| 7704 | LangAS::FirstTargetAddressSpace + |
| 7705 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global); |
| 7706 | if (!D) |
| 7707 | return DefaultGlobalAS; |
| 7708 | |
| 7709 | unsigned AddrSpace = D->getType().getAddressSpace(); |
| 7710 | assert(AddrSpace == LangAS::Default || |
| 7711 | AddrSpace >= LangAS::FirstTargetAddressSpace); |
| 7712 | if (AddrSpace != LangAS::Default) |
| 7713 | return AddrSpace; |
| 7714 | |
| 7715 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 7716 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 7717 | return ConstAS.getValue(); |
| 7718 | } |
| 7719 | return DefaultGlobalAS; |
| 7720 | } |
| 7721 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7722 | llvm::SyncScope::ID |
| 7723 | AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, |
| 7724 | llvm::LLVMContext &C) const { |
| 7725 | StringRef Name; |
| 7726 | switch (S) { |
| 7727 | case SyncScope::OpenCLWorkGroup: |
| 7728 | Name = "workgroup"; |
| 7729 | break; |
| 7730 | case SyncScope::OpenCLDevice: |
| 7731 | Name = "agent"; |
| 7732 | break; |
| 7733 | case SyncScope::OpenCLAllSVMDevices: |
| 7734 | Name = ""; |
| 7735 | break; |
| 7736 | case SyncScope::OpenCLSubGroup: |
| 7737 | Name = "subgroup"; |
| 7738 | } |
| 7739 | return C.getOrInsertSyncScopeID(Name); |
| 7740 | } |
| 7741 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7742 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 7743 | // SPARC v8 ABI Implementation. |
| 7744 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7745 | // |
| 7746 | // Ensures that complex values are passed in registers. |
| 7747 | // |
| 7748 | namespace { |
| 7749 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 7750 | public: |
| 7751 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7752 | |
| 7753 | private: |
| 7754 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7755 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7756 | }; |
| 7757 | } // end anonymous namespace |
| 7758 | |
| 7759 | |
| 7760 | ABIArgInfo |
| 7761 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 7762 | if (Ty->isAnyComplexType()) { |
| 7763 | return ABIArgInfo::getDirect(); |
| 7764 | } |
| 7765 | else { |
| 7766 | return DefaultABIInfo::classifyReturnType(Ty); |
| 7767 | } |
| 7768 | } |
| 7769 | |
| 7770 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7771 | |
| 7772 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7773 | for (auto &Arg : FI.arguments()) |
| 7774 | Arg.info = classifyArgumentType(Arg.type); |
| 7775 | } |
| 7776 | |
| 7777 | namespace { |
| 7778 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7779 | public: |
| 7780 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7781 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 7782 | }; |
| 7783 | } // end anonymous namespace |
| 7784 | |
| 7785 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7786 | // SPARC v9 ABI Implementation. |
| 7787 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7788 | // |
| 7789 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 7790 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 7791 | // the array, structs larger than 16 bytes are passed indirectly. |
| 7792 | // |
| 7793 | // One case requires special care: |
| 7794 | // |
| 7795 | // struct mixed { |
| 7796 | // int i; |
| 7797 | // float f; |
| 7798 | // }; |
| 7799 | // |
| 7800 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 7801 | // parameter array, but the int is passed in an integer register, and the float |
| 7802 | // is passed in a floating point register. This is represented as two arguments |
| 7803 | // with the LLVM IR inreg attribute: |
| 7804 | // |
| 7805 | // declare void f(i32 inreg %i, float inreg %f) |
| 7806 | // |
| 7807 | // The code generator will only allocate 4 bytes from the parameter array for |
| 7808 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 7809 | // bytes. |
| 7810 | // |
| 7811 | namespace { |
| 7812 | class SparcV9ABIInfo : public ABIInfo { |
| 7813 | public: |
| 7814 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7815 | |
| 7816 | private: |
| 7817 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7818 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7819 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7820 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7821 | |
| 7822 | // Coercion type builder for structs passed in registers. The coercion type |
| 7823 | // serves two purposes: |
| 7824 | // |
| 7825 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 7826 | // in registers. |
| 7827 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 7828 | // code generator knows to pass them in floating point registers. |
| 7829 | // |
| 7830 | // We also compute the InReg flag which indicates that the struct contains |
| 7831 | // aligned 32-bit floats. |
| 7832 | // |
| 7833 | struct CoerceBuilder { |
| 7834 | llvm::LLVMContext &Context; |
| 7835 | const llvm::DataLayout &DL; |
| 7836 | SmallVector<llvm::Type*, 8> Elems; |
| 7837 | uint64_t Size; |
| 7838 | bool InReg; |
| 7839 | |
| 7840 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 7841 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 7842 | |
| 7843 | // Pad Elems with integers until Size is ToSize. |
| 7844 | void pad(uint64_t ToSize) { |
| 7845 | assert(ToSize >= Size && "Cannot remove elements"); |
| 7846 | if (ToSize == Size) |
| 7847 | return; |
| 7848 | |
| 7849 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7850 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7851 | if (Aligned > Size && Aligned <= ToSize) { |
| 7852 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 7853 | Size = Aligned; |
| 7854 | } |
| 7855 | |
| 7856 | // Add whole 64-bit words. |
| 7857 | while (Size + 64 <= ToSize) { |
| 7858 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 7859 | Size += 64; |
| 7860 | } |
| 7861 | |
| 7862 | // Final in-word padding. |
| 7863 | if (Size < ToSize) { |
| 7864 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 7865 | Size = ToSize; |
| 7866 | } |
| 7867 | } |
| 7868 | |
| 7869 | // Add a floating point element at Offset. |
| 7870 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 7871 | // Unaligned floats are treated as integers. |
| 7872 | if (Offset % Bits) |
| 7873 | return; |
| 7874 | // The InReg flag is only required if there are any floats < 64 bits. |
| 7875 | if (Bits < 64) |
| 7876 | InReg = true; |
| 7877 | pad(Offset); |
| 7878 | Elems.push_back(Ty); |
| 7879 | Size = Offset + Bits; |
| 7880 | } |
| 7881 | |
| 7882 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 7883 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 7884 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 7885 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 7886 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 7887 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 7888 | switch (ElemTy->getTypeID()) { |
| 7889 | case llvm::Type::StructTyID: |
| 7890 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 7891 | break; |
| 7892 | case llvm::Type::FloatTyID: |
| 7893 | addFloat(ElemOffset, ElemTy, 32); |
| 7894 | break; |
| 7895 | case llvm::Type::DoubleTyID: |
| 7896 | addFloat(ElemOffset, ElemTy, 64); |
| 7897 | break; |
| 7898 | case llvm::Type::FP128TyID: |
| 7899 | addFloat(ElemOffset, ElemTy, 128); |
| 7900 | break; |
| 7901 | case llvm::Type::PointerTyID: |
| 7902 | if (ElemOffset % 64 == 0) { |
| 7903 | pad(ElemOffset); |
| 7904 | Elems.push_back(ElemTy); |
| 7905 | Size += 64; |
| 7906 | } |
| 7907 | break; |
| 7908 | default: |
| 7909 | break; |
| 7910 | } |
| 7911 | } |
| 7912 | } |
| 7913 | |
| 7914 | // Check if Ty is a usable substitute for the coercion type. |
| 7915 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 7916 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7917 | } |
| 7918 | |
| 7919 | // Get the coercion type as a literal struct type. |
| 7920 | llvm::Type *getType() const { |
| 7921 | if (Elems.size() == 1) |
| 7922 | return Elems.front(); |
| 7923 | else |
| 7924 | return llvm::StructType::get(Context, Elems); |
| 7925 | } |
| 7926 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7927 | }; |
| 7928 | } // end anonymous namespace |
| 7929 | |
| 7930 | ABIArgInfo |
| 7931 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 7932 | if (Ty->isVoidType()) |
| 7933 | return ABIArgInfo::getIgnore(); |
| 7934 | |
| 7935 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7936 | |
| 7937 | // Anything too big to fit in registers is passed with an explicit indirect |
| 7938 | // pointer / sret pointer. |
| 7939 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7940 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7941 | |
| 7942 | // Treat an enum type as its underlying type. |
| 7943 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7944 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7945 | |
| 7946 | // Integer types smaller than a register are extended. |
| 7947 | if (Size < 64 && Ty->isIntegerType()) |
| 7948 | return ABIArgInfo::getExtend(); |
| 7949 | |
| 7950 | // Other non-aggregates go in registers. |
| 7951 | if (!isAggregateTypeForABI(Ty)) |
| 7952 | return ABIArgInfo::getDirect(); |
| 7953 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7954 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 7955 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 7956 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7957 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7958 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7959 | // 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] | 7960 | // Build a coercion type from the LLVM struct type. |
| 7961 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7962 | if (!StrTy) |
| 7963 | return ABIArgInfo::getDirect(); |
| 7964 | |
| 7965 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 7966 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7967 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7968 | |
| 7969 | // Try to use the original type for coercion. |
| 7970 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 7971 | |
| 7972 | if (CB.InReg) |
| 7973 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 7974 | else |
| 7975 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7976 | } |
| 7977 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7978 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7979 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7980 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 7981 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7982 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7983 | AI.setCoerceToType(ArgTy); |
| 7984 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7985 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7986 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7987 | CGBuilderTy &Builder = CGF.Builder; |
| 7988 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 7989 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 7990 | |
| 7991 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 7992 | |
| 7993 | Address ArgAddr = Address::invalid(); |
| 7994 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7995 | switch (AI.getKind()) { |
| 7996 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 7997 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 7998 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7999 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8000 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8001 | case ABIArgInfo::Extend: { |
| 8002 | Stride = SlotSize; |
| 8003 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 8004 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8005 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8006 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8007 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8008 | case ABIArgInfo::Direct: { |
| 8009 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8010 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8011 | ArgAddr = Addr; |
| 8012 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8013 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8014 | |
| 8015 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8016 | Stride = SlotSize; |
| 8017 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 8018 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 8019 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8020 | break; |
| 8021 | |
| 8022 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8023 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8024 | } |
| 8025 | |
| 8026 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8027 | llvm::Value *NextPtr = |
| 8028 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 8029 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8030 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8031 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8032 | } |
| 8033 | |
| 8034 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8035 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 8036 | for (auto &I : FI.arguments()) |
| 8037 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8038 | } |
| 8039 | |
| 8040 | namespace { |
| 8041 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8042 | public: |
| 8043 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8044 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8045 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8046 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8047 | return 14; |
| 8048 | } |
| 8049 | |
| 8050 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8051 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8052 | }; |
| 8053 | } // end anonymous namespace |
| 8054 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8055 | bool |
| 8056 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 8057 | llvm::Value *Address) const { |
| 8058 | // This is calculated from the LLVM and GCC tables and verified |
| 8059 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 8060 | |
| 8061 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 8062 | |
| 8063 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 8064 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 8065 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 8066 | |
| 8067 | // 0-31: the 8-byte general-purpose registers |
| 8068 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 8069 | |
| 8070 | // 32-63: f0-31, the 4-byte floating-point registers |
| 8071 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 8072 | |
| 8073 | // Y = 64 |
| 8074 | // PSR = 65 |
| 8075 | // WIM = 66 |
| 8076 | // TBR = 67 |
| 8077 | // PC = 68 |
| 8078 | // NPC = 69 |
| 8079 | // FSR = 70 |
| 8080 | // CSR = 71 |
| 8081 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8082 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8083 | // 72-87: d0-15, the 8-byte floating-point registers |
| 8084 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 8085 | |
| 8086 | return false; |
| 8087 | } |
| 8088 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8089 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8090 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8091 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8092 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8093 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8094 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8095 | |
| 8096 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 8097 | /// it by reference between functions that append to it. |
| 8098 | typedef llvm::SmallString<128> SmallStringEnc; |
| 8099 | |
| 8100 | /// TypeStringCache caches the meta encodings of Types. |
| 8101 | /// |
| 8102 | /// The reason for caching TypeStrings is two fold: |
| 8103 | /// 1. To cache a type's encoding for later uses; |
| 8104 | /// 2. As a means to break recursive member type inclusion. |
| 8105 | /// |
| 8106 | /// A cache Entry can have a Status of: |
| 8107 | /// NonRecursive: The type encoding is not recursive; |
| 8108 | /// Recursive: The type encoding is recursive; |
| 8109 | /// Incomplete: An incomplete TypeString; |
| 8110 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 8111 | /// Recursive type encoding. |
| 8112 | /// |
| 8113 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 8114 | /// as possible. Whilst it may contain types which are recursive, the type |
| 8115 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 8116 | /// the type is encountered. |
| 8117 | /// |
| 8118 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 8119 | /// possible. The type itself is recursive and it may contain other types which |
| 8120 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 8121 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 8122 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 8123 | /// |
| 8124 | /// An Incomplete entry is always a RecordType and only encodes its |
| 8125 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 8126 | /// are placed into the cache during type expansion as a means to identify and |
| 8127 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 8128 | /// the entry becomes IncompleteUsed. |
| 8129 | /// |
| 8130 | /// During the expansion of a RecordType's members: |
| 8131 | /// |
| 8132 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 8133 | /// cached encoding is used; |
| 8134 | /// |
| 8135 | /// If the cache contains a Recursive encoding for the member type, the |
| 8136 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 8137 | /// |
| 8138 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 8139 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 8140 | /// |
| 8141 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 8142 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 8143 | /// it is swapped back in; |
| 8144 | /// |
| 8145 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 8146 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 8147 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 8148 | /// |
| 8149 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 8150 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 8151 | /// Else the member is part of a recursive type and thus the recursion has |
| 8152 | /// been exited too soon for the encoding to be correct for the member. |
| 8153 | /// |
| 8154 | class TypeStringCache { |
| 8155 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 8156 | struct Entry { |
| 8157 | std::string Str; // The encoded TypeString for the type. |
| 8158 | enum Status State; // Information about the encoding in 'Str'. |
| 8159 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 8160 | // during the expansion of RecordType's members. |
| 8161 | }; |
| 8162 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 8163 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 8164 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 8165 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8166 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8167 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 8168 | bool removeIncomplete(const IdentifierInfo *ID); |
| 8169 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8170 | bool IsRecursive); |
| 8171 | StringRef lookupStr(const IdentifierInfo *ID); |
| 8172 | }; |
| 8173 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8174 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8175 | /// FieldEncoding is a helper for this ordering process. |
| 8176 | class FieldEncoding { |
| 8177 | bool HasName; |
| 8178 | std::string Enc; |
| 8179 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8180 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8181 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8182 | bool operator<(const FieldEncoding &rhs) const { |
| 8183 | if (HasName != rhs.HasName) return HasName; |
| 8184 | return Enc < rhs.Enc; |
| 8185 | } |
| 8186 | }; |
| 8187 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8188 | class XCoreABIInfo : public DefaultABIInfo { |
| 8189 | public: |
| 8190 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8191 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8192 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8193 | }; |
| 8194 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8195 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8196 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8197 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8198 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8199 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 8200 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8201 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8202 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8203 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8204 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8205 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 8206 | // TODO: this implementation is likely now redundant with the default |
| 8207 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8208 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8209 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8210 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8211 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8212 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8213 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 8214 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8215 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8216 | // Handle the argument. |
| 8217 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8218 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8219 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8220 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8221 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8222 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8223 | |
| 8224 | Address Val = Address::invalid(); |
| 8225 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8226 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8227 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8228 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8229 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8230 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8231 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8232 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 8233 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8234 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8235 | case ABIArgInfo::Extend: |
| 8236 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8237 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 8238 | ArgSize = CharUnits::fromQuantity( |
| 8239 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8240 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8241 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8242 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8243 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 8244 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 8245 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8246 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8247 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8248 | |
| 8249 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8250 | if (!ArgSize.isZero()) { |
| 8251 | llvm::Value *APN = |
| 8252 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 8253 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8254 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8255 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8256 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8257 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8258 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8259 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8260 | /// into the cache as a means to identify and break recursion. |
| 8261 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8262 | /// be reinserted by removeIncomplete(). |
| 8263 | /// All other types of encoding should have been used rather than arriving here. |
| 8264 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8265 | std::string StubEnc) { |
| 8266 | if (!ID) |
| 8267 | return; |
| 8268 | Entry &E = Map[ID]; |
| 8269 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8270 | "Incorrectly use of addIncomplete"); |
| 8271 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8272 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8273 | E.Str.swap(StubEnc); |
| 8274 | E.State = Incomplete; |
| 8275 | ++IncompleteCount; |
| 8276 | } |
| 8277 | |
| 8278 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8279 | /// must be removed from the cache. |
| 8280 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8281 | /// Returns true if the RecordType was defined recursively. |
| 8282 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8283 | if (!ID) |
| 8284 | return false; |
| 8285 | auto I = Map.find(ID); |
| 8286 | assert(I != Map.end() && "Entry not present"); |
| 8287 | Entry &E = I->second; |
| 8288 | assert( (E.State == Incomplete || |
| 8289 | E.State == IncompleteUsed) && |
| 8290 | "Entry must be an incomplete type"); |
| 8291 | bool IsRecursive = false; |
| 8292 | if (E.State == IncompleteUsed) { |
| 8293 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8294 | IsRecursive = true; |
| 8295 | --IncompleteUsedCount; |
| 8296 | } |
| 8297 | if (E.Swapped.empty()) |
| 8298 | Map.erase(I); |
| 8299 | else { |
| 8300 | // Swap the Recursive back. |
| 8301 | E.Swapped.swap(E.Str); |
| 8302 | E.Swapped.clear(); |
| 8303 | E.State = Recursive; |
| 8304 | } |
| 8305 | --IncompleteCount; |
| 8306 | return IsRecursive; |
| 8307 | } |
| 8308 | |
| 8309 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8310 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8311 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8312 | bool IsRecursive) { |
| 8313 | if (!ID || IncompleteUsedCount) |
| 8314 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8315 | Entry &E = Map[ID]; |
| 8316 | if (IsRecursive && !E.Str.empty()) { |
| 8317 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8318 | "This is not the same Recursive entry"); |
| 8319 | // The parent container was not recursive after all, so we could have used |
| 8320 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8321 | // we started viz: IncompleteCount!=0. |
| 8322 | return; |
| 8323 | } |
| 8324 | assert(E.Str.empty() && "Entry already present"); |
| 8325 | E.Str = Str.str(); |
| 8326 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8327 | } |
| 8328 | |
| 8329 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8330 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8331 | /// encoding is Recursive, return an empty StringRef. |
| 8332 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8333 | if (!ID) |
| 8334 | return StringRef(); // We have no key. |
| 8335 | auto I = Map.find(ID); |
| 8336 | if (I == Map.end()) |
| 8337 | return StringRef(); // We have no encoding. |
| 8338 | Entry &E = I->second; |
| 8339 | if (E.State == Recursive && IncompleteCount) |
| 8340 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8341 | |
| 8342 | if (E.State == Incomplete) { |
| 8343 | // The incomplete type is being used to break out of recursion. |
| 8344 | E.State = IncompleteUsed; |
| 8345 | ++IncompleteUsedCount; |
| 8346 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8347 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8348 | } |
| 8349 | |
| 8350 | /// The XCore ABI includes a type information section that communicates symbol |
| 8351 | /// type information to the linker. The linker uses this information to verify |
| 8352 | /// safety/correctness of things such as array bound and pointers et al. |
| 8353 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8354 | /// This type information (TypeString) is emitted into meta data for all global |
| 8355 | /// symbols: definitions, declarations, functions & variables. |
| 8356 | /// |
| 8357 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8358 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8359 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8360 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8361 | /// |
| 8362 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8363 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8364 | |
| 8365 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8366 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8367 | CodeGen::CodeGenModule &CGM) const { |
| 8368 | SmallStringEnc Enc; |
| 8369 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8370 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8371 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8372 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8373 | llvm::NamedMDNode *MD = |
| 8374 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8375 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8376 | } |
| 8377 | } |
| 8378 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8379 | //===----------------------------------------------------------------------===// |
| 8380 | // SPIR ABI Implementation |
| 8381 | //===----------------------------------------------------------------------===// |
| 8382 | |
| 8383 | namespace { |
| 8384 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8385 | public: |
| 8386 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8387 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8388 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8389 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8390 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8391 | } // End anonymous namespace. |
| 8392 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8393 | namespace clang { |
| 8394 | namespace CodeGen { |
| 8395 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8396 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8397 | SPIRABI.computeInfo(FI); |
| 8398 | } |
| 8399 | } |
| 8400 | } |
| 8401 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8402 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8403 | return llvm::CallingConv::SPIR_KERNEL; |
| 8404 | } |
| 8405 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8406 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8407 | const CodeGen::CodeGenModule &CGM, |
| 8408 | TypeStringCache &TSC); |
| 8409 | |
| 8410 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8411 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8412 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8413 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8414 | const RecordDecl *RD, |
| 8415 | const CodeGen::CodeGenModule &CGM, |
| 8416 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8417 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8418 | SmallStringEnc Enc; |
| 8419 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8420 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8421 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8422 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8423 | Enc += "b("; |
| 8424 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8425 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8426 | Enc += ':'; |
| 8427 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8428 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8429 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8430 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8431 | Enc += ')'; |
| 8432 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 8433 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8434 | } |
| 8435 | return true; |
| 8436 | } |
| 8437 | |
| 8438 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 8439 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 8440 | /// Union types have their fields ordered according to the ABI. |
| 8441 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 8442 | const CodeGen::CodeGenModule &CGM, |
| 8443 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 8444 | // Append the cached TypeString if we have one. |
| 8445 | StringRef TypeString = TSC.lookupStr(ID); |
| 8446 | if (!TypeString.empty()) { |
| 8447 | Enc += TypeString; |
| 8448 | return true; |
| 8449 | } |
| 8450 | |
| 8451 | // Start to emit an incomplete TypeString. |
| 8452 | size_t Start = Enc.size(); |
| 8453 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 8454 | Enc += '('; |
| 8455 | if (ID) |
| 8456 | Enc += ID->getName(); |
| 8457 | Enc += "){"; |
| 8458 | |
| 8459 | // We collect all encoded fields and order as necessary. |
| 8460 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8461 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 8462 | if (RD && !RD->field_empty()) { |
| 8463 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 8464 | // so that recursive calls to this RecordType will use it whilst building a |
| 8465 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8466 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8467 | std::string StubEnc(Enc.substr(Start).str()); |
| 8468 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 8469 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 8470 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 8471 | (void) TSC.removeIncomplete(ID); |
| 8472 | return false; |
| 8473 | } |
| 8474 | IsRecursive = TSC.removeIncomplete(ID); |
| 8475 | // The ABI requires unions to be sorted but not structures. |
| 8476 | // See FieldEncoding::operator< for sort algorithm. |
| 8477 | if (RT->isUnionType()) |
| 8478 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8479 | // We can now complete the TypeString. |
| 8480 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8481 | for (unsigned I = 0; I != E; ++I) { |
| 8482 | if (I) |
| 8483 | Enc += ','; |
| 8484 | Enc += FE[I].str(); |
| 8485 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8486 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8487 | Enc += '}'; |
| 8488 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 8489 | return true; |
| 8490 | } |
| 8491 | |
| 8492 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 8493 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 8494 | TypeStringCache &TSC, |
| 8495 | const IdentifierInfo *ID) { |
| 8496 | // Append the cached TypeString if we have one. |
| 8497 | StringRef TypeString = TSC.lookupStr(ID); |
| 8498 | if (!TypeString.empty()) { |
| 8499 | Enc += TypeString; |
| 8500 | return true; |
| 8501 | } |
| 8502 | |
| 8503 | size_t Start = Enc.size(); |
| 8504 | Enc += "e("; |
| 8505 | if (ID) |
| 8506 | Enc += ID->getName(); |
| 8507 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8508 | |
| 8509 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8510 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8511 | SmallVector<FieldEncoding, 16> FE; |
| 8512 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 8513 | ++I) { |
| 8514 | SmallStringEnc EnumEnc; |
| 8515 | EnumEnc += "m("; |
| 8516 | EnumEnc += I->getName(); |
| 8517 | EnumEnc += "){"; |
| 8518 | I->getInitVal().toString(EnumEnc); |
| 8519 | EnumEnc += '}'; |
| 8520 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 8521 | } |
| 8522 | std::sort(FE.begin(), FE.end()); |
| 8523 | unsigned E = FE.size(); |
| 8524 | for (unsigned I = 0; I != E; ++I) { |
| 8525 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8526 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8527 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8528 | } |
| 8529 | } |
| 8530 | Enc += '}'; |
| 8531 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 8532 | return true; |
| 8533 | } |
| 8534 | |
| 8535 | /// Appends type's qualifier to Enc. |
| 8536 | /// This is done prior to appending the type's encoding. |
| 8537 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 8538 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 8539 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8540 | int Lookup = 0; |
| 8541 | if (QT.isConstQualified()) |
| 8542 | Lookup += 1<<0; |
| 8543 | if (QT.isRestrictQualified()) |
| 8544 | Lookup += 1<<1; |
| 8545 | if (QT.isVolatileQualified()) |
| 8546 | Lookup += 1<<2; |
| 8547 | Enc += Table[Lookup]; |
| 8548 | } |
| 8549 | |
| 8550 | /// Appends built-in types to Enc. |
| 8551 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 8552 | const char *EncType; |
| 8553 | switch (BT->getKind()) { |
| 8554 | case BuiltinType::Void: |
| 8555 | EncType = "0"; |
| 8556 | break; |
| 8557 | case BuiltinType::Bool: |
| 8558 | EncType = "b"; |
| 8559 | break; |
| 8560 | case BuiltinType::Char_U: |
| 8561 | EncType = "uc"; |
| 8562 | break; |
| 8563 | case BuiltinType::UChar: |
| 8564 | EncType = "uc"; |
| 8565 | break; |
| 8566 | case BuiltinType::SChar: |
| 8567 | EncType = "sc"; |
| 8568 | break; |
| 8569 | case BuiltinType::UShort: |
| 8570 | EncType = "us"; |
| 8571 | break; |
| 8572 | case BuiltinType::Short: |
| 8573 | EncType = "ss"; |
| 8574 | break; |
| 8575 | case BuiltinType::UInt: |
| 8576 | EncType = "ui"; |
| 8577 | break; |
| 8578 | case BuiltinType::Int: |
| 8579 | EncType = "si"; |
| 8580 | break; |
| 8581 | case BuiltinType::ULong: |
| 8582 | EncType = "ul"; |
| 8583 | break; |
| 8584 | case BuiltinType::Long: |
| 8585 | EncType = "sl"; |
| 8586 | break; |
| 8587 | case BuiltinType::ULongLong: |
| 8588 | EncType = "ull"; |
| 8589 | break; |
| 8590 | case BuiltinType::LongLong: |
| 8591 | EncType = "sll"; |
| 8592 | break; |
| 8593 | case BuiltinType::Float: |
| 8594 | EncType = "ft"; |
| 8595 | break; |
| 8596 | case BuiltinType::Double: |
| 8597 | EncType = "d"; |
| 8598 | break; |
| 8599 | case BuiltinType::LongDouble: |
| 8600 | EncType = "ld"; |
| 8601 | break; |
| 8602 | default: |
| 8603 | return false; |
| 8604 | } |
| 8605 | Enc += EncType; |
| 8606 | return true; |
| 8607 | } |
| 8608 | |
| 8609 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 8610 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 8611 | const CodeGen::CodeGenModule &CGM, |
| 8612 | TypeStringCache &TSC) { |
| 8613 | Enc += "p("; |
| 8614 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 8615 | return false; |
| 8616 | Enc += ')'; |
| 8617 | return true; |
| 8618 | } |
| 8619 | |
| 8620 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8621 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 8622 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8623 | const CodeGen::CodeGenModule &CGM, |
| 8624 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 8625 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 8626 | return false; |
| 8627 | Enc += "a("; |
| 8628 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 8629 | CAT->getSize().toStringUnsigned(Enc); |
| 8630 | else |
| 8631 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 8632 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8633 | // The Qualifiers should be attached to the type rather than the array. |
| 8634 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8635 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 8636 | return false; |
| 8637 | Enc += ')'; |
| 8638 | return true; |
| 8639 | } |
| 8640 | |
| 8641 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 8642 | /// and the arguments. |
| 8643 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 8644 | const CodeGen::CodeGenModule &CGM, |
| 8645 | TypeStringCache &TSC) { |
| 8646 | Enc += "f{"; |
| 8647 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 8648 | return false; |
| 8649 | Enc += "}("; |
| 8650 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 8651 | // N.B. we are only interested in the adjusted param types. |
| 8652 | auto I = FPT->param_type_begin(); |
| 8653 | auto E = FPT->param_type_end(); |
| 8654 | if (I != E) { |
| 8655 | do { |
| 8656 | if (!appendType(Enc, *I, CGM, TSC)) |
| 8657 | return false; |
| 8658 | ++I; |
| 8659 | if (I != E) |
| 8660 | Enc += ','; |
| 8661 | } while (I != E); |
| 8662 | if (FPT->isVariadic()) |
| 8663 | Enc += ",va"; |
| 8664 | } else { |
| 8665 | if (FPT->isVariadic()) |
| 8666 | Enc += "va"; |
| 8667 | else |
| 8668 | Enc += '0'; |
| 8669 | } |
| 8670 | } |
| 8671 | Enc += ')'; |
| 8672 | return true; |
| 8673 | } |
| 8674 | |
| 8675 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 8676 | /// type encodings. |
| 8677 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8678 | const CodeGen::CodeGenModule &CGM, |
| 8679 | TypeStringCache &TSC) { |
| 8680 | |
| 8681 | QualType QT = QType.getCanonicalType(); |
| 8682 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8683 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 8684 | // The Qualifiers should be attached to the type rather than the array. |
| 8685 | // Thus we don't call appendQualifier() here. |
| 8686 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 8687 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8688 | appendQualifier(Enc, QT); |
| 8689 | |
| 8690 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 8691 | return appendBuiltinType(Enc, BT); |
| 8692 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8693 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 8694 | return appendPointerType(Enc, PT, CGM, TSC); |
| 8695 | |
| 8696 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 8697 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 8698 | |
| 8699 | if (const RecordType *RT = QT->getAsStructureType()) |
| 8700 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8701 | |
| 8702 | if (const RecordType *RT = QT->getAsUnionType()) |
| 8703 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8704 | |
| 8705 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 8706 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 8707 | |
| 8708 | return false; |
| 8709 | } |
| 8710 | |
| 8711 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8712 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 8713 | if (!D) |
| 8714 | return false; |
| 8715 | |
| 8716 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8717 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 8718 | return false; |
| 8719 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 8720 | } |
| 8721 | |
| 8722 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8723 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 8724 | return false; |
| 8725 | QualType QT = VD->getType().getCanonicalType(); |
| 8726 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 8727 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8728 | // The Qualifiers should be attached to the type rather than the array. |
| 8729 | // Thus we don't call appendQualifier() here. |
| 8730 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8731 | } |
| 8732 | return appendType(Enc, QT, CGM, TSC); |
| 8733 | } |
| 8734 | return false; |
| 8735 | } |
| 8736 | |
| 8737 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8738 | //===----------------------------------------------------------------------===// |
| 8739 | // Driver code |
| 8740 | //===----------------------------------------------------------------------===// |
| 8741 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8742 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 8743 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8744 | } |
| 8745 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 8746 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8747 | if (TheTargetCodeGenInfo) |
| 8748 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8749 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8750 | // Helper to set the unique_ptr while still keeping the return value. |
| 8751 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 8752 | this->TheTargetCodeGenInfo.reset(P); |
| 8753 | return *P; |
| 8754 | }; |
| 8755 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 8756 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 8757 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8758 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8759 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8760 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 8761 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8762 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 8763 | case llvm::Triple::mips: |
| 8764 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 8765 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8766 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 8767 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8768 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 8769 | case llvm::Triple::mips64: |
| 8770 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8771 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8772 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 8773 | case llvm::Triple::avr: |
| 8774 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 8775 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 8776 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 8777 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8778 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 8779 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8780 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 8781 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 8782 | return SetCGInfo( |
| 8783 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8784 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8785 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8786 | } |
| 8787 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8788 | case llvm::Triple::wasm32: |
| 8789 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8790 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8791 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8792 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 8793 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8794 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8795 | case llvm::Triple::thumbeb: { |
| 8796 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 8797 | return SetCGInfo( |
| 8798 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 8799 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8800 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8801 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 8802 | StringRef ABIStr = getTarget().getABI(); |
| 8803 | if (ABIStr == "apcs-gnu") |
| 8804 | Kind = ARMABIInfo::APCS; |
| 8805 | else if (ABIStr == "aapcs16") |
| 8806 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 8807 | else if (CodeGenOpts.FloatABI == "hard" || |
| 8808 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8809 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 8810 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8811 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8812 | Kind = ARMABIInfo::AAPCS_VFP; |
| 8813 | |
| 8814 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 8815 | } |
| 8816 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8817 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8818 | return SetCGInfo( |
| 8819 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 8820 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8821 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8822 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8823 | if (getTarget().getABI() == "elfv2") |
| 8824 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8825 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8826 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8827 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8828 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 8829 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8830 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8831 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8832 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 8833 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8834 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8835 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8836 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8837 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8838 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8839 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8840 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 8841 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8842 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8843 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 8844 | case llvm::Triple::nvptx: |
| 8845 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8846 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 8847 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8848 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8849 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8850 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8851 | case llvm::Triple::systemz: { |
| 8852 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8853 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8854 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 8855 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8856 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 8857 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8858 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8859 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8860 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8861 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 8862 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8863 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 8864 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 8865 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8866 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8867 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 8868 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8869 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8870 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8871 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 8872 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8873 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 8874 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8875 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8876 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8877 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8878 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8879 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8880 | X86AVXABILevel AVXLevel = |
| 8881 | (ABI == "avx512" |
| 8882 | ? X86AVXABILevel::AVX512 |
| 8883 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8884 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8885 | switch (Triple.getOS()) { |
| 8886 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8887 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 8888 | case llvm::Triple::PS4: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8889 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8890 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8891 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8892 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8893 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 8894 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8895 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 8896 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8897 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8898 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8899 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 8900 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8901 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 8902 | case llvm::Triple::sparc: |
| 8903 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8904 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8905 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8906 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8907 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8908 | case llvm::Triple::spir: |
| 8909 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8910 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8911 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8912 | } |