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 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 447 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 448 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 449 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 450 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 451 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 452 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 453 | if (FD->isUnnamedBitfield()) |
| 454 | return true; |
| 455 | |
| 456 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 457 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 458 | // Constant arrays of empty records count as empty, strip them off. |
| 459 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 460 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 461 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 462 | if (AT->getSize() == 0) |
| 463 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 464 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 465 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 466 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 467 | const RecordType *RT = FT->getAs<RecordType>(); |
| 468 | if (!RT) |
| 469 | return false; |
| 470 | |
| 471 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 472 | // |
| 473 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 474 | // current ABI. |
| 475 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 476 | return false; |
| 477 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 478 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 481 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 482 | /// fields. Note that a structure with a flexible array member is not |
| 483 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 484 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 485 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 486 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 487 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 488 | const RecordDecl *RD = RT->getDecl(); |
| 489 | if (RD->hasFlexibleArrayMember()) |
| 490 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 491 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 492 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 493 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 494 | for (const auto &I : CXXRD->bases()) |
| 495 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 496 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 497 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 498 | for (const auto *I : RD->fields()) |
| 499 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 500 | return false; |
| 501 | return true; |
| 502 | } |
| 503 | |
| 504 | /// isSingleElementStruct - Determine if a structure is a "single |
| 505 | /// element struct", i.e. it has exactly one non-empty field or |
| 506 | /// exactly one field which is itself a single element |
| 507 | /// struct. Structures with flexible array members are never |
| 508 | /// considered single element structs. |
| 509 | /// |
| 510 | /// \return The field declaration for the single non-empty field, if |
| 511 | /// it exists. |
| 512 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 513 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 514 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 515 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 516 | |
| 517 | const RecordDecl *RD = RT->getDecl(); |
| 518 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 519 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 520 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 521 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 522 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 523 | // If this is a C++ record, check the bases first. |
| 524 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 525 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 526 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 527 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 528 | continue; |
| 529 | |
| 530 | // If we already found an element then this isn't a single-element struct. |
| 531 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 532 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 533 | |
| 534 | // If this is non-empty and not a single element struct, the composite |
| 535 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 536 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 537 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 538 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 543 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 544 | QualType FT = FD->getType(); |
| 545 | |
| 546 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 547 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 548 | continue; |
| 549 | |
| 550 | // If we already found an element then this isn't a single-element |
| 551 | // struct. |
| 552 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 553 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 554 | |
| 555 | // Treat single element arrays as the element. |
| 556 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 557 | if (AT->getSize().getZExtValue() != 1) |
| 558 | break; |
| 559 | FT = AT->getElementType(); |
| 560 | } |
| 561 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 562 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 563 | Found = FT.getTypePtr(); |
| 564 | } else { |
| 565 | Found = isSingleElementStruct(FT, Context); |
| 566 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 567 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 571 | // We don't consider a struct a single-element struct if it has |
| 572 | // padding beyond the element type. |
| 573 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 574 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 575 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 576 | return Found; |
| 577 | } |
| 578 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 579 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 580 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 581 | const ABIArgInfo &AI) { |
| 582 | // This default implementation defers to the llvm backend's va_arg |
| 583 | // instruction. It can handle only passing arguments directly |
| 584 | // (typically only handled in the backend for primitive types), or |
| 585 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 586 | // flag has ABI impact in the callee, this implementation cannot |
| 587 | // work.) |
| 588 | |
| 589 | // Only a few cases are covered here at the moment -- those needed |
| 590 | // by the default abi. |
| 591 | llvm::Value *Val; |
| 592 | |
| 593 | if (AI.isIndirect()) { |
| 594 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 595 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 596 | assert( |
| 597 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 598 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 599 | |
| 600 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 601 | CharUnits TyAlignForABI = TyInfo.second; |
| 602 | |
| 603 | llvm::Type *BaseTy = |
| 604 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 605 | llvm::Value *Addr = |
| 606 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 607 | return Address(Addr, TyAlignForABI); |
| 608 | } else { |
| 609 | assert((AI.isDirect() || AI.isExtend()) && |
| 610 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 611 | |
| 612 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 613 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 614 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 615 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 616 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 617 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 618 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 619 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 620 | |
| 621 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 622 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 623 | CGF.Builder.CreateStore(Val, Temp); |
| 624 | return Temp; |
| 625 | } |
| 626 | } |
| 627 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 628 | /// DefaultABIInfo - The default implementation for ABI specific |
| 629 | /// details. This implementation provides information which results in |
| 630 | /// self-consistent and sensible LLVM IR generation, but does not |
| 631 | /// conform to any particular ABI. |
| 632 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 633 | public: |
| 634 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 636 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 637 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 638 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 639 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 640 | if (!getCXXABI().classifyReturnType(FI)) |
| 641 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 642 | for (auto &I : FI.arguments()) |
| 643 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 644 | } |
| 645 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 646 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 647 | QualType Ty) const override { |
| 648 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 649 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 650 | }; |
| 651 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 652 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 653 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 654 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 655 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 656 | }; |
| 657 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 658 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 659 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 660 | |
| 661 | if (isAggregateTypeForABI(Ty)) { |
| 662 | // Records with non-trivial destructors/copy-constructors should not be |
| 663 | // passed by value. |
| 664 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 665 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 666 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 667 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 668 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 669 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 670 | // Treat an enum type as its underlying type. |
| 671 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 672 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 673 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 674 | return (Ty->isPromotableIntegerType() ? |
| 675 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 678 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 679 | if (RetTy->isVoidType()) |
| 680 | return ABIArgInfo::getIgnore(); |
| 681 | |
| 682 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 683 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 684 | |
| 685 | // Treat an enum type as its underlying type. |
| 686 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 687 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 688 | |
| 689 | return (RetTy->isPromotableIntegerType() ? |
| 690 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 691 | } |
| 692 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 693 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 694 | // WebAssembly ABI Implementation |
| 695 | // |
| 696 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 697 | //===----------------------------------------------------------------------===// |
| 698 | |
| 699 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 700 | public: |
| 701 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 702 | : DefaultABIInfo(CGT) {} |
| 703 | |
| 704 | private: |
| 705 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 706 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 707 | |
| 708 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 709 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 710 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 711 | void computeInfo(CGFunctionInfo &FI) const override { |
| 712 | if (!getCXXABI().classifyReturnType(FI)) |
| 713 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 714 | for (auto &Arg : FI.arguments()) |
| 715 | Arg.info = classifyArgumentType(Arg.type); |
| 716 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 717 | |
| 718 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 719 | QualType Ty) const override; |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 720 | }; |
| 721 | |
| 722 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 723 | public: |
| 724 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 725 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 726 | }; |
| 727 | |
| 728 | /// \brief Classify argument of given type \p Ty. |
| 729 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 730 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 731 | |
| 732 | if (isAggregateTypeForABI(Ty)) { |
| 733 | // Records with non-trivial destructors/copy-constructors should not be |
| 734 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 735 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 736 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 737 | // Ignore empty structs/unions. |
| 738 | if (isEmptyRecord(getContext(), Ty, true)) |
| 739 | return ABIArgInfo::getIgnore(); |
| 740 | // Lower single-element structs to just pass a regular value. TODO: We |
| 741 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 742 | // though watch out for things like bitfields. |
| 743 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 744 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | // Otherwise just do the default thing. |
| 748 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 749 | } |
| 750 | |
| 751 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 752 | if (isAggregateTypeForABI(RetTy)) { |
| 753 | // Records with non-trivial destructors/copy-constructors should not be |
| 754 | // returned by value. |
| 755 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 756 | // Ignore empty structs/unions. |
| 757 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 758 | return ABIArgInfo::getIgnore(); |
| 759 | // Lower single-element structs to just return a regular value. TODO: We |
| 760 | // could do reasonable-size multiple-element structs too, using |
| 761 | // ABIArgInfo::getDirect(). |
| 762 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 763 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | // Otherwise just do the default thing. |
| 768 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 769 | } |
| 770 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 771 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 772 | QualType Ty) const { |
| 773 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 774 | getContext().getTypeInfoInChars(Ty), |
| 775 | CharUnits::fromQuantity(4), |
| 776 | /*AllowHigherAlign=*/ true); |
| 777 | } |
| 778 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 779 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 780 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 781 | // |
| 782 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 783 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 784 | //===----------------------------------------------------------------------===// |
| 785 | |
| 786 | class PNaClABIInfo : public ABIInfo { |
| 787 | public: |
| 788 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 789 | |
| 790 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 791 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 792 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 793 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 794 | Address EmitVAArg(CodeGenFunction &CGF, |
| 795 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 796 | }; |
| 797 | |
| 798 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 799 | public: |
| 800 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 801 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 802 | }; |
| 803 | |
| 804 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 805 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 806 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 807 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 808 | for (auto &I : FI.arguments()) |
| 809 | I.info = classifyArgumentType(I.type); |
| 810 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 811 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 812 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 813 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 814 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 815 | // function classification. Structs get passed directly for varargs |
| 816 | // functions, through a rewriting transform in |
| 817 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 818 | // this target to actually support a va_arg instructions with an |
| 819 | // aggregate type, unlike other targets. |
| 820 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 823 | /// \brief Classify argument of given type \p Ty. |
| 824 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 825 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 826 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 827 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 828 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 829 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 830 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 831 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 832 | } else if (Ty->isFloatingType()) { |
| 833 | // Floating-point types don't go inreg. |
| 834 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 835 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 836 | |
| 837 | return (Ty->isPromotableIntegerType() ? |
| 838 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 842 | if (RetTy->isVoidType()) |
| 843 | return ABIArgInfo::getIgnore(); |
| 844 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 845 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 846 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 847 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 848 | |
| 849 | // Treat an enum type as its underlying type. |
| 850 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 851 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 852 | |
| 853 | return (RetTy->isPromotableIntegerType() ? |
| 854 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 855 | } |
| 856 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 857 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 858 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 859 | // 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] | 860 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 861 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 862 | IRType->getScalarSizeInBits() != 64; |
| 863 | } |
| 864 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 865 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 866 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 867 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 868 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 869 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 870 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 871 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 874 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 878 | return Ty; |
| 879 | } |
| 880 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 881 | /// Returns true if this type can be passed in SSE registers with the |
| 882 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 883 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 884 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 885 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 886 | return true; |
| 887 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 888 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 889 | // registers specially. |
| 890 | unsigned VecSize = Context.getTypeSize(VT); |
| 891 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 892 | return true; |
| 893 | } |
| 894 | return false; |
| 895 | } |
| 896 | |
| 897 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 898 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 899 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 900 | return NumMembers <= 4; |
| 901 | } |
| 902 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 903 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 904 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 905 | auto AI = ABIArgInfo::getDirect(T); |
| 906 | AI.setInReg(true); |
| 907 | AI.setCanBeFlattened(false); |
| 908 | return AI; |
| 909 | } |
| 910 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 911 | //===----------------------------------------------------------------------===// |
| 912 | // X86-32 ABI Implementation |
| 913 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 914 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 915 | /// \brief Similar to llvm::CCState, but for Clang. |
| 916 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 917 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 918 | |
| 919 | unsigned CC; |
| 920 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 921 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 922 | }; |
| 923 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 924 | enum { |
| 925 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 926 | VectorcallMaxParamNumAsReg = 6 |
| 927 | }; |
| 928 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 929 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 930 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 931 | enum Class { |
| 932 | Integer, |
| 933 | Float |
| 934 | }; |
| 935 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 936 | static const unsigned MinABIStackAlignInBytes = 4; |
| 937 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 938 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 939 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 940 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 941 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 942 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 943 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 944 | |
| 945 | static bool isRegisterSize(unsigned Size) { |
| 946 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 947 | } |
| 948 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 949 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 950 | // FIXME: Assumes vectorcall is in use. |
| 951 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 952 | } |
| 953 | |
| 954 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 955 | uint64_t NumMembers) const override { |
| 956 | // FIXME: Assumes vectorcall is in use. |
| 957 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 958 | } |
| 959 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 960 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 961 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 962 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 963 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 964 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 965 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 966 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 967 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 968 | /// \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] | 969 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 970 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 971 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 972 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 973 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 974 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 975 | /// \brief Updates the number of available free registers, returns |
| 976 | /// true if any registers were allocated. |
| 977 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 978 | |
| 979 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 980 | bool &NeedsPadding) const; |
| 981 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 982 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 983 | bool canExpandIndirectArgument(QualType Ty) const; |
| 984 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 985 | /// \brief Rewrite the function info so that all memory arguments use |
| 986 | /// inalloca. |
| 987 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 988 | |
| 989 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 990 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 991 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 992 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 993 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 994 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 995 | public: |
| 996 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 997 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 998 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 999 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1000 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1001 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1002 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1003 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1004 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1005 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 1006 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1007 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1008 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1009 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1010 | |
| 1011 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 1012 | ArrayRef<llvm::Type*> scalars, |
| 1013 | bool asReturnValue) const override { |
| 1014 | // LLVM's x86-32 lowering currently only assigns up to three |
| 1015 | // integer registers and three fp registers. Oddly, it'll use up to |
| 1016 | // four vector registers for vectors, but those can overlap with the |
| 1017 | // scalar registers. |
| 1018 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
| 1019 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 1020 | |
| 1021 | bool isSwiftErrorInRegister() const override { |
| 1022 | // x86-32 lowering does not support passing swifterror in a register. |
| 1023 | return false; |
| 1024 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1025 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1026 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1027 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1028 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1029 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1030 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1031 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 1032 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1033 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 1034 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1035 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1036 | static bool isStructReturnInRegABI( |
| 1037 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1038 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1039 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 1040 | CodeGen::CodeGenModule &CGM, |
| 1041 | ForDefinition_t IsForDefinition) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1042 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1043 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1044 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1045 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1046 | return 4; |
| 1047 | } |
| 1048 | |
| 1049 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1050 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1051 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1052 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1053 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1054 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1055 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1056 | } |
| 1057 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1058 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1059 | std::string &Constraints, |
| 1060 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1061 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1062 | std::vector<LValue> &ResultRegDests, |
| 1063 | std::string &AsmString, |
| 1064 | unsigned NumOutputs) const override; |
| 1065 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1066 | llvm::Constant * |
| 1067 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1068 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1069 | (0x06 << 8) | // .+0x08 |
| 1070 | ('F' << 16) | |
| 1071 | ('T' << 24); |
| 1072 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1073 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1074 | |
| 1075 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1076 | return "movl\t%ebp, %ebp" |
| 1077 | "\t\t## marker for objc_retainAutoreleaseReturnValue"; |
| 1078 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1079 | }; |
| 1080 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1081 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1082 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1083 | /// Rewrite input constraint references after adding some output constraints. |
| 1084 | /// In the case where there is one output and one input and we add one output, |
| 1085 | /// we need to replace all operand references greater than or equal to 1: |
| 1086 | /// mov $0, $1 |
| 1087 | /// mov eax, $1 |
| 1088 | /// The result will be: |
| 1089 | /// mov $0, $2 |
| 1090 | /// mov eax, $2 |
| 1091 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1092 | unsigned NumNewOuts, |
| 1093 | std::string &AsmString) { |
| 1094 | std::string Buf; |
| 1095 | llvm::raw_string_ostream OS(Buf); |
| 1096 | size_t Pos = 0; |
| 1097 | while (Pos < AsmString.size()) { |
| 1098 | size_t DollarStart = AsmString.find('$', Pos); |
| 1099 | if (DollarStart == std::string::npos) |
| 1100 | DollarStart = AsmString.size(); |
| 1101 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1102 | if (DollarEnd == std::string::npos) |
| 1103 | DollarEnd = AsmString.size(); |
| 1104 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1105 | Pos = DollarEnd; |
| 1106 | size_t NumDollars = DollarEnd - DollarStart; |
| 1107 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1108 | // We have an operand reference. |
| 1109 | size_t DigitStart = Pos; |
| 1110 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1111 | if (DigitEnd == std::string::npos) |
| 1112 | DigitEnd = AsmString.size(); |
| 1113 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1114 | unsigned OperandIndex; |
| 1115 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1116 | if (OperandIndex >= FirstIn) |
| 1117 | OperandIndex += NumNewOuts; |
| 1118 | OS << OperandIndex; |
| 1119 | } else { |
| 1120 | OS << OperandStr; |
| 1121 | } |
| 1122 | Pos = DigitEnd; |
| 1123 | } |
| 1124 | } |
| 1125 | AsmString = std::move(OS.str()); |
| 1126 | } |
| 1127 | |
| 1128 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1129 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1130 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1131 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1132 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1133 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1134 | unsigned NumOutputs) const { |
| 1135 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1136 | |
| 1137 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1138 | // larger. |
| 1139 | if (!Constraints.empty()) |
| 1140 | Constraints += ','; |
| 1141 | if (RetWidth <= 32) { |
| 1142 | Constraints += "={eax}"; |
| 1143 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1144 | } else { |
| 1145 | // Use the 'A' constraint for EAX:EDX. |
| 1146 | Constraints += "=A"; |
| 1147 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1148 | } |
| 1149 | |
| 1150 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1151 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1152 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1153 | |
| 1154 | // Coerce the integer by bitcasting the return slot pointer. |
| 1155 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1156 | CoerceTy->getPointerTo())); |
| 1157 | ResultRegDests.push_back(ReturnSlot); |
| 1158 | |
| 1159 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1160 | } |
| 1161 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1162 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1163 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1164 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1165 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1166 | uint64_t Size = Context.getTypeSize(Ty); |
| 1167 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1168 | // For i386, type must be register sized. |
| 1169 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1170 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1171 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1172 | |
| 1173 | if (Ty->isVectorType()) { |
| 1174 | // 64- and 128- bit vectors inside structures are not returned in |
| 1175 | // registers. |
| 1176 | if (Size == 64 || Size == 128) |
| 1177 | return false; |
| 1178 | |
| 1179 | return true; |
| 1180 | } |
| 1181 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1182 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1183 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1184 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1185 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1186 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1187 | return true; |
| 1188 | |
| 1189 | // Arrays are treated like records. |
| 1190 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1191 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1192 | |
| 1193 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1194 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1195 | if (!RT) return false; |
| 1196 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1197 | // FIXME: Traverse bases here too. |
| 1198 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1199 | // Structure types are passed in register if all fields would be |
| 1200 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1201 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1202 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1203 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1204 | continue; |
| 1205 | |
| 1206 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1207 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1208 | return false; |
| 1209 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1210 | return true; |
| 1211 | } |
| 1212 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1213 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1214 | // Treat complex types as the element type. |
| 1215 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1216 | Ty = CTy->getElementType(); |
| 1217 | |
| 1218 | // Check for a type which we know has a simple scalar argument-passing |
| 1219 | // convention without any padding. (We're specifically looking for 32 |
| 1220 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1221 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1222 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1223 | return false; |
| 1224 | |
| 1225 | uint64_t Size = Context.getTypeSize(Ty); |
| 1226 | return Size == 32 || Size == 64; |
| 1227 | } |
| 1228 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1229 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1230 | uint64_t &Size) { |
| 1231 | for (const auto *FD : RD->fields()) { |
| 1232 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1233 | // argument is smaller than 32-bits, expanding the struct will create |
| 1234 | // alignment padding. |
| 1235 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1236 | return false; |
| 1237 | |
| 1238 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1239 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1240 | // counts as "basic" is more complicated than what we were doing previously. |
| 1241 | if (FD->isBitField()) |
| 1242 | return false; |
| 1243 | |
| 1244 | Size += Context.getTypeSize(FD->getType()); |
| 1245 | } |
| 1246 | return true; |
| 1247 | } |
| 1248 | |
| 1249 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1250 | uint64_t &Size) { |
| 1251 | // Don't do this if there are any non-empty bases. |
| 1252 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1253 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1254 | Size)) |
| 1255 | return false; |
| 1256 | } |
| 1257 | if (!addFieldSizes(Context, RD, Size)) |
| 1258 | return false; |
| 1259 | return true; |
| 1260 | } |
| 1261 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1262 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1263 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1264 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1265 | /// optimizations. |
| 1266 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1267 | // We can only expand structure types. |
| 1268 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1269 | if (!RT) |
| 1270 | return false; |
| 1271 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1272 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1273 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1274 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1275 | // On non-Windows, we have to conservatively match our old bitcode |
| 1276 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1277 | if (!CXXRD->isCLike()) |
| 1278 | return false; |
| 1279 | } else { |
| 1280 | // Don't do this for dynamic classes. |
| 1281 | if (CXXRD->isDynamicClass()) |
| 1282 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1283 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1284 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1285 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1286 | } else { |
| 1287 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1288 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | // We can do this if there was no alignment padding. |
| 1292 | return Size == getContext().getTypeSize(Ty); |
| 1293 | } |
| 1294 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1295 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1296 | // If the return value is indirect, then the hidden argument is consuming one |
| 1297 | // integer register. |
| 1298 | if (State.FreeRegs) { |
| 1299 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1300 | if (!IsMCUABI) |
| 1301 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1302 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1303 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1306 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1307 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1308 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1309 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1310 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1311 | const Type *Base = nullptr; |
| 1312 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1313 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1314 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1315 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1316 | // The LLVM struct type for such an aggregate should lower properly. |
| 1317 | return ABIArgInfo::getDirect(); |
| 1318 | } |
| 1319 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1320 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1321 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1322 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1323 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1324 | |
| 1325 | // 128-bit vectors are a special case; they are returned in |
| 1326 | // registers and we need to make sure to pick a type the LLVM |
| 1327 | // backend will like. |
| 1328 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1329 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1330 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1331 | |
| 1332 | // Always return in register if it fits in a general purpose |
| 1333 | // register, or if it is 64 bits and has a single element. |
| 1334 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1335 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1336 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1337 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1338 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1339 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1343 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1344 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1345 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1346 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1347 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1348 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1349 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1350 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1351 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1352 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1353 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1354 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1355 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1356 | // Ignore empty structs/unions. |
| 1357 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1358 | return ABIArgInfo::getIgnore(); |
| 1359 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1360 | // Small structures which are register sized are generally returned |
| 1361 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1362 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1363 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1364 | |
| 1365 | // As a special-case, if the struct is a "single-element" struct, and |
| 1366 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1367 | // floating-point register. (MSVC does not apply this special case.) |
| 1368 | // We apply a similar transformation for pointer types to improve the |
| 1369 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1370 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1371 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1372 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1373 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1374 | |
| 1375 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1376 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1377 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1380 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1381 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1382 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1383 | // Treat an enum type as its underlying type. |
| 1384 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1385 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1386 | |
| 1387 | return (RetTy->isPromotableIntegerType() ? |
| 1388 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1391 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1392 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1393 | } |
| 1394 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1395 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1396 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1397 | if (!RT) |
| 1398 | return 0; |
| 1399 | const RecordDecl *RD = RT->getDecl(); |
| 1400 | |
| 1401 | // If this is a C++ record, check the bases first. |
| 1402 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1403 | for (const auto &I : CXXRD->bases()) |
| 1404 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1405 | return false; |
| 1406 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1407 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1408 | QualType FT = i->getType(); |
| 1409 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1410 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1411 | return true; |
| 1412 | |
| 1413 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1414 | return true; |
| 1415 | } |
| 1416 | |
| 1417 | return false; |
| 1418 | } |
| 1419 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1420 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1421 | unsigned Align) const { |
| 1422 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1423 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1424 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1425 | return 0; // Use default alignment. |
| 1426 | |
| 1427 | // On non-Darwin, the stack type alignment is always 4. |
| 1428 | if (!IsDarwinVectorABI) { |
| 1429 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1430 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1431 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1432 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1433 | // 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] | 1434 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1435 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1436 | return 16; |
| 1437 | |
| 1438 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1441 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1442 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1443 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1444 | if (State.FreeRegs) { |
| 1445 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1446 | if (!IsMCUABI) |
| 1447 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1448 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1449 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1450 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1451 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1452 | // Compute the byval alignment. |
| 1453 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1454 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1455 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1456 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1457 | |
| 1458 | // If the stack alignment is less than the type alignment, realign the |
| 1459 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1460 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1461 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1462 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1465 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1466 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1467 | if (!T) |
| 1468 | T = Ty.getTypePtr(); |
| 1469 | |
| 1470 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1471 | BuiltinType::Kind K = BT->getKind(); |
| 1472 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1473 | return Float; |
| 1474 | } |
| 1475 | return Integer; |
| 1476 | } |
| 1477 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1478 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1479 | if (!IsSoftFloatABI) { |
| 1480 | Class C = classify(Ty); |
| 1481 | if (C == Float) |
| 1482 | return false; |
| 1483 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1484 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1485 | unsigned Size = getContext().getTypeSize(Ty); |
| 1486 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1487 | |
| 1488 | if (SizeInRegs == 0) |
| 1489 | return false; |
| 1490 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1491 | if (!IsMCUABI) { |
| 1492 | if (SizeInRegs > State.FreeRegs) { |
| 1493 | State.FreeRegs = 0; |
| 1494 | return false; |
| 1495 | } |
| 1496 | } else { |
| 1497 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1498 | // earlier parameters that are passed on the stack. Also, |
| 1499 | // it does not allow passing >8-byte structs in-register, |
| 1500 | // even if there are 3 free registers available. |
| 1501 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1502 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1503 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1504 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1505 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1506 | return true; |
| 1507 | } |
| 1508 | |
| 1509 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
| 1510 | bool &InReg, |
| 1511 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1512 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1513 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1514 | // (HFAs) have already been dealt with at this point. |
| 1515 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1516 | return false; |
| 1517 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1518 | NeedsPadding = false; |
| 1519 | InReg = !IsMCUABI; |
| 1520 | |
| 1521 | if (!updateFreeRegs(Ty, State)) |
| 1522 | return false; |
| 1523 | |
| 1524 | if (IsMCUABI) |
| 1525 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1526 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1527 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1528 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1529 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1530 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1531 | NeedsPadding = true; |
| 1532 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1533 | return false; |
| 1534 | } |
| 1535 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1536 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1539 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1540 | if (!updateFreeRegs(Ty, State)) |
| 1541 | return false; |
| 1542 | |
| 1543 | if (IsMCUABI) |
| 1544 | return false; |
| 1545 | |
| 1546 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1547 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1548 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1549 | if (getContext().getTypeSize(Ty) > 32) |
| 1550 | return false; |
| 1551 | |
| 1552 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
| 1553 | Ty->isReferenceType()); |
| 1554 | } |
| 1555 | |
| 1556 | return true; |
| 1557 | } |
| 1558 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1559 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1560 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1561 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1562 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1563 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1564 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1565 | // Check with the C++ ABI first. |
| 1566 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1567 | if (RT) { |
| 1568 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1569 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1570 | return getIndirectResult(Ty, false, State); |
| 1571 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1572 | // The field index doesn't matter, we'll fix it up later. |
| 1573 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1574 | } |
| 1575 | } |
| 1576 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1577 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1578 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1579 | const Type *Base = nullptr; |
| 1580 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1581 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1582 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1583 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1584 | if (State.FreeSSERegs >= NumElts) { |
| 1585 | State.FreeSSERegs -= NumElts; |
| 1586 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1587 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1588 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1589 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1590 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1594 | // Structures with flexible arrays are always indirect. |
| 1595 | // FIXME: This should not be byval! |
| 1596 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1597 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1598 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1599 | // Ignore empty structs/unions on non-Windows. |
| 1600 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1601 | return ABIArgInfo::getIgnore(); |
| 1602 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1603 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1604 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1605 | bool NeedsPadding = false; |
| 1606 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1607 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1608 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1609 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1610 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1611 | if (InReg) |
| 1612 | return ABIArgInfo::getDirectInReg(Result); |
| 1613 | else |
| 1614 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1615 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1616 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1617 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1618 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1619 | // of those arguments will match the struct. This is important because the |
| 1620 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1621 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1622 | // Don't do this for the MCU if there are still free integer registers |
| 1623 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1624 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1625 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1626 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1627 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1628 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1629 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1630 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1631 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1632 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1635 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1636 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1637 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1638 | if (IsDarwinVectorABI) { |
| 1639 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1640 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1641 | (Size == 64 && VT->getNumElements() == 1)) |
| 1642 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1643 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1644 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1645 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1646 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1647 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1648 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1649 | return ABIArgInfo::getDirect(); |
| 1650 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1651 | |
| 1652 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1653 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1654 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1655 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1656 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1657 | |
| 1658 | if (Ty->isPromotableIntegerType()) { |
| 1659 | if (InReg) |
| 1660 | return ABIArgInfo::getExtendInReg(); |
| 1661 | return ABIArgInfo::getExtend(); |
| 1662 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1663 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1664 | if (InReg) |
| 1665 | return ABIArgInfo::getDirectInReg(); |
| 1666 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1669 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1670 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1671 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1672 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1673 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1674 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1675 | // registers. |
| 1676 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1677 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1678 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1679 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1680 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1681 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1682 | // First pass do all the vector types. |
| 1683 | const Type *Base = nullptr; |
| 1684 | uint64_t NumElts = 0; |
| 1685 | const QualType& Ty = I.type; |
| 1686 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1687 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1688 | if (State.FreeSSERegs >= NumElts) { |
| 1689 | State.FreeSSERegs -= NumElts; |
| 1690 | I.info = ABIArgInfo::getDirect(); |
| 1691 | } else { |
| 1692 | I.info = classifyArgumentType(Ty, State); |
| 1693 | } |
| 1694 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1695 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1696 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1697 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1698 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1699 | // Second pass, do the rest! |
| 1700 | const Type *Base = nullptr; |
| 1701 | uint64_t NumElts = 0; |
| 1702 | const QualType& Ty = I.type; |
| 1703 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1704 | |
| 1705 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1706 | // Assign true HVAs (non vector/native FP types). |
| 1707 | if (State.FreeSSERegs >= NumElts) { |
| 1708 | State.FreeSSERegs -= NumElts; |
| 1709 | I.info = getDirectX86Hva(); |
| 1710 | } else { |
| 1711 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1712 | } |
| 1713 | } else if (!IsHva) { |
| 1714 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1715 | I.info = classifyArgumentType(Ty, State); |
| 1716 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1717 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1718 | } |
| 1719 | } |
| 1720 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1721 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1722 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1723 | if (IsMCUABI) |
| 1724 | State.FreeRegs = 3; |
| 1725 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1726 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1727 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1728 | State.FreeRegs = 2; |
| 1729 | State.FreeSSERegs = 6; |
| 1730 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1731 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1732 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1733 | State.FreeRegs = 5; |
| 1734 | State.FreeSSERegs = 8; |
| 1735 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1736 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1737 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1738 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1739 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1740 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1741 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1742 | // return value was sret and put it in a register ourselves if appropriate. |
| 1743 | if (State.FreeRegs) { |
| 1744 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1745 | if (!IsMCUABI) |
| 1746 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1747 | } |
| 1748 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1749 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1750 | // The chain argument effectively gives us another free register. |
| 1751 | if (FI.isChainCall()) |
| 1752 | ++State.FreeRegs; |
| 1753 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1754 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1755 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1756 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1757 | } else { |
| 1758 | // If not vectorcall, revert to normal behavior. |
| 1759 | for (auto &I : FI.arguments()) { |
| 1760 | I.info = classifyArgumentType(I.type, State); |
| 1761 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1762 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1766 | // all the memory arguments to use inalloca. |
| 1767 | if (UsedInAlloca) |
| 1768 | rewriteWithInAlloca(FI); |
| 1769 | } |
| 1770 | |
| 1771 | void |
| 1772 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1773 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1774 | QualType Type) const { |
| 1775 | // Arguments are always 4-byte-aligned. |
| 1776 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1777 | |
| 1778 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1779 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1780 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1781 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1782 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1783 | // Insert padding bytes to respect alignment. |
| 1784 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1785 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1786 | if (StackOffset != FieldEnd) { |
| 1787 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1788 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1789 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1790 | FrameFields.push_back(Ty); |
| 1791 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1794 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1795 | // Leave ignored and inreg arguments alone. |
| 1796 | switch (Info.getKind()) { |
| 1797 | case ABIArgInfo::InAlloca: |
| 1798 | return true; |
| 1799 | case ABIArgInfo::Indirect: |
| 1800 | assert(Info.getIndirectByVal()); |
| 1801 | return true; |
| 1802 | case ABIArgInfo::Ignore: |
| 1803 | return false; |
| 1804 | case ABIArgInfo::Direct: |
| 1805 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1806 | if (Info.getInReg()) |
| 1807 | return false; |
| 1808 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1809 | case ABIArgInfo::Expand: |
| 1810 | case ABIArgInfo::CoerceAndExpand: |
| 1811 | // These are aggregate types which are never passed in registers when |
| 1812 | // inalloca is involved. |
| 1813 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1814 | } |
| 1815 | llvm_unreachable("invalid enum"); |
| 1816 | } |
| 1817 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1818 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1819 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1820 | |
| 1821 | // Build a packed struct type for all of the arguments in memory. |
| 1822 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1823 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1824 | // The stack alignment is always 4. |
| 1825 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1826 | |
| 1827 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1828 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1829 | |
| 1830 | // Put 'this' into the struct before 'sret', if necessary. |
| 1831 | bool IsThisCall = |
| 1832 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1833 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1834 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1835 | isArgInAlloca(I->info)) { |
| 1836 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1837 | ++I; |
| 1838 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1839 | |
| 1840 | // 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] | 1841 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1842 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1843 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1844 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1845 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1849 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1850 | ++I; |
| 1851 | |
| 1852 | // Put arguments passed in memory into the struct. |
| 1853 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1854 | if (isArgInAlloca(I->info)) |
| 1855 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1859 | /*isPacked=*/true), |
| 1860 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1863 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1864 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1865 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1866 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1867 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1868 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1869 | // |
| 1870 | // Just messing with TypeInfo like this works because we never pass |
| 1871 | // anything indirectly. |
| 1872 | TypeInfo.second = CharUnits::fromQuantity( |
| 1873 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1874 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1875 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1876 | TypeInfo, CharUnits::fromQuantity(4), |
| 1877 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1880 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1881 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1882 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1883 | |
| 1884 | switch (Opts.getStructReturnConvention()) { |
| 1885 | case CodeGenOptions::SRCK_Default: |
| 1886 | break; |
| 1887 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1888 | return false; |
| 1889 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1890 | return true; |
| 1891 | } |
| 1892 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1893 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1894 | return true; |
| 1895 | |
| 1896 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1897 | case llvm::Triple::DragonFly: |
| 1898 | case llvm::Triple::FreeBSD: |
| 1899 | case llvm::Triple::OpenBSD: |
| 1900 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1901 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1902 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1903 | default: |
| 1904 | return false; |
| 1905 | } |
| 1906 | } |
| 1907 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 1908 | void X86_32TargetCodeGenInfo::setTargetAttributes( |
| 1909 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 1910 | ForDefinition_t IsForDefinition) const { |
| 1911 | if (!IsForDefinition) |
| 1912 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1913 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1914 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1915 | // Get the LLVM function. |
| 1916 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1917 | |
| 1918 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1919 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1920 | B.addStackAlignmentAttr(16); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1921 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1922 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1923 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1924 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1925 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1926 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1930 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1931 | CodeGen::CodeGenFunction &CGF, |
| 1932 | llvm::Value *Address) const { |
| 1933 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1934 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1935 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1936 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1937 | // 0-7 are the eight integer registers; the order is different |
| 1938 | // on Darwin (for EH), but the range is the same. |
| 1939 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1940 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1941 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1942 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1943 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1944 | // These have size 16, which is sizeof(long double) on |
| 1945 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1946 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1947 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1948 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1949 | } else { |
| 1950 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1951 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1952 | Builder.CreateAlignedStore( |
| 1953 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1954 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1955 | |
| 1956 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1957 | // These have size 12, which is sizeof(long double) on |
| 1958 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1959 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1960 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1961 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1962 | |
| 1963 | return false; |
| 1964 | } |
| 1965 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1966 | //===----------------------------------------------------------------------===// |
| 1967 | // X86-64 ABI Implementation |
| 1968 | //===----------------------------------------------------------------------===// |
| 1969 | |
| 1970 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1971 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1972 | /// The AVX ABI level for X86 targets. |
| 1973 | enum class X86AVXABILevel { |
| 1974 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1975 | AVX, |
| 1976 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1977 | }; |
| 1978 | |
| 1979 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1980 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1981 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1982 | case X86AVXABILevel::AVX512: |
| 1983 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1984 | case X86AVXABILevel::AVX: |
| 1985 | return 256; |
| 1986 | case X86AVXABILevel::None: |
| 1987 | return 128; |
| 1988 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1989 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1992 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1993 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1994 | enum Class { |
| 1995 | Integer = 0, |
| 1996 | SSE, |
| 1997 | SSEUp, |
| 1998 | X87, |
| 1999 | X87Up, |
| 2000 | ComplexX87, |
| 2001 | NoClass, |
| 2002 | Memory |
| 2003 | }; |
| 2004 | |
| 2005 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2006 | /// |
| 2007 | /// Merge an accumulating classification \arg Accum with a field |
| 2008 | /// classification \arg Field. |
| 2009 | /// |
| 2010 | /// \param Accum - The accumulating classification. This should |
| 2011 | /// always be either NoClass or the result of a previous merge |
| 2012 | /// call. In addition, this should never be Memory (the caller |
| 2013 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2014 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2015 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2016 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2017 | /// |
| 2018 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2019 | /// final MEMORY or SSE classes when necessary. |
| 2020 | /// |
| 2021 | /// \param AggregateSize - The size of the current aggregate in |
| 2022 | /// the classification process. |
| 2023 | /// |
| 2024 | /// \param Lo - The classification for the parts of the type |
| 2025 | /// residing in the low word of the containing object. |
| 2026 | /// |
| 2027 | /// \param Hi - The classification for the parts of the type |
| 2028 | /// residing in the higher words of the containing object. |
| 2029 | /// |
| 2030 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2031 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2032 | /// classify - Determine the x86_64 register classes in which the |
| 2033 | /// given type T should be passed. |
| 2034 | /// |
| 2035 | /// \param Lo - The classification for the parts of the type |
| 2036 | /// residing in the low word of the containing object. |
| 2037 | /// |
| 2038 | /// \param Hi - The classification for the parts of the type |
| 2039 | /// residing in the high word of the containing object. |
| 2040 | /// |
| 2041 | /// \param OffsetBase - The bit offset of this type in the |
| 2042 | /// containing object. Some parameters are classified different |
| 2043 | /// depending on whether they straddle an eightbyte boundary. |
| 2044 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2045 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2046 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2047 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2048 | /// If a word is unused its result will be NoClass; if a type should |
| 2049 | /// be passed in Memory then at least the classification of \arg Lo |
| 2050 | /// will be Memory. |
| 2051 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2052 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2053 | /// |
| 2054 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2055 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2056 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2057 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2058 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2059 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2060 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2061 | unsigned IROffset, QualType SourceTy, |
| 2062 | unsigned SourceOffset) const; |
| 2063 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2064 | unsigned IROffset, QualType SourceTy, |
| 2065 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2066 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2067 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2068 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2069 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2070 | |
| 2071 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2072 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2073 | /// |
| 2074 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2075 | /// available. |
| 2076 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2077 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2078 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2079 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2080 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2081 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2082 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2083 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2084 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2085 | unsigned &NeededSSE) const; |
| 2086 | |
| 2087 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2088 | unsigned &NeededSSE) const; |
| 2089 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2090 | bool IsIllegalVectorType(QualType Ty) const; |
| 2091 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2092 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2093 | /// unfortunately in ways that were not always consistent with |
| 2094 | /// certain previous compilers. In particular, platforms which |
| 2095 | /// required strict binary compatibility with older versions of GCC |
| 2096 | /// may need to exempt themselves. |
| 2097 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2098 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2101 | /// GCC classifies <1 x long long> as SSE but compatibility with older clang |
| 2102 | // compilers require us to classify it as INTEGER. |
| 2103 | bool classifyIntegerMMXAsSSE() const { |
| 2104 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2105 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2106 | return false; |
| 2107 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2108 | return false; |
| 2109 | return true; |
| 2110 | } |
| 2111 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2112 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2113 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2114 | // 64-bit hardware. |
| 2115 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2116 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2117 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2118 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2119 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2120 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2121 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2122 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2123 | bool isPassedUsingAVXType(QualType type) const { |
| 2124 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2125 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2126 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2127 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2128 | if (info.isDirect()) { |
| 2129 | llvm::Type *ty = info.getCoerceToType(); |
| 2130 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2131 | return (vectorTy->getBitWidth() > 128); |
| 2132 | } |
| 2133 | return false; |
| 2134 | } |
| 2135 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2136 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2137 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2138 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2139 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2140 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2141 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2142 | |
| 2143 | bool has64BitPointers() const { |
| 2144 | return Has64BitPointers; |
| 2145 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2146 | |
| 2147 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 2148 | ArrayRef<llvm::Type*> scalars, |
| 2149 | bool asReturnValue) const override { |
| 2150 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2151 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2152 | bool isSwiftErrorInRegister() const override { |
| 2153 | return true; |
| 2154 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2155 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2156 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2157 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2158 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2159 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2160 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2161 | : SwiftABIInfo(CGT), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2162 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2163 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2164 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2165 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2166 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2167 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2168 | |
| 2169 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2170 | // FIXME: Assumes vectorcall is in use. |
| 2171 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2172 | } |
| 2173 | |
| 2174 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2175 | uint64_t NumMembers) const override { |
| 2176 | // FIXME: Assumes vectorcall is in use. |
| 2177 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2178 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2179 | |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2180 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 2181 | ArrayRef<llvm::Type *> scalars, |
| 2182 | bool asReturnValue) const override { |
| 2183 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2184 | } |
| 2185 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2186 | bool isSwiftErrorInRegister() const override { |
| 2187 | return true; |
| 2188 | } |
| 2189 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2190 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2191 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2192 | bool IsVectorCall, bool IsRegCall) const; |
| 2193 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2194 | const ABIArgInfo ¤t) const; |
| 2195 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2196 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2197 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2198 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2199 | }; |
| 2200 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2201 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2202 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2203 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2204 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2205 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2206 | const X86_64ABIInfo &getABIInfo() const { |
| 2207 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2208 | } |
| 2209 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2210 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2211 | return 7; |
| 2212 | } |
| 2213 | |
| 2214 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2215 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2216 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2217 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2218 | // 0-15 are the 16 integer registers. |
| 2219 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2220 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2221 | return false; |
| 2222 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2223 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2224 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2225 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2226 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2227 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2228 | } |
| 2229 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2230 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2231 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2232 | // The default CC on x86-64 sets %al to the number of SSA |
| 2233 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2234 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2235 | // that when AVX types are involved: the ABI explicitly states it is |
| 2236 | // undefined, and it doesn't work in practice because of how the ABI |
| 2237 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2238 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2239 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2240 | for (CallArgList::const_iterator |
| 2241 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2242 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2243 | HasAVXType = true; |
| 2244 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2245 | } |
| 2246 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2247 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2248 | if (!HasAVXType) |
| 2249 | return true; |
| 2250 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2251 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2252 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2255 | llvm::Constant * |
| 2256 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2257 | unsigned Sig; |
| 2258 | if (getABIInfo().has64BitPointers()) |
| 2259 | Sig = (0xeb << 0) | // jmp rel8 |
| 2260 | (0x0a << 8) | // .+0x0c |
| 2261 | ('F' << 16) | |
| 2262 | ('T' << 24); |
| 2263 | else |
| 2264 | Sig = (0xeb << 0) | // jmp rel8 |
| 2265 | (0x06 << 8) | // .+0x08 |
| 2266 | ('F' << 16) | |
| 2267 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2268 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2269 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2270 | |
| 2271 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 2272 | CodeGen::CodeGenModule &CGM, |
| 2273 | ForDefinition_t IsForDefinition) const override { |
| 2274 | if (!IsForDefinition) |
| 2275 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2276 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2277 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2278 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2279 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2280 | } |
| 2281 | } |
| 2282 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2283 | }; |
| 2284 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2285 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2286 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2287 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2288 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2289 | |
| 2290 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 2291 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2292 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 2293 | // If the argument contains a space, enclose it in quotes. |
| 2294 | if (Lib.find(" ") != StringRef::npos) |
| 2295 | Opt += "\"" + Lib.str() + "\""; |
| 2296 | else |
| 2297 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2298 | } |
| 2299 | }; |
| 2300 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2301 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2302 | // If the argument does not end in .lib, automatically add the suffix. |
| 2303 | // If the argument contains a space, enclose it in quotes. |
| 2304 | // This matches the behavior of MSVC. |
| 2305 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2306 | std::string ArgStr = Quote ? "\"" : ""; |
| 2307 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 2308 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2309 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2310 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2311 | return ArgStr; |
| 2312 | } |
| 2313 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2314 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2315 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2316 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2317 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2318 | unsigned NumRegisterParameters) |
| 2319 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2320 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2321 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2322 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 2323 | CodeGen::CodeGenModule &CGM, |
| 2324 | ForDefinition_t IsForDefinition) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2325 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2326 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2327 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2328 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2329 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2330 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2331 | |
| 2332 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2333 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2334 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2335 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2336 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2337 | }; |
| 2338 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2339 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 2340 | llvm::GlobalValue *GV, |
| 2341 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 2342 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2343 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 2344 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2345 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2346 | Fn->addFnAttr("stack-probe-size", |
| 2347 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2348 | } |
| 2349 | } |
| 2350 | } |
| 2351 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 2352 | void WinX86_32TargetCodeGenInfo::setTargetAttributes( |
| 2353 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 2354 | ForDefinition_t IsForDefinition) const { |
| 2355 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition); |
| 2356 | if (!IsForDefinition) |
| 2357 | return; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2358 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2359 | } |
| 2360 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2361 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2362 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2363 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2364 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2365 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2366 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2367 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 2368 | CodeGen::CodeGenModule &CGM, |
| 2369 | ForDefinition_t IsForDefinition) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2370 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2371 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2372 | return 7; |
| 2373 | } |
| 2374 | |
| 2375 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2376 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2377 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2378 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2379 | // 0-15 are the 16 integer registers. |
| 2380 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2381 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2382 | return false; |
| 2383 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2384 | |
| 2385 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2386 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2387 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2388 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2389 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2390 | |
| 2391 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2392 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2393 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2394 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2395 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2396 | }; |
| 2397 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 2398 | void WinX86_64TargetCodeGenInfo::setTargetAttributes( |
| 2399 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 2400 | ForDefinition_t IsForDefinition) const { |
| 2401 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition); |
| 2402 | if (!IsForDefinition) |
| 2403 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2404 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2405 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2406 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2407 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2408 | } |
| 2409 | } |
| 2410 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2411 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2412 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2413 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2414 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2415 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2416 | Class &Hi) const { |
| 2417 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2418 | // |
| 2419 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2420 | // memory. |
| 2421 | // |
| 2422 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2423 | // memory. |
| 2424 | // |
| 2425 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2426 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2427 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2428 | // ABI working for processors that don't support the __m256 type. |
| 2429 | // |
| 2430 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2431 | // |
| 2432 | // Some of these are enforced by the merging logic. Others can arise |
| 2433 | // only with unions; for example: |
| 2434 | // union { _Complex double; unsigned; } |
| 2435 | // |
| 2436 | // Note that clauses (b) and (c) were added in 0.98. |
| 2437 | // |
| 2438 | if (Hi == Memory) |
| 2439 | Lo = Memory; |
| 2440 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2441 | Lo = Memory; |
| 2442 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2443 | Lo = Memory; |
| 2444 | if (Hi == SSEUp && Lo != SSE) |
| 2445 | Hi = SSE; |
| 2446 | } |
| 2447 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2448 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2449 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2450 | // classified recursively so that always two fields are |
| 2451 | // considered. The resulting class is calculated according to |
| 2452 | // the classes of the fields in the eightbyte: |
| 2453 | // |
| 2454 | // (a) If both classes are equal, this is the resulting class. |
| 2455 | // |
| 2456 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2457 | // the other class. |
| 2458 | // |
| 2459 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2460 | // class. |
| 2461 | // |
| 2462 | // (d) If one of the classes is INTEGER, the result is the |
| 2463 | // INTEGER. |
| 2464 | // |
| 2465 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2466 | // MEMORY is used as class. |
| 2467 | // |
| 2468 | // (f) Otherwise class SSE is used. |
| 2469 | |
| 2470 | // Accum should never be memory (we should have returned) or |
| 2471 | // ComplexX87 (because this cannot be passed in a structure). |
| 2472 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2473 | "Invalid accumulated classification during merge."); |
| 2474 | if (Accum == Field || Field == NoClass) |
| 2475 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2476 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2477 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2478 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2479 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2480 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2481 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2482 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2483 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2484 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2485 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2488 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2489 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2490 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2491 | // Class pairs with appropriate constructor methods for the various |
| 2492 | // situations. |
| 2493 | |
| 2494 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2495 | // shouldn't be passed in registers for example, so there is no chance they |
| 2496 | // can straddle an eightbyte. Verify & simplify. |
| 2497 | |
| 2498 | Lo = Hi = NoClass; |
| 2499 | |
| 2500 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2501 | Current = Memory; |
| 2502 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2503 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2504 | BuiltinType::Kind k = BT->getKind(); |
| 2505 | |
| 2506 | if (k == BuiltinType::Void) { |
| 2507 | Current = NoClass; |
| 2508 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2509 | Lo = Integer; |
| 2510 | Hi = Integer; |
| 2511 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2512 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2513 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2514 | Current = SSE; |
| 2515 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2516 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2517 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2518 | Lo = SSE; |
| 2519 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2520 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2521 | Lo = X87; |
| 2522 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2523 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2524 | Current = SSE; |
| 2525 | } else |
| 2526 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2527 | } |
| 2528 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2529 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2530 | return; |
| 2531 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2532 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2533 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2534 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2535 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2536 | return; |
| 2537 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2538 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2539 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2540 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2541 | return; |
| 2542 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2543 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2544 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2545 | if (Ty->isMemberFunctionPointerType()) { |
| 2546 | if (Has64BitPointers) { |
| 2547 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2548 | // Lo and Hi now. |
| 2549 | Lo = Hi = Integer; |
| 2550 | } else { |
| 2551 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2552 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2553 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2554 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2555 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2556 | Lo = Hi = Integer; |
| 2557 | } else { |
| 2558 | Current = Integer; |
| 2559 | } |
| 2560 | } |
| 2561 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2562 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2563 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2564 | return; |
| 2565 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2566 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2567 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2568 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2569 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2570 | // gcc passes the following as integer: |
| 2571 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2572 | // 2 bytes - <2 x char>, <1 x short> |
| 2573 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2574 | Current = Integer; |
| 2575 | |
| 2576 | // If this type crosses an eightbyte boundary, it should be |
| 2577 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2578 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2579 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2580 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2581 | Hi = Lo; |
| 2582 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2583 | QualType ElementType = VT->getElementType(); |
| 2584 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2585 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2586 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2587 | return; |
| 2588 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2589 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2590 | // pass them as integer. For platforms where clang is the de facto |
| 2591 | // platform compiler, we must continue to use integer. |
| 2592 | if (!classifyIntegerMMXAsSSE() && |
| 2593 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2594 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2595 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2596 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2597 | Current = Integer; |
| 2598 | else |
| 2599 | Current = SSE; |
| 2600 | |
| 2601 | // If this type crosses an eightbyte boundary, it should be |
| 2602 | // split. |
| 2603 | if (OffsetBase && OffsetBase != 64) |
| 2604 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2605 | } else if (Size == 128 || |
| 2606 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2607 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2608 | // least significant one belongs to class SSE and all the others to class |
| 2609 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2610 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2611 | // This design isn't correct for 256-bits, but since there're no cases |
| 2612 | // where the upper parts would need to be inspected, avoid adding |
| 2613 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2614 | // |
| 2615 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2616 | // registers if they are "named", i.e. not part of the "..." of a |
| 2617 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2618 | // |
| 2619 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2620 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2621 | Lo = SSE; |
| 2622 | Hi = SSEUp; |
| 2623 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2624 | return; |
| 2625 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2626 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2627 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2628 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2629 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2630 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2631 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2632 | if (Size <= 64) |
| 2633 | Current = Integer; |
| 2634 | else if (Size <= 128) |
| 2635 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2636 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2637 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2638 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2639 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2640 | } else if (ET == getContext().LongDoubleTy) { |
| 2641 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2642 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2643 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2644 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2645 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2646 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2647 | Lo = Hi = SSE; |
| 2648 | else |
| 2649 | llvm_unreachable("unexpected long double representation!"); |
| 2650 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2651 | |
| 2652 | // If this complex type crosses an eightbyte boundary then it |
| 2653 | // should be split. |
| 2654 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2655 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2656 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2657 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2658 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2659 | return; |
| 2660 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2661 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2662 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2663 | // Arrays are treated like structures. |
| 2664 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2665 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2666 | |
| 2667 | // 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] | 2668 | // than eight eightbytes, ..., it has class MEMORY. |
| 2669 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2670 | return; |
| 2671 | |
| 2672 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2673 | // fields, it has class MEMORY. |
| 2674 | // |
| 2675 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2676 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2677 | return; |
| 2678 | |
| 2679 | // Otherwise implement simplified merge. We could be smarter about |
| 2680 | // this, but it isn't worth it and would be harder to verify. |
| 2681 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2682 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2683 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2684 | |
| 2685 | // The only case a 256-bit wide vector could be used is when the array |
| 2686 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2687 | // 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] | 2688 | // |
| 2689 | if (Size > 128 && |
| 2690 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2691 | return; |
| 2692 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2693 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2694 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2695 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2696 | Lo = merge(Lo, FieldLo); |
| 2697 | Hi = merge(Hi, FieldHi); |
| 2698 | if (Lo == Memory || Hi == Memory) |
| 2699 | break; |
| 2700 | } |
| 2701 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2702 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2703 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2704 | return; |
| 2705 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2706 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2707 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2708 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2709 | |
| 2710 | // 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] | 2711 | // than eight eightbytes, ..., it has class MEMORY. |
| 2712 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2713 | return; |
| 2714 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2715 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2716 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2717 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2718 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2719 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2720 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2721 | const RecordDecl *RD = RT->getDecl(); |
| 2722 | |
| 2723 | // Assume variable sized types are passed in memory. |
| 2724 | if (RD->hasFlexibleArrayMember()) |
| 2725 | return; |
| 2726 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2727 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2728 | |
| 2729 | // Reset Lo class, this will be recomputed. |
| 2730 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2731 | |
| 2732 | // If this is a C++ record, classify the bases first. |
| 2733 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2734 | for (const auto &I : CXXRD->bases()) { |
| 2735 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2736 | "Unexpected base class!"); |
| 2737 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2738 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2739 | |
| 2740 | // Classify this field. |
| 2741 | // |
| 2742 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2743 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2744 | // initialized to class NO_CLASS. |
| 2745 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2746 | uint64_t Offset = |
| 2747 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2748 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2749 | Lo = merge(Lo, FieldLo); |
| 2750 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2751 | if (Lo == Memory || Hi == Memory) { |
| 2752 | postMerge(Size, Lo, Hi); |
| 2753 | return; |
| 2754 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2755 | } |
| 2756 | } |
| 2757 | |
| 2758 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2759 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2760 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2761 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2762 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2763 | bool BitField = i->isBitField(); |
| 2764 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2765 | // Ignore padding bit-fields. |
| 2766 | if (BitField && i->isUnnamedBitfield()) |
| 2767 | continue; |
| 2768 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2769 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2770 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2771 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2772 | // The only case a 256-bit wide vector could be used is when the struct |
| 2773 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2774 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2775 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2776 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2777 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2778 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2779 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2780 | return; |
| 2781 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2782 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2783 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2784 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2785 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2786 | return; |
| 2787 | } |
| 2788 | |
| 2789 | // Classify this field. |
| 2790 | // |
| 2791 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2792 | // exceeds a single eightbyte, each is classified |
| 2793 | // separately. Each eightbyte gets initialized to class |
| 2794 | // NO_CLASS. |
| 2795 | Class FieldLo, FieldHi; |
| 2796 | |
| 2797 | // Bit-fields require special handling, they do not force the |
| 2798 | // structure to be passed in memory even if unaligned, and |
| 2799 | // therefore they can straddle an eightbyte. |
| 2800 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2801 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2802 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2803 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2804 | |
| 2805 | uint64_t EB_Lo = Offset / 64; |
| 2806 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2807 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2808 | if (EB_Lo) { |
| 2809 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2810 | FieldLo = NoClass; |
| 2811 | FieldHi = Integer; |
| 2812 | } else { |
| 2813 | FieldLo = Integer; |
| 2814 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2815 | } |
| 2816 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2817 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2818 | Lo = merge(Lo, FieldLo); |
| 2819 | Hi = merge(Hi, FieldHi); |
| 2820 | if (Lo == Memory || Hi == Memory) |
| 2821 | break; |
| 2822 | } |
| 2823 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2824 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2825 | } |
| 2826 | } |
| 2827 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2828 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2829 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2830 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2831 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2832 | // Treat an enum type as its underlying type. |
| 2833 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2834 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2835 | |
| 2836 | return (Ty->isPromotableIntegerType() ? |
| 2837 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2838 | } |
| 2839 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2840 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2843 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2844 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2845 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2846 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2847 | if (Size <= 64 || Size > LargestVector) |
| 2848 | return true; |
| 2849 | } |
| 2850 | |
| 2851 | return false; |
| 2852 | } |
| 2853 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2854 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2855 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2856 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2857 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2858 | // |
| 2859 | // This assumption is optimistic, as there could be free registers available |
| 2860 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2861 | // the argument in the free register. This does not seem to happen currently, |
| 2862 | // but this code would be much safer if we could mark the argument with |
| 2863 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2864 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2865 | // Treat an enum type as its underlying type. |
| 2866 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2867 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2868 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2869 | return (Ty->isPromotableIntegerType() ? |
| 2870 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2871 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2872 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2873 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2874 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2875 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2876 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2877 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2878 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2879 | |
| 2880 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2881 | // is important for good codegen. |
| 2882 | // |
| 2883 | // We do this by coercing the value into a scalar type which the backend can |
| 2884 | // handle naturally (i.e., without using byval). |
| 2885 | // |
| 2886 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2887 | // free integer registers. Doing this when there are free integer registers |
| 2888 | // would require more care, as we would have to ensure that the coerced value |
| 2889 | // did not claim the unused register. That would require either reording the |
| 2890 | // arguments to the function (so that any subsequent inreg values came first), |
| 2891 | // or only doing this optimization when there were no following arguments that |
| 2892 | // might be inreg. |
| 2893 | // |
| 2894 | // We currently expect it to be rare (particularly in well written code) for |
| 2895 | // arguments to be passed on the stack when there are still free integer |
| 2896 | // registers available (this would typically imply large structs being passed |
| 2897 | // by value), so this seems like a fair tradeoff for now. |
| 2898 | // |
| 2899 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2900 | // attributes. See PR12193. |
| 2901 | if (freeIntRegs == 0) { |
| 2902 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2903 | |
| 2904 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2905 | // type, which will end up on the stack (with alignment 8). |
| 2906 | if (Align == 8 && Size <= 64) |
| 2907 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2908 | Size)); |
| 2909 | } |
| 2910 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2911 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2914 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2915 | /// 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] | 2916 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2917 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2918 | // vectors; strip them off if present. |
| 2919 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2920 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2921 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2922 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2923 | if (isa<llvm::VectorType>(IRType) || |
| 2924 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2925 | return IRType; |
| 2926 | |
| 2927 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2928 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2929 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2930 | |
| 2931 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2932 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2933 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2936 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2937 | /// is known to either be off the end of the specified type or being in |
| 2938 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2939 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2940 | /// classification that put one of the two halves in the INTEGER class. |
| 2941 | /// |
| 2942 | /// It is conservatively correct to return false. |
| 2943 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2944 | unsigned EndBit, ASTContext &Context) { |
| 2945 | // If the bytes being queried are off the end of the type, there is no user |
| 2946 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2947 | // types that don't contain interesting padding. |
| 2948 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2949 | if (TySize <= StartBit) |
| 2950 | return true; |
| 2951 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2952 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2953 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2954 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2955 | |
| 2956 | // Check each element to see if the element overlaps with the queried range. |
| 2957 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2958 | // If the element is after the span we care about, then we're done.. |
| 2959 | unsigned EltOffset = i*EltSize; |
| 2960 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2961 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2962 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2963 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2964 | EndBit-EltOffset, Context)) |
| 2965 | return false; |
| 2966 | } |
| 2967 | // If it overlaps no elements, then it is safe to process as padding. |
| 2968 | return true; |
| 2969 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2970 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2971 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2972 | const RecordDecl *RD = RT->getDecl(); |
| 2973 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2974 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2975 | // If this is a C++ record, check the bases first. |
| 2976 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2977 | for (const auto &I : CXXRD->bases()) { |
| 2978 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2979 | "Unexpected base class!"); |
| 2980 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2981 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2982 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2983 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2984 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2985 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2986 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2987 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2988 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2989 | EndBit-BaseOffset, Context)) |
| 2990 | return false; |
| 2991 | } |
| 2992 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2993 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2994 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2995 | // this could be sped up a lot by being smarter about queried fields, |
| 2996 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2997 | // much. |
| 2998 | unsigned idx = 0; |
| 2999 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3000 | i != e; ++i, ++idx) { |
| 3001 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3002 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3003 | // If we found a field after the region we care about, then we're done. |
| 3004 | if (FieldOffset >= EndBit) break; |
| 3005 | |
| 3006 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 3007 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 3008 | Context)) |
| 3009 | return false; |
| 3010 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3011 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3012 | // If nothing in this record overlapped the area of interest, then we're |
| 3013 | // clean. |
| 3014 | return true; |
| 3015 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3016 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3017 | return false; |
| 3018 | } |
| 3019 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3020 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3021 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3022 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3023 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3024 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3025 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3026 | // Base case if we find a float. |
| 3027 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3028 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3029 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3030 | // 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] | 3031 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3032 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3033 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3034 | IROffset -= SL->getElementOffset(Elt); |
| 3035 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3036 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3037 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3038 | // 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] | 3039 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3040 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3041 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3042 | IROffset -= IROffset/EltSize*EltSize; |
| 3043 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3044 | } |
| 3045 | |
| 3046 | return false; |
| 3047 | } |
| 3048 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3049 | |
| 3050 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3051 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3052 | llvm::Type *X86_64ABIInfo:: |
| 3053 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3054 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3055 | // 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] | 3056 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3057 | // structs that contain 3 floats. |
| 3058 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3059 | SourceOffset*8+64, getContext())) |
| 3060 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3061 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3062 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3063 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3064 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3065 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3066 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3067 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3068 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3069 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3070 | } |
| 3071 | |
| 3072 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3073 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3074 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3075 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3076 | /// 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] | 3077 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3078 | /// etc). |
| 3079 | /// |
| 3080 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3081 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3082 | /// the 8-byte value references. PrefType may be null. |
| 3083 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3084 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3085 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3086 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3087 | llvm::Type *X86_64ABIInfo:: |
| 3088 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3089 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3090 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3091 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3092 | if (IROffset == 0) { |
| 3093 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3094 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3095 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3096 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3097 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3098 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3099 | // goodness in the source type is just tail padding. This is allowed to |
| 3100 | // kick in for struct {double,int} on the int, but not on |
| 3101 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3102 | // have to do this analysis on the source type because we can't depend on |
| 3103 | // unions being lowered a specific way etc. |
| 3104 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3105 | IRType->isIntegerTy(32) || |
| 3106 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3107 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3108 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3109 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3110 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3111 | SourceOffset*8+64, getContext())) |
| 3112 | return IRType; |
| 3113 | } |
| 3114 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3115 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3116 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3117 | // 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] | 3118 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3119 | if (IROffset < SL->getSizeInBytes()) { |
| 3120 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3121 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3122 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3123 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3124 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3125 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3126 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3127 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3128 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3129 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3130 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3131 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3132 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3133 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3134 | } |
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 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3137 | // 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] | 3138 | unsigned TySizeInBytes = |
| 3139 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3140 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3141 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3142 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3143 | // It is always safe to classify this as an integer type up to i64 that |
| 3144 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3145 | return llvm::IntegerType::get(getVMContext(), |
| 3146 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3149 | |
| 3150 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3151 | /// be used as elements of a two register pair to pass or return, return a |
| 3152 | /// first class aggregate to represent them. For example, if the low part of |
| 3153 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3154 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3155 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3156 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3157 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3158 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3159 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3160 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3161 | // the second element at offset 8. Check for this: |
| 3162 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3163 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3164 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3165 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3166 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3167 | // To handle this, we have to increase the size of the low part so that the |
| 3168 | // second element will start at an 8 byte offset. We can't increase the size |
| 3169 | // of the second element because it might make us access off the end of the |
| 3170 | // struct. |
| 3171 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3172 | // There are usually two sorts of types the ABI generation code can produce |
| 3173 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3174 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3175 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3176 | // Promote these to a larger type. |
| 3177 | if (Lo->isFloatTy()) |
| 3178 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3179 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3180 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3181 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3182 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3183 | } |
| 3184 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3185 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3186 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3187 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3188 | // Verify that the second element is at an 8-byte offset. |
| 3189 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3190 | "Invalid x86-64 argument pair!"); |
| 3191 | return Result; |
| 3192 | } |
| 3193 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3194 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3195 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3196 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3197 | // classification algorithm. |
| 3198 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3199 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3200 | |
| 3201 | // Check some invariants. |
| 3202 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3203 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3204 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3205 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3206 | switch (Lo) { |
| 3207 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3208 | if (Hi == NoClass) |
| 3209 | return ABIArgInfo::getIgnore(); |
| 3210 | // If the low part is just padding, it takes no register, leave ResType |
| 3211 | // null. |
| 3212 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3213 | "Unknown missing lo part"); |
| 3214 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3215 | |
| 3216 | case SSEUp: |
| 3217 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3218 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3219 | |
| 3220 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3221 | // hidden argument. |
| 3222 | case Memory: |
| 3223 | return getIndirectReturnResult(RetTy); |
| 3224 | |
| 3225 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3226 | // available register of the sequence %rax, %rdx is used. |
| 3227 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3228 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3229 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3230 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3231 | // so that the parameter gets the right LLVM IR attributes. |
| 3232 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3233 | // Treat an enum type as its underlying type. |
| 3234 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3235 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3236 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3237 | if (RetTy->isIntegralOrEnumerationType() && |
| 3238 | RetTy->isPromotableIntegerType()) |
| 3239 | return ABIArgInfo::getExtend(); |
| 3240 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3241 | break; |
| 3242 | |
| 3243 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3244 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3245 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3246 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3247 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3248 | |
| 3249 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3250 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3251 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3252 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3253 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3254 | |
| 3255 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3256 | // part of the value is returned in %st0 and the imaginary part in |
| 3257 | // %st1. |
| 3258 | case ComplexX87: |
| 3259 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3260 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3261 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3262 | break; |
| 3263 | } |
| 3264 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3265 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3266 | switch (Hi) { |
| 3267 | // Memory was handled previously and X87 should |
| 3268 | // never occur as a hi class. |
| 3269 | case Memory: |
| 3270 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3271 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3272 | |
| 3273 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3274 | case NoClass: |
| 3275 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3276 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3277 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3278 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3279 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3280 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3281 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3282 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3283 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3284 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3285 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3286 | break; |
| 3287 | |
| 3288 | // 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] | 3289 | // is passed in the next available eightbyte chunk if the last used |
| 3290 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3291 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3292 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3293 | case SSEUp: |
| 3294 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3295 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3296 | break; |
| 3297 | |
| 3298 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3299 | // returned together with the previous X87 value in %st0. |
| 3300 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3301 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3302 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3303 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3304 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3305 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3306 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3307 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3308 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3309 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3310 | break; |
| 3311 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3312 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3313 | // 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] | 3314 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3315 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3316 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3317 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3318 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3319 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3322 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3323 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3324 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3325 | const |
| 3326 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3327 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3328 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3329 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3330 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3331 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3332 | // Check some invariants. |
| 3333 | // FIXME: Enforce these by construction. |
| 3334 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3335 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3336 | |
| 3337 | neededInt = 0; |
| 3338 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3339 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3340 | switch (Lo) { |
| 3341 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3342 | if (Hi == NoClass) |
| 3343 | return ABIArgInfo::getIgnore(); |
| 3344 | // If the low part is just padding, it takes no register, leave ResType |
| 3345 | // null. |
| 3346 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3347 | "Unknown missing lo part"); |
| 3348 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3349 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3350 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3351 | // on the stack. |
| 3352 | case Memory: |
| 3353 | |
| 3354 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3355 | // COMPLEX_X87, it is passed in memory. |
| 3356 | case X87: |
| 3357 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3358 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3359 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3360 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3361 | |
| 3362 | case SSEUp: |
| 3363 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3364 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3365 | |
| 3366 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3367 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3368 | // and %r9 is used. |
| 3369 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3370 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3371 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3372 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3373 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3374 | |
| 3375 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3376 | // so that the parameter gets the right LLVM IR attributes. |
| 3377 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3378 | // Treat an enum type as its underlying type. |
| 3379 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3380 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3381 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3382 | if (Ty->isIntegralOrEnumerationType() && |
| 3383 | Ty->isPromotableIntegerType()) |
| 3384 | return ABIArgInfo::getExtend(); |
| 3385 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3386 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3387 | break; |
| 3388 | |
| 3389 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3390 | // available SSE register is used, the registers are taken in the |
| 3391 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3392 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3393 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3394 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3395 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3396 | break; |
| 3397 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3398 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3399 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3400 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3401 | switch (Hi) { |
| 3402 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3403 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3404 | // which is passed in memory. |
| 3405 | case Memory: |
| 3406 | case X87: |
| 3407 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3408 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3409 | |
| 3410 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3411 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3412 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3413 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3414 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3415 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3416 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3417 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3418 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3419 | break; |
| 3420 | |
| 3421 | // X87Up generally doesn't occur here (long double is passed in |
| 3422 | // memory), except in situations involving unions. |
| 3423 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3424 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3425 | HighPart = GetSSETypeAtOffset(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); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3429 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3430 | ++neededSSE; |
| 3431 | break; |
| 3432 | |
| 3433 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3434 | // 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] | 3435 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3436 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3437 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3438 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3439 | break; |
| 3440 | } |
| 3441 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3442 | // If a high part was specified, merge it together with the low part. It is |
| 3443 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3444 | // first class struct aggregate with the high and low part: {low, high} |
| 3445 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3446 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3447 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3448 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3451 | ABIArgInfo |
| 3452 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3453 | unsigned &NeededSSE) const { |
| 3454 | auto RT = Ty->getAs<RecordType>(); |
| 3455 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3456 | |
| 3457 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3458 | return getIndirectReturnResult(Ty); |
| 3459 | |
| 3460 | // Sum up bases |
| 3461 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3462 | if (CXXRD->isDynamicClass()) { |
| 3463 | NeededInt = NeededSSE = 0; |
| 3464 | return getIndirectReturnResult(Ty); |
| 3465 | } |
| 3466 | |
| 3467 | for (const auto &I : CXXRD->bases()) |
| 3468 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3469 | .isIndirect()) { |
| 3470 | NeededInt = NeededSSE = 0; |
| 3471 | return getIndirectReturnResult(Ty); |
| 3472 | } |
| 3473 | } |
| 3474 | |
| 3475 | // Sum up members |
| 3476 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3477 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3478 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3479 | .isIndirect()) { |
| 3480 | NeededInt = NeededSSE = 0; |
| 3481 | return getIndirectReturnResult(Ty); |
| 3482 | } |
| 3483 | } else { |
| 3484 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3485 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3486 | LocalNeededSSE, true) |
| 3487 | .isIndirect()) { |
| 3488 | NeededInt = NeededSSE = 0; |
| 3489 | return getIndirectReturnResult(Ty); |
| 3490 | } |
| 3491 | NeededInt += LocalNeededInt; |
| 3492 | NeededSSE += LocalNeededSSE; |
| 3493 | } |
| 3494 | } |
| 3495 | |
| 3496 | return ABIArgInfo::getDirect(); |
| 3497 | } |
| 3498 | |
| 3499 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3500 | unsigned &NeededInt, |
| 3501 | unsigned &NeededSSE) const { |
| 3502 | |
| 3503 | NeededInt = 0; |
| 3504 | NeededSSE = 0; |
| 3505 | |
| 3506 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3507 | } |
| 3508 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3509 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3510 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3511 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3512 | |
| 3513 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3514 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3515 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3516 | unsigned NeededInt, NeededSSE; |
| 3517 | |
| 3518 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3519 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3520 | FI.getReturnInfo() = |
| 3521 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3522 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3523 | FreeIntRegs -= NeededInt; |
| 3524 | FreeSSERegs -= NeededSSE; |
| 3525 | } else { |
| 3526 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3527 | } |
| 3528 | } else if (!getCXXABI().classifyReturnType(FI)) |
| 3529 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3530 | |
| 3531 | // If the return value is indirect, then the hidden argument is consuming one |
| 3532 | // integer register. |
| 3533 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3534 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3535 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3536 | // The chain argument effectively gives us another free register. |
| 3537 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3538 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3539 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3540 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3541 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3542 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3543 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3544 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3545 | it != ie; ++it, ++ArgNo) { |
| 3546 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3547 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3548 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3549 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3550 | else |
| 3551 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3552 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3553 | |
| 3554 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3555 | // eightbyte of an argument, the whole argument is passed on the |
| 3556 | // stack. If registers have already been assigned for some |
| 3557 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3558 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3559 | FreeIntRegs -= NeededInt; |
| 3560 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3561 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3562 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3563 | } |
| 3564 | } |
| 3565 | } |
| 3566 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3567 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3568 | Address VAListAddr, QualType Ty) { |
| 3569 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3570 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3571 | llvm::Value *overflow_arg_area = |
| 3572 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3573 | |
| 3574 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3575 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3576 | // It isn't stated explicitly in the standard, but in practice we use |
| 3577 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3578 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3579 | if (Align > CharUnits::fromQuantity(8)) { |
| 3580 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3581 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3582 | } |
| 3583 | |
| 3584 | // 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] | 3585 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3586 | llvm::Value *Res = |
| 3587 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3588 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3589 | |
| 3590 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3591 | // l->overflow_arg_area + sizeof(type). |
| 3592 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3593 | // an 8 byte boundary. |
| 3594 | |
| 3595 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3596 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3597 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3598 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3599 | "overflow_arg_area.next"); |
| 3600 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3601 | |
| 3602 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3603 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3606 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3607 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3608 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3609 | // struct { |
| 3610 | // i32 gp_offset; |
| 3611 | // i32 fp_offset; |
| 3612 | // i8* overflow_arg_area; |
| 3613 | // i8* reg_save_area; |
| 3614 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3615 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3616 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3617 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3618 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3619 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3620 | |
| 3621 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3622 | // in the registers. If not go to step 7. |
| 3623 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3624 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3625 | |
| 3626 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3627 | // general purpose registers needed to pass type and num_fp to hold |
| 3628 | // the number of floating point registers needed. |
| 3629 | |
| 3630 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3631 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3632 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3633 | // |
| 3634 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3635 | // register save space). |
| 3636 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3637 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3638 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3639 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3640 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3641 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3642 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3643 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3644 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3645 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3646 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3650 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3651 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3652 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3653 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3654 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3655 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3656 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3657 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3658 | } |
| 3659 | |
| 3660 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3661 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3662 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3663 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3664 | |
| 3665 | // Emit code to load the value if it was passed in registers. |
| 3666 | |
| 3667 | CGF.EmitBlock(InRegBlock); |
| 3668 | |
| 3669 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3670 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3671 | // copying to a temporary location in case the parameter is passed |
| 3672 | // in different register classes or requires an alignment greater |
| 3673 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3674 | // |
| 3675 | // FIXME: This really results in shameful code when we end up needing to |
| 3676 | // collect arguments from different places; often what should result in a |
| 3677 | // simple assembling of a structure from scattered addresses has many more |
| 3678 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3679 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3680 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3681 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3682 | "reg_save_area"); |
| 3683 | |
| 3684 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3685 | if (neededInt && neededSSE) { |
| 3686 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3687 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3688 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3689 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3690 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3691 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3692 | llvm::Type *TyLo = ST->getElementType(0); |
| 3693 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3694 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3695 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3696 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3697 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3698 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3699 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3700 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3701 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3702 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3703 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3704 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3705 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3706 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3707 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3708 | CGF.Builder.CreateStore(V, |
| 3709 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3710 | |
| 3711 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3712 | V = CGF.Builder.CreateAlignedLoad( |
| 3713 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3714 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3715 | CharUnits Offset = CharUnits::fromQuantity( |
| 3716 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3717 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3718 | |
| 3719 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3720 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3721 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3722 | CharUnits::fromQuantity(8)); |
| 3723 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3724 | |
| 3725 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3726 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3727 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3728 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3729 | CharUnits TyAlign = SizeAlign.second; |
| 3730 | |
| 3731 | // Copy into a temporary if the type is more aligned than the |
| 3732 | // register save area. |
| 3733 | if (TyAlign.getQuantity() > 8) { |
| 3734 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3735 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3736 | RegAddr = Tmp; |
| 3737 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3738 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3739 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3740 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3741 | CharUnits::fromQuantity(16)); |
| 3742 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3743 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3744 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3745 | // SSE registers are spaced 16 bytes apart in the register save |
| 3746 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3747 | // The ABI isn't explicit about this, but it seems reasonable |
| 3748 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3749 | // naturally 16-byte aligned and the prologue is expected to store |
| 3750 | // all the SSE registers to the RSA. |
| 3751 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3752 | CharUnits::fromQuantity(16)); |
| 3753 | Address RegAddrHi = |
| 3754 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3755 | CharUnits::fromQuantity(16)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3756 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3757 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3758 | llvm::Value *V; |
| 3759 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3760 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3761 | V = CGF.Builder.CreateLoad( |
| 3762 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3763 | CGF.Builder.CreateStore(V, |
| 3764 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3765 | V = CGF.Builder.CreateLoad( |
| 3766 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3767 | CGF.Builder.CreateStore(V, |
| 3768 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3769 | |
| 3770 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3774 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3775 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3776 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3777 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3778 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3779 | gp_offset_p); |
| 3780 | } |
| 3781 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3782 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3783 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3784 | fp_offset_p); |
| 3785 | } |
| 3786 | CGF.EmitBranch(ContBlock); |
| 3787 | |
| 3788 | // Emit code to load the value if it was passed in memory. |
| 3789 | |
| 3790 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3791 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3792 | |
| 3793 | // Return the appropriate result. |
| 3794 | |
| 3795 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3796 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3797 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3798 | return ResAddr; |
| 3799 | } |
| 3800 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3801 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3802 | QualType Ty) const { |
| 3803 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3804 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3805 | CharUnits::fromQuantity(8), |
| 3806 | /*allowHigherAlign*/ false); |
| 3807 | } |
| 3808 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3809 | ABIArgInfo |
| 3810 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3811 | const ABIArgInfo ¤t) const { |
| 3812 | // Assumes vectorCall calling convention. |
| 3813 | const Type *Base = nullptr; |
| 3814 | uint64_t NumElts = 0; |
| 3815 | |
| 3816 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3817 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3818 | FreeSSERegs -= NumElts; |
| 3819 | return getDirectX86Hva(); |
| 3820 | } |
| 3821 | return current; |
| 3822 | } |
| 3823 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3824 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3825 | bool IsReturnType, bool IsVectorCall, |
| 3826 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3827 | |
| 3828 | if (Ty->isVoidType()) |
| 3829 | return ABIArgInfo::getIgnore(); |
| 3830 | |
| 3831 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3832 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3833 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3834 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3835 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3836 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3837 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3838 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3839 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3840 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3841 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3842 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3846 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3847 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3848 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3849 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3850 | const Type *Base = nullptr; |
| 3851 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3852 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3853 | // other targets. |
| 3854 | if ((IsVectorCall || IsRegCall) && |
| 3855 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3856 | if (IsRegCall) { |
| 3857 | if (FreeSSERegs >= NumElts) { |
| 3858 | FreeSSERegs -= NumElts; |
| 3859 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3860 | return ABIArgInfo::getDirect(); |
| 3861 | return ABIArgInfo::getExpand(); |
| 3862 | } |
| 3863 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3864 | } else if (IsVectorCall) { |
| 3865 | if (FreeSSERegs >= NumElts && |
| 3866 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3867 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3868 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3869 | } else if (IsReturnType) { |
| 3870 | return ABIArgInfo::getExpand(); |
| 3871 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3872 | // HVAs are delayed and reclassified in the 2nd step. |
| 3873 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3874 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3875 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3878 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3879 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3880 | // directly. |
| 3881 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3882 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3883 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3886 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3887 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3888 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3889 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3890 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3891 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3892 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3893 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3896 | // Bool type is always extended to the ABI, other builtin types are not |
| 3897 | // extended. |
| 3898 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3899 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3900 | return ABIArgInfo::getExtend(); |
| 3901 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3902 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3903 | // passes them indirectly through memory. |
| 3904 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3905 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3906 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3907 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3908 | } |
| 3909 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3910 | return ABIArgInfo::getDirect(); |
| 3911 | } |
| 3912 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3913 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 3914 | unsigned FreeSSERegs, |
| 3915 | bool IsVectorCall, |
| 3916 | bool IsRegCall) const { |
| 3917 | unsigned Count = 0; |
| 3918 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 3919 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 3920 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3921 | if (Count < VectorcallMaxParamNumAsReg) |
| 3922 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 3923 | else { |
| 3924 | // Since these cannot be passed in registers, pretend no registers |
| 3925 | // are left. |
| 3926 | unsigned ZeroSSERegsAvail = 0; |
| 3927 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 3928 | IsVectorCall, IsRegCall); |
| 3929 | } |
| 3930 | ++Count; |
| 3931 | } |
| 3932 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3933 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 3934 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3935 | } |
| 3936 | } |
| 3937 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3938 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3939 | bool IsVectorCall = |
| 3940 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3941 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3942 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3943 | unsigned FreeSSERegs = 0; |
| 3944 | if (IsVectorCall) { |
| 3945 | // We can use up to 4 SSE return registers with vectorcall. |
| 3946 | FreeSSERegs = 4; |
| 3947 | } else if (IsRegCall) { |
| 3948 | // RegCall gives us 16 SSE registers. |
| 3949 | FreeSSERegs = 16; |
| 3950 | } |
| 3951 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3952 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3953 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 3954 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3955 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3956 | if (IsVectorCall) { |
| 3957 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3958 | FreeSSERegs = 6; |
| 3959 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3960 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3961 | FreeSSERegs = 16; |
| 3962 | } |
| 3963 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3964 | if (IsVectorCall) { |
| 3965 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 3966 | } else { |
| 3967 | for (auto &I : FI.arguments()) |
| 3968 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 3969 | } |
| 3970 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3973 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3974 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 3975 | |
| 3976 | bool IsIndirect = false; |
| 3977 | |
| 3978 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3979 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 3980 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 3981 | uint64_t Width = getContext().getTypeSize(Ty); |
| 3982 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 3983 | } |
| 3984 | |
| 3985 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3986 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3987 | CharUnits::fromQuantity(8), |
| 3988 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3989 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3990 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3991 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3992 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3993 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3994 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3995 | bool IsSoftFloatABI; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3996 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3997 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 3998 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3999 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4000 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4001 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4002 | }; |
| 4003 | |
| 4004 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4005 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4006 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 4007 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4008 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4009 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4010 | // This is recovered from gcc output. |
| 4011 | return 1; // r1 is the dedicated stack pointer |
| 4012 | } |
| 4013 | |
| 4014 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4015 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4016 | }; |
| 4017 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4018 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4019 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4020 | // TODO: this implementation is now likely redundant with |
| 4021 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4022 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4023 | QualType Ty) const { |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4024 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4025 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4026 | // TODO: Implement this. For now ignore. |
| 4027 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4028 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4031 | // struct __va_list_tag { |
| 4032 | // unsigned char gpr; |
| 4033 | // unsigned char fpr; |
| 4034 | // unsigned short reserved; |
| 4035 | // void *overflow_arg_area; |
| 4036 | // void *reg_save_area; |
| 4037 | // }; |
| 4038 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4039 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4040 | bool isInt = |
| 4041 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4042 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4043 | |
| 4044 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4045 | // with the argument-lowering code. |
| 4046 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4047 | |
| 4048 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4049 | |
| 4050 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4051 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4052 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4053 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 4054 | } else { |
| 4055 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4056 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4057 | |
| 4058 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4059 | |
| 4060 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4061 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4062 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4063 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4064 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4065 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4066 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4067 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4068 | |
| 4069 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4070 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4071 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4072 | |
| 4073 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4074 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4075 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4076 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4077 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4078 | // Case 1: consume registers. |
| 4079 | Address RegAddr = Address::invalid(); |
| 4080 | { |
| 4081 | CGF.EmitBlock(UsingRegs); |
| 4082 | |
| 4083 | Address RegSaveAreaPtr = |
| 4084 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 4085 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4086 | CharUnits::fromQuantity(8)); |
| 4087 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4088 | |
| 4089 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4090 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4091 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4092 | CharUnits::fromQuantity(32)); |
| 4093 | } |
| 4094 | |
| 4095 | // Get the address of the saved value by scaling the number of |
| 4096 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4097 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4098 | llvm::Value *RegOffset = |
| 4099 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4100 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4101 | RegAddr.getPointer(), RegOffset), |
| 4102 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4103 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4104 | |
| 4105 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4106 | NumRegs = |
| 4107 | Builder.CreateAdd(NumRegs, |
| 4108 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4109 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4110 | |
| 4111 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4112 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4113 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4114 | // Case 2: consume space in the overflow area. |
| 4115 | Address MemAddr = Address::invalid(); |
| 4116 | { |
| 4117 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4118 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4119 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4120 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4121 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4122 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4123 | |
| 4124 | CharUnits Size; |
| 4125 | if (!isIndirect) { |
| 4126 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4127 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4128 | } else { |
| 4129 | Size = CGF.getPointerSize(); |
| 4130 | } |
| 4131 | |
| 4132 | Address OverflowAreaAddr = |
| 4133 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4134 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4135 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4136 | // Round up address of argument to alignment |
| 4137 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4138 | if (Align > OverflowAreaAlign) { |
| 4139 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4140 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4141 | Align); |
| 4142 | } |
| 4143 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4144 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4145 | |
| 4146 | // Increase the overflow area. |
| 4147 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4148 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4149 | CGF.EmitBranch(Cont); |
| 4150 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4151 | |
| 4152 | CGF.EmitBlock(Cont); |
| 4153 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4154 | // Merge the cases with a phi. |
| 4155 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4156 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4157 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4158 | // Load the pointer if the argument was passed indirectly. |
| 4159 | if (isIndirect) { |
| 4160 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4161 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | return Result; |
| 4165 | } |
| 4166 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4167 | bool |
| 4168 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4169 | llvm::Value *Address) const { |
| 4170 | // This is calculated from the LLVM and GCC tables and verified |
| 4171 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4172 | |
| 4173 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4174 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4175 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4176 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4177 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4178 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4179 | |
| 4180 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4181 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4182 | |
| 4183 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4184 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4185 | |
| 4186 | // 64-76 are various 4-byte special-purpose registers: |
| 4187 | // 64: mq |
| 4188 | // 65: lr |
| 4189 | // 66: ctr |
| 4190 | // 67: ap |
| 4191 | // 68-75 cr0-7 |
| 4192 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4193 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4194 | |
| 4195 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4196 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4197 | |
| 4198 | // 109: vrsave |
| 4199 | // 110: vscr |
| 4200 | // 111: spe_acc |
| 4201 | // 112: spefscr |
| 4202 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4203 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4204 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4205 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4206 | } |
| 4207 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4208 | // PowerPC-64 |
| 4209 | |
| 4210 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4211 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4212 | class PPC64_SVR4_ABIInfo : public ABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4213 | public: |
| 4214 | enum ABIKind { |
| 4215 | ELFv1 = 0, |
| 4216 | ELFv2 |
| 4217 | }; |
| 4218 | |
| 4219 | private: |
| 4220 | static const unsigned GPRBits = 64; |
| 4221 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4222 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4223 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4224 | |
| 4225 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4226 | // will be passed in a QPX register. |
| 4227 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4228 | if (!HasQPX) |
| 4229 | return false; |
| 4230 | |
| 4231 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4232 | unsigned NumElements = VT->getNumElements(); |
| 4233 | if (NumElements == 1) |
| 4234 | return false; |
| 4235 | |
| 4236 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4237 | if (getContext().getTypeSize(Ty) <= 256) |
| 4238 | return true; |
| 4239 | } else if (VT->getElementType()-> |
| 4240 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4241 | if (getContext().getTypeSize(Ty) <= 128) |
| 4242 | return true; |
| 4243 | } |
| 4244 | } |
| 4245 | |
| 4246 | return false; |
| 4247 | } |
| 4248 | |
| 4249 | bool IsQPXVectorTy(QualType Ty) const { |
| 4250 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4251 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4252 | |
| 4253 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4254 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4255 | bool SoftFloatABI) |
| 4256 | : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
| 4257 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4258 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4259 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4260 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4261 | |
| 4262 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4263 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4264 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4265 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4266 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4267 | uint64_t Members) const override; |
| 4268 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4269 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4270 | // Example: For aggregate arguments that fit in a register, we could |
| 4271 | // use getDirectInReg (as is done below for structs containing a single |
| 4272 | // floating-point value) to avoid pushing them to memory on function |
| 4273 | // entry. This would require changing the logic in PPCISelLowering |
| 4274 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4275 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4276 | if (!getCXXABI().classifyReturnType(FI)) |
| 4277 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4278 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4279 | // We rely on the default argument classification for the most part. |
| 4280 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4281 | // 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] | 4282 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4283 | if (T) { |
| 4284 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4285 | if (IsQPXVectorTy(T) || |
| 4286 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4287 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4288 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4289 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4290 | continue; |
| 4291 | } |
| 4292 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4293 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4294 | } |
| 4295 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4296 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4297 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4298 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4299 | }; |
| 4300 | |
| 4301 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4302 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4303 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4304 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4305 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4306 | bool SoftFloatABI) |
| 4307 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4308 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4309 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4310 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4311 | // This is recovered from gcc output. |
| 4312 | return 1; // r1 is the dedicated stack pointer |
| 4313 | } |
| 4314 | |
| 4315 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4316 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4317 | }; |
| 4318 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4319 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4320 | public: |
| 4321 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4322 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4323 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4324 | // This is recovered from gcc output. |
| 4325 | return 1; // r1 is the dedicated stack pointer |
| 4326 | } |
| 4327 | |
| 4328 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4329 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4330 | }; |
| 4331 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4332 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4333 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4334 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4335 | // extended to 64 bits. |
| 4336 | bool |
| 4337 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4338 | // Treat an enum type as its underlying type. |
| 4339 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4340 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4341 | |
| 4342 | // Promotable integer types are required to be promoted by the ABI. |
| 4343 | if (Ty->isPromotableIntegerType()) |
| 4344 | return true; |
| 4345 | |
| 4346 | // In addition to the usual promotable integer types, we also need to |
| 4347 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4348 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4349 | switch (BT->getKind()) { |
| 4350 | case BuiltinType::Int: |
| 4351 | case BuiltinType::UInt: |
| 4352 | return true; |
| 4353 | default: |
| 4354 | break; |
| 4355 | } |
| 4356 | |
| 4357 | return false; |
| 4358 | } |
| 4359 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4360 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4361 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4362 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4363 | // Complex types are passed just like their elements. |
| 4364 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4365 | Ty = CTy->getElementType(); |
| 4366 | |
| 4367 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4368 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4369 | if (IsQPXVectorTy(Ty)) { |
| 4370 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4371 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4372 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4373 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4374 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4375 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4376 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4377 | |
| 4378 | // For single-element float/vector structs, we consider the whole type |
| 4379 | // to have the same alignment requirements as its single element. |
| 4380 | const Type *AlignAsType = nullptr; |
| 4381 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4382 | if (EltType) { |
| 4383 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4384 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4385 | getContext().getTypeSize(EltType) == 128) || |
| 4386 | (BT && BT->isFloatingPoint())) |
| 4387 | AlignAsType = EltType; |
| 4388 | } |
| 4389 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4390 | // Likewise for ELFv2 homogeneous aggregates. |
| 4391 | const Type *Base = nullptr; |
| 4392 | uint64_t Members = 0; |
| 4393 | if (!AlignAsType && Kind == ELFv2 && |
| 4394 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4395 | AlignAsType = Base; |
| 4396 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4397 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4398 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4399 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4400 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4401 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4402 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4403 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4404 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4405 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4406 | |
| 4407 | // Otherwise, we only need alignment for any aggregate type that |
| 4408 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4409 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4410 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4411 | return CharUnits::fromQuantity(32); |
| 4412 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4413 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4414 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4415 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4418 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4419 | /// aggregate. Base is set to the base element type, and Members is set |
| 4420 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4421 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4422 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4423 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4424 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4425 | if (NElements == 0) |
| 4426 | return false; |
| 4427 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4428 | return false; |
| 4429 | Members *= NElements; |
| 4430 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4431 | const RecordDecl *RD = RT->getDecl(); |
| 4432 | if (RD->hasFlexibleArrayMember()) |
| 4433 | return false; |
| 4434 | |
| 4435 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4436 | |
| 4437 | // If this is a C++ record, check the bases first. |
| 4438 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4439 | for (const auto &I : CXXRD->bases()) { |
| 4440 | // Ignore empty records. |
| 4441 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4442 | continue; |
| 4443 | |
| 4444 | uint64_t FldMembers; |
| 4445 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4446 | return false; |
| 4447 | |
| 4448 | Members += FldMembers; |
| 4449 | } |
| 4450 | } |
| 4451 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4452 | for (const auto *FD : RD->fields()) { |
| 4453 | // Ignore (non-zero arrays of) empty records. |
| 4454 | QualType FT = FD->getType(); |
| 4455 | while (const ConstantArrayType *AT = |
| 4456 | getContext().getAsConstantArrayType(FT)) { |
| 4457 | if (AT->getSize().getZExtValue() == 0) |
| 4458 | return false; |
| 4459 | FT = AT->getElementType(); |
| 4460 | } |
| 4461 | if (isEmptyRecord(getContext(), FT, true)) |
| 4462 | continue; |
| 4463 | |
| 4464 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4465 | if (getContext().getLangOpts().CPlusPlus && |
| 4466 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4467 | continue; |
| 4468 | |
| 4469 | uint64_t FldMembers; |
| 4470 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4471 | return false; |
| 4472 | |
| 4473 | Members = (RD->isUnion() ? |
| 4474 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4475 | } |
| 4476 | |
| 4477 | if (!Base) |
| 4478 | return false; |
| 4479 | |
| 4480 | // Ensure there is no padding. |
| 4481 | if (getContext().getTypeSize(Base) * Members != |
| 4482 | getContext().getTypeSize(Ty)) |
| 4483 | return false; |
| 4484 | } else { |
| 4485 | Members = 1; |
| 4486 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4487 | Members = 2; |
| 4488 | Ty = CT->getElementType(); |
| 4489 | } |
| 4490 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4491 | // Most ABIs only support float, double, and some vector type widths. |
| 4492 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4493 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4494 | |
| 4495 | // The base type must be the same for all members. Types that |
| 4496 | // agree in both total size and mode (float vs. vector) are |
| 4497 | // treated as being equivalent here. |
| 4498 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4499 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4500 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4501 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4502 | // so make sure to widen it explicitly. |
| 4503 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4504 | QualType EltTy = VT->getElementType(); |
| 4505 | unsigned NumElements = |
| 4506 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4507 | Base = getContext() |
| 4508 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4509 | .getTypePtr(); |
| 4510 | } |
| 4511 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4512 | |
| 4513 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4514 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4515 | return false; |
| 4516 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4517 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4518 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4519 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4520 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4521 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4522 | // double, long double, or 128-bit vectors. |
| 4523 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4524 | if (BT->getKind() == BuiltinType::Float || |
| 4525 | BT->getKind() == BuiltinType::Double || |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4526 | BT->getKind() == BuiltinType::LongDouble) { |
| 4527 | if (IsSoftFloatABI) |
| 4528 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4529 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4530 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4531 | } |
| 4532 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4533 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4534 | return true; |
| 4535 | } |
| 4536 | return false; |
| 4537 | } |
| 4538 | |
| 4539 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4540 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4541 | // Vector types require one register, floating point types require one |
| 4542 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4543 | uint32_t NumRegs = |
| 4544 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4545 | |
| 4546 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4547 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4550 | ABIArgInfo |
| 4551 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4552 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4553 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4554 | if (Ty->isAnyComplexType()) |
| 4555 | return ABIArgInfo::getDirect(); |
| 4556 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4557 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4558 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4559 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4560 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4561 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4562 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4563 | else if (Size < 128) { |
| 4564 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4565 | return ABIArgInfo::getDirect(CoerceTy); |
| 4566 | } |
| 4567 | } |
| 4568 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4569 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4570 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4571 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4572 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4573 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4574 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4575 | |
| 4576 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4577 | const Type *Base = nullptr; |
| 4578 | uint64_t Members = 0; |
| 4579 | if (Kind == ELFv2 && |
| 4580 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4581 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4582 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4583 | return ABIArgInfo::getDirect(CoerceTy); |
| 4584 | } |
| 4585 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4586 | // If an aggregate may end up fully in registers, we do not |
| 4587 | // use the ByVal method, but pass the aggregate as array. |
| 4588 | // This is usually beneficial since we avoid forcing the |
| 4589 | // back-end to store the argument to memory. |
| 4590 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4591 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4592 | llvm::Type *CoerceTy; |
| 4593 | |
| 4594 | // Types up to 8 bytes are passed as integer type (which will be |
| 4595 | // properly aligned in the argument save area doubleword). |
| 4596 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4597 | CoerceTy = |
| 4598 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4599 | // Larger types are passed as arrays, with the base type selected |
| 4600 | // according to the required alignment in the save area. |
| 4601 | else { |
| 4602 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4603 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4604 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4605 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4606 | } |
| 4607 | |
| 4608 | return ABIArgInfo::getDirect(CoerceTy); |
| 4609 | } |
| 4610 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4611 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4612 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4613 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4614 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | return (isPromotableTypeForABI(Ty) ? |
| 4618 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4619 | } |
| 4620 | |
| 4621 | ABIArgInfo |
| 4622 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4623 | if (RetTy->isVoidType()) |
| 4624 | return ABIArgInfo::getIgnore(); |
| 4625 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4626 | if (RetTy->isAnyComplexType()) |
| 4627 | return ABIArgInfo::getDirect(); |
| 4628 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4629 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4630 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4631 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4632 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4633 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4634 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4635 | else if (Size < 128) { |
| 4636 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4637 | return ABIArgInfo::getDirect(CoerceTy); |
| 4638 | } |
| 4639 | } |
| 4640 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4641 | if (isAggregateTypeForABI(RetTy)) { |
| 4642 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4643 | const Type *Base = nullptr; |
| 4644 | uint64_t Members = 0; |
| 4645 | if (Kind == ELFv2 && |
| 4646 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4647 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4648 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4649 | return ABIArgInfo::getDirect(CoerceTy); |
| 4650 | } |
| 4651 | |
| 4652 | // ELFv2 small aggregates are returned in up to two registers. |
| 4653 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4654 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4655 | if (Bits == 0) |
| 4656 | return ABIArgInfo::getIgnore(); |
| 4657 | |
| 4658 | llvm::Type *CoerceTy; |
| 4659 | if (Bits > GPRBits) { |
| 4660 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4661 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4662 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4663 | CoerceTy = |
| 4664 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4665 | return ABIArgInfo::getDirect(CoerceTy); |
| 4666 | } |
| 4667 | |
| 4668 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4669 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4670 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4671 | |
| 4672 | return (isPromotableTypeForABI(RetTy) ? |
| 4673 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4674 | } |
| 4675 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4676 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4677 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4678 | QualType Ty) const { |
| 4679 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4680 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4681 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4682 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4683 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4684 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4685 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4686 | // in separate doublewords. However, Clang expects us to produce a |
| 4687 | // pointer to a structure with the two parts packed tightly. So generate |
| 4688 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4689 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4690 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4691 | CharUnits EltSize = TypeInfo.first / 2; |
| 4692 | if (EltSize < SlotSize) { |
| 4693 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4694 | SlotSize * 2, SlotSize, |
| 4695 | SlotSize, /*AllowHigher*/ true); |
| 4696 | |
| 4697 | Address RealAddr = Addr; |
| 4698 | Address ImagAddr = RealAddr; |
| 4699 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4700 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4701 | SlotSize - EltSize); |
| 4702 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4703 | 2 * SlotSize - EltSize); |
| 4704 | } else { |
| 4705 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4706 | } |
| 4707 | |
| 4708 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4709 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4710 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4711 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4712 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4713 | |
| 4714 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4715 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4716 | /*init*/ true); |
| 4717 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4718 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4719 | } |
| 4720 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4721 | // Otherwise, just use the general rule. |
| 4722 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4723 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | static bool |
| 4727 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4728 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4729 | // This is calculated from the LLVM and GCC tables and verified |
| 4730 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4731 | |
| 4732 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4733 | |
| 4734 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4735 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4736 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4737 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4738 | |
| 4739 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4740 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4741 | |
| 4742 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4743 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4744 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4745 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4746 | // 64: mq |
| 4747 | // 65: lr |
| 4748 | // 66: ctr |
| 4749 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4750 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4751 | |
| 4752 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4753 | // 68-75 cr0-7 |
| 4754 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4755 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4756 | |
| 4757 | // 77-108: v0-31, the 16-byte vector registers |
| 4758 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4759 | |
| 4760 | // 109: vrsave |
| 4761 | // 110: vscr |
| 4762 | // 111: spe_acc |
| 4763 | // 112: spefscr |
| 4764 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4765 | // 114: tfhar |
| 4766 | // 115: tfiar |
| 4767 | // 116: texasr |
| 4768 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4769 | |
| 4770 | return false; |
| 4771 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4772 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4773 | bool |
| 4774 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4775 | CodeGen::CodeGenFunction &CGF, |
| 4776 | llvm::Value *Address) const { |
| 4777 | |
| 4778 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4779 | } |
| 4780 | |
| 4781 | bool |
| 4782 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4783 | llvm::Value *Address) const { |
| 4784 | |
| 4785 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4786 | } |
| 4787 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4788 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4789 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4790 | //===----------------------------------------------------------------------===// |
| 4791 | |
| 4792 | namespace { |
| 4793 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4794 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4795 | public: |
| 4796 | enum ABIKind { |
| 4797 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4798 | DarwinPCS, |
| 4799 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4800 | }; |
| 4801 | |
| 4802 | private: |
| 4803 | ABIKind Kind; |
| 4804 | |
| 4805 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4806 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4807 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4808 | |
| 4809 | private: |
| 4810 | ABIKind getABIKind() const { return Kind; } |
| 4811 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4812 | |
| 4813 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4814 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4815 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4816 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4817 | uint64_t Members) const override; |
| 4818 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4819 | bool isIllegalVectorType(QualType Ty) const; |
| 4820 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4821 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4822 | if (!getCXXABI().classifyReturnType(FI)) |
| 4823 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4824 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4825 | for (auto &it : FI.arguments()) |
| 4826 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4827 | } |
| 4828 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4829 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4830 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4831 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4832 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4833 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4834 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4835 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4836 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4837 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 4838 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4839 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4840 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4841 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4842 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4843 | QualType Ty) const override; |
| 4844 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4845 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 4846 | ArrayRef<llvm::Type*> scalars, |
| 4847 | bool asReturnValue) const override { |
| 4848 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4849 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 4850 | bool isSwiftErrorInRegister() const override { |
| 4851 | return true; |
| 4852 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 4853 | |
| 4854 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 4855 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4856 | }; |
| 4857 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4858 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4859 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4860 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4861 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4862 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4863 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Saleem Abdulrasool | 40db477 | 2017-02-11 23:03:13 +0000 | [diff] [blame] | 4864 | return "mov\tfp, fp\t\t# marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4867 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4868 | return 31; |
| 4869 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4870 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4871 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4872 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 4873 | |
| 4874 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 4875 | public: |
| 4876 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 4877 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 4878 | |
| 4879 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 4880 | llvm::SmallString<24> &Opt) const override { |
| 4881 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 4882 | } |
| 4883 | |
| 4884 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 4885 | llvm::SmallString<32> &Opt) const override { |
| 4886 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 4887 | } |
| 4888 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4889 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4890 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4891 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4892 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4893 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4894 | // Handle illegal vector types here. |
| 4895 | if (isIllegalVectorType(Ty)) { |
| 4896 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4897 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 4898 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4899 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 4900 | return ABIArgInfo::getDirect(ResType); |
| 4901 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4902 | if (Size <= 32) { |
| 4903 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4904 | return ABIArgInfo::getDirect(ResType); |
| 4905 | } |
| 4906 | if (Size == 64) { |
| 4907 | llvm::Type *ResType = |
| 4908 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4909 | return ABIArgInfo::getDirect(ResType); |
| 4910 | } |
| 4911 | if (Size == 128) { |
| 4912 | llvm::Type *ResType = |
| 4913 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4914 | return ABIArgInfo::getDirect(ResType); |
| 4915 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4916 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4917 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4918 | |
| 4919 | if (!isAggregateTypeForABI(Ty)) { |
| 4920 | // Treat an enum type as its underlying type. |
| 4921 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4922 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4923 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4924 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4925 | ? ABIArgInfo::getExtend() |
| 4926 | : ABIArgInfo::getDirect()); |
| 4927 | } |
| 4928 | |
| 4929 | // Structures with either a non-trivial destructor or a non-trivial |
| 4930 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4931 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4932 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4933 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
| 4936 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4937 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4938 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4939 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 4940 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4941 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4942 | return ABIArgInfo::getIgnore(); |
| 4943 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4944 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 4945 | // 0. |
| 4946 | if (IsEmpty && Size == 0) |
| 4947 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4948 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4949 | } |
| 4950 | |
| 4951 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4952 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4953 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4954 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4955 | return ABIArgInfo::getDirect( |
| 4956 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4960 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 4961 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 4962 | // same size and alignment. |
| 4963 | if (getTarget().isRenderScriptTarget()) { |
| 4964 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 4965 | } |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4966 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 4967 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4968 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4969 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4970 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4971 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4972 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4973 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4974 | } |
| 4975 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4976 | } |
| 4977 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4978 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4981 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4982 | if (RetTy->isVoidType()) |
| 4983 | return ABIArgInfo::getIgnore(); |
| 4984 | |
| 4985 | // Large vector types should be returned via memory. |
| 4986 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4987 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4988 | |
| 4989 | if (!isAggregateTypeForABI(RetTy)) { |
| 4990 | // Treat an enum type as its underlying type. |
| 4991 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4992 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4993 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4994 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4995 | ? ABIArgInfo::getExtend() |
| 4996 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4999 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5000 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5001 | return ABIArgInfo::getIgnore(); |
| 5002 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5003 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5004 | uint64_t Members = 0; |
| 5005 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5006 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 5007 | return ABIArgInfo::getDirect(); |
| 5008 | |
| 5009 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5010 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5011 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5012 | // same size and alignment. |
| 5013 | if (getTarget().isRenderScriptTarget()) { |
| 5014 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5015 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5016 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5017 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5018 | |
| 5019 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5020 | // For aggregates with 16-byte alignment, we use i128. |
| 5021 | if (Alignment < 128 && Size == 128) { |
| 5022 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5023 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5024 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5025 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5026 | } |
| 5027 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5028 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5031 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5032 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5033 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5034 | // Check whether VT is legal. |
| 5035 | unsigned NumElements = VT->getNumElements(); |
| 5036 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5037 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5038 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5039 | return true; |
| 5040 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5041 | } |
| 5042 | return false; |
| 5043 | } |
| 5044 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5045 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5046 | llvm::Type *eltTy, |
| 5047 | unsigned elts) const { |
| 5048 | if (!llvm::isPowerOf2_32(elts)) |
| 5049 | return false; |
| 5050 | if (totalSize.getQuantity() != 8 && |
| 5051 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5052 | return false; |
| 5053 | return true; |
| 5054 | } |
| 5055 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5056 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5057 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5058 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5059 | // but with the difference that any floating-point type is allowed, |
| 5060 | // including __fp16. |
| 5061 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5062 | if (BT->isFloatingPoint()) |
| 5063 | return true; |
| 5064 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5065 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5066 | if (VecSize == 64 || VecSize == 128) |
| 5067 | return true; |
| 5068 | } |
| 5069 | return false; |
| 5070 | } |
| 5071 | |
| 5072 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5073 | uint64_t Members) const { |
| 5074 | return Members <= 4; |
| 5075 | } |
| 5076 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5077 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5078 | QualType Ty, |
| 5079 | CodeGenFunction &CGF) const { |
| 5080 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5081 | bool IsIndirect = AI.isIndirect(); |
| 5082 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5083 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5084 | if (IsIndirect) |
| 5085 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5086 | else if (AI.getCoerceToType()) |
| 5087 | BaseTy = AI.getCoerceToType(); |
| 5088 | |
| 5089 | unsigned NumRegs = 1; |
| 5090 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5091 | BaseTy = ArrTy->getElementType(); |
| 5092 | NumRegs = ArrTy->getNumElements(); |
| 5093 | } |
| 5094 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5095 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5096 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5097 | // Standard, section B.4: |
| 5098 | // |
| 5099 | // struct { |
| 5100 | // void *__stack; |
| 5101 | // void *__gr_top; |
| 5102 | // void *__vr_top; |
| 5103 | // int __gr_offs; |
| 5104 | // int __vr_offs; |
| 5105 | // }; |
| 5106 | |
| 5107 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5108 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5109 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5110 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5111 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5112 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5113 | CharUnits TyAlign = TyInfo.second; |
| 5114 | |
| 5115 | Address reg_offs_p = Address::invalid(); |
| 5116 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5117 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5118 | CharUnits reg_top_offset; |
| 5119 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5120 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5121 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5122 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5123 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5124 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5125 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5126 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5127 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5128 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5129 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5130 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5131 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5132 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 5133 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5134 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5135 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5136 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5137 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5138 | } |
| 5139 | |
| 5140 | //======================================= |
| 5141 | // Find out where argument was passed |
| 5142 | //======================================= |
| 5143 | |
| 5144 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5145 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5146 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5147 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5148 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5149 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5150 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5151 | |
| 5152 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5153 | |
| 5154 | // 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] | 5155 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5156 | CGF.EmitBlock(MaybeRegBlock); |
| 5157 | |
| 5158 | // Integer arguments may need to correct register alignment (for example a |
| 5159 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5160 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5161 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5162 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5163 | |
| 5164 | reg_offs = CGF.Builder.CreateAdd( |
| 5165 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5166 | "align_regoffs"); |
| 5167 | reg_offs = CGF.Builder.CreateAnd( |
| 5168 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5169 | "aligned_regoffs"); |
| 5170 | } |
| 5171 | |
| 5172 | // 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] | 5173 | // The fact that this is done unconditionally reflects the fact that |
| 5174 | // allocating an argument to the stack also uses up all the remaining |
| 5175 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5176 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5177 | NewOffset = CGF.Builder.CreateAdd( |
| 5178 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5179 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5180 | |
| 5181 | // Now we're in a position to decide whether this argument really was in |
| 5182 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5183 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5184 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5185 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5186 | |
| 5187 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5188 | |
| 5189 | //======================================= |
| 5190 | // Argument was in registers |
| 5191 | //======================================= |
| 5192 | |
| 5193 | // Now we emit the code for if the argument was originally passed in |
| 5194 | // registers. First start the appropriate block: |
| 5195 | CGF.EmitBlock(InRegBlock); |
| 5196 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5197 | llvm::Value *reg_top = nullptr; |
| 5198 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 5199 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5200 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5201 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5202 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5203 | Address RegAddr = Address::invalid(); |
| 5204 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5205 | |
| 5206 | if (IsIndirect) { |
| 5207 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5208 | // stored registers or on the stack will actually be a struct **. |
| 5209 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5210 | } |
| 5211 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5212 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5213 | uint64_t NumMembers = 0; |
| 5214 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5215 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5216 | // Homogeneous aggregates passed in registers will have their elements split |
| 5217 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5218 | // qN+1, ...). We reload and store into a temporary local variable |
| 5219 | // contiguously. |
| 5220 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5221 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5222 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5223 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5224 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5225 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5226 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5227 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5228 | int Offset = 0; |
| 5229 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5230 | BaseTyInfo.first.getQuantity() < 16) |
| 5231 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5232 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5233 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5234 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5235 | Address LoadAddr = |
| 5236 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5237 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5238 | |
| 5239 | Address StoreAddr = |
| 5240 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5241 | |
| 5242 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5243 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5244 | } |
| 5245 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5246 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5247 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5248 | // Otherwise the object is contiguous in memory. |
| 5249 | |
| 5250 | // It might be right-aligned in its slot. |
| 5251 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5252 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5253 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5254 | TyInfo.first < SlotSize) { |
| 5255 | CharUnits Offset = SlotSize - TyInfo.first; |
| 5256 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5257 | } |
| 5258 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5259 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5260 | } |
| 5261 | |
| 5262 | CGF.EmitBranch(ContBlock); |
| 5263 | |
| 5264 | //======================================= |
| 5265 | // Argument was on the stack |
| 5266 | //======================================= |
| 5267 | CGF.EmitBlock(OnStackBlock); |
| 5268 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5269 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 5270 | CharUnits::Zero(), "stack_p"); |
| 5271 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5272 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5273 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5274 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5275 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5276 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5277 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5278 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5279 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5280 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5281 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5282 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5283 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5284 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5285 | "align_stack"); |
| 5286 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5287 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5288 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5289 | Address OnStackAddr(OnStackPtr, |
| 5290 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
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 | // All stack slots are multiples of 8 bytes. |
| 5293 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5294 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5295 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5296 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5297 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5298 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5299 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5300 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5301 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5302 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5303 | |
| 5304 | // Write the new value of __stack for the next call to va_arg |
| 5305 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5306 | |
| 5307 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5308 | TyInfo.first < StackSlotSize) { |
| 5309 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 5310 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5313 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5314 | |
| 5315 | CGF.EmitBranch(ContBlock); |
| 5316 | |
| 5317 | //======================================= |
| 5318 | // Tidy up |
| 5319 | //======================================= |
| 5320 | CGF.EmitBlock(ContBlock); |
| 5321 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5322 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5323 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5324 | |
| 5325 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5326 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 5327 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5328 | |
| 5329 | return ResAddr; |
| 5330 | } |
| 5331 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5332 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5333 | CodeGenFunction &CGF) const { |
| 5334 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5335 | // illegal vector types. Lower VAArg here for these cases and use |
| 5336 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5337 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5338 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5339 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5340 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5341 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5342 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5343 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5344 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5345 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5346 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5349 | // The size of the actual thing passed, which might end up just |
| 5350 | // being a pointer for indirect types. |
| 5351 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5352 | |
| 5353 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5354 | // aggregates should be passed indirectly. |
| 5355 | bool IsIndirect = false; |
| 5356 | if (TyInfo.first.getQuantity() > 16) { |
| 5357 | const Type *Base = nullptr; |
| 5358 | uint64_t Members = 0; |
| 5359 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5360 | } |
| 5361 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5362 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5363 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5364 | } |
| 5365 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5366 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5367 | QualType Ty) const { |
| 5368 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5369 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5370 | CharUnits::fromQuantity(8), |
| 5371 | /*allowHigherAlign*/ false); |
| 5372 | } |
| 5373 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5374 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5375 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5376 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5377 | |
| 5378 | namespace { |
| 5379 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5380 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5381 | public: |
| 5382 | enum ABIKind { |
| 5383 | APCS = 0, |
| 5384 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5385 | AAPCS_VFP = 2, |
| 5386 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5387 | }; |
| 5388 | |
| 5389 | private: |
| 5390 | ABIKind Kind; |
| 5391 | |
| 5392 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5393 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5394 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5395 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5396 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5397 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5398 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5399 | switch (getTarget().getTriple().getEnvironment()) { |
| 5400 | case llvm::Triple::Android: |
| 5401 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5402 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5403 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5404 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5405 | case llvm::Triple::MuslEABI: |
| 5406 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5407 | return true; |
| 5408 | default: |
| 5409 | return false; |
| 5410 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5413 | bool isEABIHF() const { |
| 5414 | switch (getTarget().getTriple().getEnvironment()) { |
| 5415 | case llvm::Triple::EABIHF: |
| 5416 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5417 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5418 | return true; |
| 5419 | default: |
| 5420 | return false; |
| 5421 | } |
| 5422 | } |
| 5423 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5424 | ABIKind getABIKind() const { return Kind; } |
| 5425 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5426 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 5427 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5428 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5429 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5430 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5431 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5432 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5433 | uint64_t Members) const override; |
| 5434 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5435 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5436 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5437 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5438 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5439 | |
| 5440 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5441 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5442 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5443 | |
| 5444 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 5445 | ArrayRef<llvm::Type*> scalars, |
| 5446 | bool asReturnValue) const override { |
| 5447 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5448 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5449 | bool isSwiftErrorInRegister() const override { |
| 5450 | return true; |
| 5451 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5452 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5453 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5454 | }; |
| 5455 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5456 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5457 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5458 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5459 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5460 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5461 | const ARMABIInfo &getABIInfo() const { |
| 5462 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5463 | } |
| 5464 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5465 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5466 | return 13; |
| 5467 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5468 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5469 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5470 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 5471 | } |
| 5472 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5473 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5474 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5475 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5476 | |
| 5477 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5478 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5479 | return false; |
| 5480 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5481 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5482 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5483 | if (getABIInfo().isEABI()) return 88; |
| 5484 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5485 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5486 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5487 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 5488 | CodeGen::CodeGenModule &CGM, |
| 5489 | ForDefinition_t IsForDefinition) const override { |
| 5490 | if (!IsForDefinition) |
| 5491 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5492 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5493 | if (!FD) |
| 5494 | return; |
| 5495 | |
| 5496 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5497 | if (!Attr) |
| 5498 | return; |
| 5499 | |
| 5500 | const char *Kind; |
| 5501 | switch (Attr->getInterrupt()) { |
| 5502 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5503 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5504 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5505 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5506 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5507 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5508 | } |
| 5509 | |
| 5510 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5511 | |
| 5512 | Fn->addFnAttr("interrupt", Kind); |
| 5513 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5514 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5515 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5516 | return; |
| 5517 | |
| 5518 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5519 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5520 | // the backend to perform a realignment as part of the function prologue. |
| 5521 | llvm::AttrBuilder B; |
| 5522 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5523 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5524 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5525 | }; |
| 5526 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5527 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5528 | public: |
| 5529 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5530 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5531 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5532 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 5533 | CodeGen::CodeGenModule &CGM, |
| 5534 | ForDefinition_t IsForDefinition) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5535 | |
| 5536 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5537 | llvm::SmallString<24> &Opt) const override { |
| 5538 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5539 | } |
| 5540 | |
| 5541 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5542 | llvm::SmallString<32> &Opt) const override { |
| 5543 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5544 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5545 | }; |
| 5546 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5547 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 5548 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 5549 | ForDefinition_t IsForDefinition) const { |
| 5550 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition); |
| 5551 | if (!IsForDefinition) |
| 5552 | return; |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5553 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 5554 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5555 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5556 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5557 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5558 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5559 | FI.getReturnInfo() = |
| 5560 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5561 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5562 | for (auto &I : FI.arguments()) |
| 5563 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5564 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5565 | // Always honor user-specified calling convention. |
| 5566 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5567 | return; |
| 5568 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5569 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5570 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5571 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5572 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5573 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5574 | /// Return the default calling convention that LLVM will use. |
| 5575 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5576 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5577 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5578 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5579 | else if (isEABI()) |
| 5580 | return llvm::CallingConv::ARM_AAPCS; |
| 5581 | else |
| 5582 | return llvm::CallingConv::ARM_APCS; |
| 5583 | } |
| 5584 | |
| 5585 | /// Return the calling convention that our ABI would like us to use |
| 5586 | /// as the C calling convention. |
| 5587 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5588 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5589 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5590 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5591 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5592 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5593 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5594 | llvm_unreachable("bad ABI kind"); |
| 5595 | } |
| 5596 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5597 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5598 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5599 | |
| 5600 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5601 | // they'd just match what LLVM will infer from the triple. |
| 5602 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5603 | if (abiCC != getLLVMDefaultCC()) |
| 5604 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5605 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5606 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 5607 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 5608 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
| 5609 | switch (getABIKind()) { |
| 5610 | case APCS: |
| 5611 | case AAPCS16_VFP: |
| 5612 | if (abiCC != getLLVMDefaultCC()) |
| 5613 | BuiltinCC = abiCC; |
| 5614 | break; |
| 5615 | case AAPCS: |
| 5616 | case AAPCS_VFP: |
| 5617 | BuiltinCC = llvm::CallingConv::ARM_AAPCS; |
| 5618 | break; |
| 5619 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5620 | } |
| 5621 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5622 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5623 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5624 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5625 | // A single-precision floating-point type (including promoted |
| 5626 | // half-precision types); A double-precision floating-point type; |
| 5627 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5628 | // with a Base Type of a single- or double-precision floating-point type, |
| 5629 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5630 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5631 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5632 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5633 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5634 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5635 | // Handle illegal vector types here. |
| 5636 | if (isIllegalVectorType(Ty)) { |
| 5637 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5638 | if (Size <= 32) { |
| 5639 | llvm::Type *ResType = |
| 5640 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5641 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5642 | } |
| 5643 | if (Size == 64) { |
| 5644 | llvm::Type *ResType = llvm::VectorType::get( |
| 5645 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5646 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5647 | } |
| 5648 | if (Size == 128) { |
| 5649 | llvm::Type *ResType = llvm::VectorType::get( |
| 5650 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5651 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5652 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5653 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5654 | } |
| 5655 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5656 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 5657 | // unspecified. This is not done for OpenCL as it handles the half type |
| 5658 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5659 | if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5660 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5661 | llvm::Type::getFloatTy(getVMContext()) : |
| 5662 | llvm::Type::getInt32Ty(getVMContext()); |
| 5663 | return ABIArgInfo::getDirect(ResType); |
| 5664 | } |
| 5665 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5666 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5667 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5668 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5669 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5670 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5671 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5672 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5673 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5674 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5675 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5676 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5677 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5678 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5679 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5680 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5681 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5682 | return ABIArgInfo::getIgnore(); |
| 5683 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5684 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5685 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5686 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5687 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5688 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5689 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5690 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5691 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5692 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5693 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5694 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5695 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5696 | // this convention even for a variadic function: the backend will use GPRs |
| 5697 | // if needed. |
| 5698 | const Type *Base = nullptr; |
| 5699 | uint64_t Members = 0; |
| 5700 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5701 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5702 | llvm::Type *Ty = |
| 5703 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5704 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5705 | } |
| 5706 | } |
| 5707 | |
| 5708 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5709 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5710 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5711 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5712 | // and a pointer is passed. |
| 5713 | return ABIArgInfo::getIndirect( |
| 5714 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5715 | } |
| 5716 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5717 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5718 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5719 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5720 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5721 | uint64_t ABIAlign = 4; |
| 5722 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5723 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5724 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5725 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5726 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5727 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5728 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5729 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5730 | /*ByVal=*/true, |
| 5731 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5732 | } |
| 5733 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5734 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5735 | // same size and alignment. |
| 5736 | if (getTarget().isRenderScriptTarget()) { |
| 5737 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5738 | } |
| 5739 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5740 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5741 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5742 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5743 | // FIXME: Try to match the types of the arguments more accurately where |
| 5744 | // we can. |
| 5745 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5746 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5747 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5748 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5749 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5750 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5751 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5752 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5753 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5754 | } |
| 5755 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5756 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5757 | llvm::LLVMContext &VMContext) { |
| 5758 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5759 | // is called integer-like if its size is less than or equal to one word, and |
| 5760 | // the offset of each of its addressable sub-fields is zero. |
| 5761 | |
| 5762 | uint64_t Size = Context.getTypeSize(Ty); |
| 5763 | |
| 5764 | // Check that the type fits in a word. |
| 5765 | if (Size > 32) |
| 5766 | return false; |
| 5767 | |
| 5768 | // FIXME: Handle vector types! |
| 5769 | if (Ty->isVectorType()) |
| 5770 | return false; |
| 5771 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5772 | // Float types are never treated as "integer like". |
| 5773 | if (Ty->isRealFloatingType()) |
| 5774 | return false; |
| 5775 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5776 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5777 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5778 | return true; |
| 5779 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5780 | // Small complex integer types are "integer like". |
| 5781 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5782 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5783 | |
| 5784 | // Single element and zero sized arrays should be allowed, by the definition |
| 5785 | // above, but they are not. |
| 5786 | |
| 5787 | // Otherwise, it must be a record type. |
| 5788 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5789 | if (!RT) return false; |
| 5790 | |
| 5791 | // Ignore records with flexible arrays. |
| 5792 | const RecordDecl *RD = RT->getDecl(); |
| 5793 | if (RD->hasFlexibleArrayMember()) |
| 5794 | return false; |
| 5795 | |
| 5796 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5797 | // like". |
| 5798 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5799 | |
| 5800 | bool HadField = false; |
| 5801 | unsigned idx = 0; |
| 5802 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5803 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5804 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5805 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5806 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5807 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5808 | // struct { int : 0; int x } |
| 5809 | // is non-integer like according to gcc. |
| 5810 | if (FD->isBitField()) { |
| 5811 | if (!RD->isUnion()) |
| 5812 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5813 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5814 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5815 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5816 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5817 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5818 | } |
| 5819 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5820 | // Check if this field is at offset 0. |
| 5821 | if (Layout.getFieldOffset(idx) != 0) |
| 5822 | return false; |
| 5823 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5824 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5825 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5826 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5827 | // Only allow at most one field in a structure. This doesn't match the |
| 5828 | // wording above, but follows gcc in situations with a field following an |
| 5829 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5830 | if (!RD->isUnion()) { |
| 5831 | if (HadField) |
| 5832 | return false; |
| 5833 | |
| 5834 | HadField = true; |
| 5835 | } |
| 5836 | } |
| 5837 | |
| 5838 | return true; |
| 5839 | } |
| 5840 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5841 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5842 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5843 | bool IsEffectivelyAAPCS_VFP = |
| 5844 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5845 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5846 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5847 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5848 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5849 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5850 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5851 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5852 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5853 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5854 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5855 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5856 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5857 | if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5858 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5859 | llvm::Type::getFloatTy(getVMContext()) : |
| 5860 | llvm::Type::getInt32Ty(getVMContext()); |
| 5861 | return ABIArgInfo::getDirect(ResType); |
| 5862 | } |
| 5863 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5864 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5865 | // Treat an enum type as its underlying type. |
| 5866 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5867 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5868 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5869 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5870 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5871 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5872 | |
| 5873 | // Are we following APCS? |
| 5874 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5875 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5876 | return ABIArgInfo::getIgnore(); |
| 5877 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5878 | // Complex types are all returned as packed integers. |
| 5879 | // |
| 5880 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5881 | // correctly. |
| 5882 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5883 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5884 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5885 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5886 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5887 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5888 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5889 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5890 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5891 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5892 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5893 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5894 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5895 | } |
| 5896 | |
| 5897 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5898 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5899 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5900 | |
| 5901 | // Otherwise this is an AAPCS variant. |
| 5902 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5903 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5904 | return ABIArgInfo::getIgnore(); |
| 5905 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5906 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5907 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5908 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5909 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5910 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5911 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5912 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5913 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5914 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5915 | } |
| 5916 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5917 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5918 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5919 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5920 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5921 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 5922 | // same size and alignment. |
| 5923 | if (getTarget().isRenderScriptTarget()) { |
| 5924 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5925 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5926 | if (getDataLayout().isBigEndian()) |
| 5927 | // 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] | 5928 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5929 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5930 | // Return in the smallest viable integer type. |
| 5931 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5932 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5933 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5934 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5935 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5936 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5937 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5938 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5939 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5940 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5941 | } |
| 5942 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5943 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5944 | } |
| 5945 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5946 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5947 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 5948 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 5949 | if (isAndroid()) { |
| 5950 | // Android shipped using Clang 3.1, which supported a slightly different |
| 5951 | // vector ABI. The primary differences were that 3-element vector types |
| 5952 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 5953 | // accepts that legacy behavior for Android only. |
| 5954 | // Check whether VT is legal. |
| 5955 | unsigned NumElements = VT->getNumElements(); |
| 5956 | // NumElements should be power of 2 or equal to 3. |
| 5957 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 5958 | return true; |
| 5959 | } else { |
| 5960 | // Check whether VT is legal. |
| 5961 | unsigned NumElements = VT->getNumElements(); |
| 5962 | uint64_t Size = getContext().getTypeSize(VT); |
| 5963 | // NumElements should be power of 2. |
| 5964 | if (!llvm::isPowerOf2_32(NumElements)) |
| 5965 | return true; |
| 5966 | // Size should be greater than 32 bits. |
| 5967 | return Size <= 32; |
| 5968 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5969 | } |
| 5970 | return false; |
| 5971 | } |
| 5972 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5973 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 5974 | llvm::Type *eltTy, |
| 5975 | unsigned numElts) const { |
| 5976 | if (!llvm::isPowerOf2_32(numElts)) |
| 5977 | return false; |
| 5978 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 5979 | if (size > 64) |
| 5980 | return false; |
| 5981 | if (vectorSize.getQuantity() != 8 && |
| 5982 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 5983 | return false; |
| 5984 | return true; |
| 5985 | } |
| 5986 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5987 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5988 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 5989 | // double, or 64-bit or 128-bit vectors. |
| 5990 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5991 | if (BT->getKind() == BuiltinType::Float || |
| 5992 | BT->getKind() == BuiltinType::Double || |
| 5993 | BT->getKind() == BuiltinType::LongDouble) |
| 5994 | return true; |
| 5995 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5996 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5997 | if (VecSize == 64 || VecSize == 128) |
| 5998 | return true; |
| 5999 | } |
| 6000 | return false; |
| 6001 | } |
| 6002 | |
| 6003 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 6004 | uint64_t Members) const { |
| 6005 | return Members <= 4; |
| 6006 | } |
| 6007 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6008 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6009 | QualType Ty) const { |
| 6010 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6011 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6012 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6013 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6014 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 6015 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 6016 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6017 | } |
| 6018 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6019 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 6020 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6021 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6022 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6023 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6024 | const Type *Base = nullptr; |
| 6025 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6026 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 6027 | IsIndirect = true; |
| 6028 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6029 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6030 | // allocated by the caller. |
| 6031 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 6032 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6033 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6034 | IsIndirect = true; |
| 6035 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6036 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6037 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6038 | // 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] | 6039 | // Our callers should be prepared to handle an under-aligned address. |
| 6040 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6041 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6042 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6043 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6044 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6045 | // ARMv7k allows type alignment up to 16 bytes. |
| 6046 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6047 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6048 | } else { |
| 6049 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6050 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6051 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6052 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6053 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6054 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6055 | } |
| 6056 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6057 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6058 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6059 | //===----------------------------------------------------------------------===// |
| 6060 | |
| 6061 | namespace { |
| 6062 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6063 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6064 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6065 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6066 | |
| 6067 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6068 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6069 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6070 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6071 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6072 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6073 | }; |
| 6074 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6075 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6076 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6077 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6078 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6079 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6080 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 6081 | CodeGen::CodeGenModule &M, |
| 6082 | ForDefinition_t IsForDefinition) const override; |
| 6083 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6084 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6085 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6086 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6087 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6088 | }; |
| 6089 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6090 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6091 | if (RetTy->isVoidType()) |
| 6092 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6093 | |
| 6094 | // note: this is different from default ABI |
| 6095 | if (!RetTy->isScalarType()) |
| 6096 | return ABIArgInfo::getDirect(); |
| 6097 | |
| 6098 | // Treat an enum type as its underlying type. |
| 6099 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6100 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6101 | |
| 6102 | return (RetTy->isPromotableIntegerType() ? |
| 6103 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
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::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6107 | // Treat an enum type as its underlying type. |
| 6108 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6109 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6110 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6111 | // Return aggregates type as indirect by value |
| 6112 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6113 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6114 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6115 | return (Ty->isPromotableIntegerType() ? |
| 6116 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6119 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6120 | if (!getCXXABI().classifyReturnType(FI)) |
| 6121 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6122 | for (auto &I : FI.arguments()) |
| 6123 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6124 | |
| 6125 | // Always honor user-specified calling convention. |
| 6126 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6127 | return; |
| 6128 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6129 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6130 | } |
| 6131 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6132 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6133 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6134 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6135 | } |
| 6136 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 6137 | void NVPTXTargetCodeGenInfo::setTargetAttributes( |
| 6138 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 6139 | ForDefinition_t IsForDefinition) const { |
| 6140 | if (!IsForDefinition) |
| 6141 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6142 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6143 | if (!FD) return; |
| 6144 | |
| 6145 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6146 | |
| 6147 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6148 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6149 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6150 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6151 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6152 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6153 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6154 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6155 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6156 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6157 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6158 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6159 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6160 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6161 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6162 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6163 | // __global__ functions cannot be called from the device, we do not |
| 6164 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6165 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6166 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6167 | addNVVMMetadata(F, "kernel", 1); |
| 6168 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6169 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6170 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6171 | llvm::APSInt MaxThreads(32); |
| 6172 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6173 | if (MaxThreads > 0) |
| 6174 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6175 | |
| 6176 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6177 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6178 | // we don't have to add a PTX directive. |
| 6179 | if (Attr->getMinBlocks()) { |
| 6180 | llvm::APSInt MinBlocks(32); |
| 6181 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6182 | if (MinBlocks > 0) |
| 6183 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6184 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6185 | } |
| 6186 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6187 | } |
| 6188 | } |
| 6189 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6190 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6191 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6192 | llvm::Module *M = F->getParent(); |
| 6193 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6194 | |
| 6195 | // Get "nvvm.annotations" metadata node |
| 6196 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6197 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6198 | llvm::Metadata *MDVals[] = { |
| 6199 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6200 | llvm::ConstantAsMetadata::get( |
| 6201 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6202 | // Append metadata to nvvm.annotations |
| 6203 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6204 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6205 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6206 | |
| 6207 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6208 | // SystemZ ABI Implementation |
| 6209 | //===----------------------------------------------------------------------===// |
| 6210 | |
| 6211 | namespace { |
| 6212 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6213 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6214 | bool HasVector; |
| 6215 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6216 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6217 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6218 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6219 | |
| 6220 | bool isPromotableIntegerType(QualType Ty) const; |
| 6221 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6222 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6223 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6224 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6225 | |
| 6226 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6227 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6228 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6229 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6230 | if (!getCXXABI().classifyReturnType(FI)) |
| 6231 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6232 | for (auto &I : FI.arguments()) |
| 6233 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6234 | } |
| 6235 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6236 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6237 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6238 | |
| 6239 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 6240 | ArrayRef<llvm::Type*> scalars, |
| 6241 | bool asReturnValue) const override { |
| 6242 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6243 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6244 | bool isSwiftErrorInRegister() const override { |
| 6245 | return true; |
| 6246 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6247 | }; |
| 6248 | |
| 6249 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6250 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6251 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6252 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6253 | }; |
| 6254 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6255 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6256 | |
| 6257 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6258 | // Treat an enum type as its underlying type. |
| 6259 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6260 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6261 | |
| 6262 | // Promotable integer types are required to be promoted by the ABI. |
| 6263 | if (Ty->isPromotableIntegerType()) |
| 6264 | return true; |
| 6265 | |
| 6266 | // 32-bit values must also be promoted. |
| 6267 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6268 | switch (BT->getKind()) { |
| 6269 | case BuiltinType::Int: |
| 6270 | case BuiltinType::UInt: |
| 6271 | return true; |
| 6272 | default: |
| 6273 | return false; |
| 6274 | } |
| 6275 | return false; |
| 6276 | } |
| 6277 | |
| 6278 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6279 | return (Ty->isAnyComplexType() || |
| 6280 | Ty->isVectorType() || |
| 6281 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6282 | } |
| 6283 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6284 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6285 | return (HasVector && |
| 6286 | Ty->isVectorType() && |
| 6287 | getContext().getTypeSize(Ty) <= 128); |
| 6288 | } |
| 6289 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6290 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6291 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6292 | switch (BT->getKind()) { |
| 6293 | case BuiltinType::Float: |
| 6294 | case BuiltinType::Double: |
| 6295 | return true; |
| 6296 | default: |
| 6297 | return false; |
| 6298 | } |
| 6299 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6300 | return false; |
| 6301 | } |
| 6302 | |
| 6303 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6304 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6305 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6306 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6307 | |
| 6308 | // If this is a C++ record, check the bases first. |
| 6309 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6310 | for (const auto &I : CXXRD->bases()) { |
| 6311 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6312 | |
| 6313 | // Empty bases don't affect things either way. |
| 6314 | if (isEmptyRecord(getContext(), Base, true)) |
| 6315 | continue; |
| 6316 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6317 | if (!Found.isNull()) |
| 6318 | return Ty; |
| 6319 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6320 | } |
| 6321 | |
| 6322 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6323 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6324 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6325 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6326 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6327 | if (getContext().getLangOpts().CPlusPlus && |
| 6328 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 6329 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6330 | |
| 6331 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6332 | // Nested structures still do though. |
| 6333 | if (!Found.isNull()) |
| 6334 | return Ty; |
| 6335 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6336 | } |
| 6337 | |
| 6338 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6339 | // 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] | 6340 | if (!Found.isNull()) |
| 6341 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6344 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6345 | } |
| 6346 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6347 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6348 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6349 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6350 | // struct { |
| 6351 | // i64 __gpr; |
| 6352 | // i64 __fpr; |
| 6353 | // i8 *__overflow_arg_area; |
| 6354 | // i8 *__reg_save_area; |
| 6355 | // }; |
| 6356 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6357 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6358 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6359 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6360 | Ty = getContext().getCanonicalType(Ty); |
| 6361 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6362 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6363 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6364 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6365 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6366 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6367 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6368 | CharUnits UnpaddedSize; |
| 6369 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6370 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6371 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6372 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6373 | } else { |
| 6374 | if (AI.getCoerceToType()) |
| 6375 | ArgTy = AI.getCoerceToType(); |
| 6376 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6377 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6378 | UnpaddedSize = TyInfo.first; |
| 6379 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6380 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6381 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6382 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6383 | PaddedSize = CharUnits::fromQuantity(16); |
| 6384 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6385 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6386 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6387 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6388 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6389 | llvm::Value *PaddedSizeV = |
| 6390 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6391 | |
| 6392 | if (IsVector) { |
| 6393 | // Work out the address of a vector argument on the stack. |
| 6394 | // Vector arguments are always passed in the high bits of a |
| 6395 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6396 | Address OverflowArgAreaPtr = |
| 6397 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6398 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6399 | Address OverflowArgArea = |
| 6400 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6401 | TyInfo.second); |
| 6402 | Address MemAddr = |
| 6403 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6404 | |
| 6405 | // Update overflow_arg_area_ptr pointer |
| 6406 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6407 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6408 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6409 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6410 | |
| 6411 | return MemAddr; |
| 6412 | } |
| 6413 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6414 | assert(PaddedSize.getQuantity() == 8); |
| 6415 | |
| 6416 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6417 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6418 | if (InFPRs) { |
| 6419 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6420 | RegCountField = 1; // __fpr |
| 6421 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6422 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6423 | } else { |
| 6424 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6425 | RegCountField = 0; // __gpr |
| 6426 | RegSaveIndex = 2; // save offset for r2 |
| 6427 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6428 | } |
| 6429 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6430 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 6431 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 6432 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6433 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6434 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6435 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6436 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6437 | |
| 6438 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6439 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6440 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6441 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6442 | |
| 6443 | // Emit code to load the value if it was passed in registers. |
| 6444 | CGF.EmitBlock(InRegBlock); |
| 6445 | |
| 6446 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6447 | llvm::Value *ScaledRegCount = |
| 6448 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6449 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6450 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6451 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6452 | llvm::Value *RegOffset = |
| 6453 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6454 | Address RegSaveAreaPtr = |
| 6455 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 6456 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6457 | llvm::Value *RegSaveArea = |
| 6458 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6459 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6460 | "raw_reg_addr"), |
| 6461 | PaddedSize); |
| 6462 | Address RegAddr = |
| 6463 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6464 | |
| 6465 | // Update the register count |
| 6466 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6467 | llvm::Value *NewRegCount = |
| 6468 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6469 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6470 | CGF.EmitBranch(ContBlock); |
| 6471 | |
| 6472 | // Emit code to load the value if it was passed in memory. |
| 6473 | CGF.EmitBlock(InMemBlock); |
| 6474 | |
| 6475 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6476 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 6477 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 6478 | Address OverflowArgArea = |
| 6479 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6480 | PaddedSize); |
| 6481 | Address RawMemAddr = |
| 6482 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6483 | Address MemAddr = |
| 6484 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6485 | |
| 6486 | // Update overflow_arg_area_ptr pointer |
| 6487 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6488 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6489 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6490 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6491 | CGF.EmitBranch(ContBlock); |
| 6492 | |
| 6493 | // Return the appropriate result. |
| 6494 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6495 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6496 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6497 | |
| 6498 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6499 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6500 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6501 | |
| 6502 | return ResAddr; |
| 6503 | } |
| 6504 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6505 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6506 | if (RetTy->isVoidType()) |
| 6507 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6508 | if (isVectorArgumentType(RetTy)) |
| 6509 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6510 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6511 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6512 | return (isPromotableIntegerType(RetTy) ? |
| 6513 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6514 | } |
| 6515 | |
| 6516 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6517 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6518 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6519 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6520 | |
| 6521 | // Integers and enums are extended to full register width. |
| 6522 | if (isPromotableIntegerType(Ty)) |
| 6523 | return ABIArgInfo::getExtend(); |
| 6524 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6525 | // Handle vector types and vector-like structure types. Note that |
| 6526 | // as opposed to float-like structure types, we do not allow any |
| 6527 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6528 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6529 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6530 | if (isVectorArgumentType(SingleElementTy) && |
| 6531 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6532 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6533 | |
| 6534 | // 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] | 6535 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6536 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6537 | |
| 6538 | // Handle small structures. |
| 6539 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6540 | // Structures with flexible arrays have variable length, so really |
| 6541 | // fail the size test above. |
| 6542 | const RecordDecl *RD = RT->getDecl(); |
| 6543 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6544 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6545 | |
| 6546 | // The structure is passed as an unextended integer, a float, or a double. |
| 6547 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6548 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6549 | assert(Size == 32 || Size == 64); |
| 6550 | if (Size == 32) |
| 6551 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6552 | else |
| 6553 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6554 | } else |
| 6555 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6556 | return ABIArgInfo::getDirect(PassTy); |
| 6557 | } |
| 6558 | |
| 6559 | // Non-structure compounds are passed indirectly. |
| 6560 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6561 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6562 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6563 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6564 | } |
| 6565 | |
| 6566 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6567 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6568 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6569 | |
| 6570 | namespace { |
| 6571 | |
| 6572 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6573 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6574 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6575 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6576 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 6577 | CodeGen::CodeGenModule &M, |
| 6578 | ForDefinition_t IsForDefinition) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6579 | }; |
| 6580 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6581 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6582 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 6583 | void MSP430TargetCodeGenInfo::setTargetAttributes( |
| 6584 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 6585 | ForDefinition_t IsForDefinition) const { |
| 6586 | if (!IsForDefinition) |
| 6587 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6588 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6589 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 6590 | // Handle 'interrupt' attribute: |
| 6591 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6592 | |
| 6593 | // Step 1: Set ISR calling convention. |
| 6594 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 6595 | |
| 6596 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6597 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6598 | |
| 6599 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 6600 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 6601 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 6602 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6603 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6604 | } |
| 6605 | } |
| 6606 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6607 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6608 | // MIPS ABI Implementation. This works for both little-endian and |
| 6609 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6610 | //===----------------------------------------------------------------------===// |
| 6611 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6612 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6613 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6614 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6615 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6616 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6617 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6618 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6619 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6620 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6621 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6622 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6623 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6624 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6625 | |
| 6626 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6627 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6628 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6629 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6630 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6631 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6632 | }; |
| 6633 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6634 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6635 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6636 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6637 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6638 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6639 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6640 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6641 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6642 | return 29; |
| 6643 | } |
| 6644 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6645 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 6646 | CodeGen::CodeGenModule &CGM, |
| 6647 | ForDefinition_t IsForDefinition) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6648 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6649 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6650 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 6651 | |
| 6652 | if (FD->hasAttr<MipsLongCallAttr>()) |
| 6653 | Fn->addFnAttr("long-call"); |
| 6654 | else if (FD->hasAttr<MipsShortCallAttr>()) |
| 6655 | Fn->addFnAttr("short-call"); |
| 6656 | |
| 6657 | // Other attributes do not have a meaning for declarations. |
| 6658 | if (!IsForDefinition) |
| 6659 | return; |
| 6660 | |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6661 | if (FD->hasAttr<Mips16Attr>()) { |
| 6662 | Fn->addFnAttr("mips16"); |
| 6663 | } |
| 6664 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6665 | Fn->addFnAttr("nomips16"); |
| 6666 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6667 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6668 | if (FD->hasAttr<MicroMipsAttr>()) |
| 6669 | Fn->addFnAttr("micromips"); |
| 6670 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 6671 | Fn->addFnAttr("nomicromips"); |
| 6672 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6673 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6674 | if (!Attr) |
| 6675 | return; |
| 6676 | |
| 6677 | const char *Kind; |
| 6678 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6679 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6680 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6681 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6682 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6683 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6684 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6685 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6686 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6687 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6688 | } |
| 6689 | |
| 6690 | Fn->addFnAttr("interrupt", Kind); |
| 6691 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6692 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6693 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6694 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6695 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6696 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6697 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6698 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6699 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6700 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6701 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6702 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6703 | void MipsABIInfo::CoerceToIntArgs( |
| 6704 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6705 | llvm::IntegerType *IntTy = |
| 6706 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6707 | |
| 6708 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6709 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6710 | ArgList.push_back(IntTy); |
| 6711 | |
| 6712 | // If necessary, add one more integer type to ArgList. |
| 6713 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6714 | |
| 6715 | if (R) |
| 6716 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6717 | } |
| 6718 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6719 | // In N32/64, an aligned double precision floating point field is passed in |
| 6720 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6721 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6722 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6723 | |
| 6724 | if (IsO32) { |
| 6725 | CoerceToIntArgs(TySize, ArgList); |
| 6726 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6727 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6728 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6729 | if (Ty->isComplexType()) |
| 6730 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6731 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6732 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6733 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6734 | // Unions/vectors are passed in integer registers. |
| 6735 | if (!RT || !RT->isStructureOrClassType()) { |
| 6736 | CoerceToIntArgs(TySize, ArgList); |
| 6737 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6738 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6739 | |
| 6740 | const RecordDecl *RD = RT->getDecl(); |
| 6741 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6742 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6743 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6744 | uint64_t LastOffset = 0; |
| 6745 | unsigned idx = 0; |
| 6746 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6747 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6748 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6749 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6750 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6751 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6752 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6753 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6754 | |
| 6755 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6756 | continue; |
| 6757 | |
| 6758 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6759 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6760 | continue; |
| 6761 | |
| 6762 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6763 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6764 | ArgList.push_back(I64); |
| 6765 | |
| 6766 | // Add double type. |
| 6767 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6768 | LastOffset = Offset + 64; |
| 6769 | } |
| 6770 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6771 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6772 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6773 | |
| 6774 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6775 | } |
| 6776 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6777 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6778 | uint64_t Offset) const { |
| 6779 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6780 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6781 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6782 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6783 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6784 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6785 | ABIArgInfo |
| 6786 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6787 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6788 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6789 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6790 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6791 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6792 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6793 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6794 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6795 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6796 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6797 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6798 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6799 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6800 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6801 | return ABIArgInfo::getIgnore(); |
| 6802 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6803 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6804 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6805 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6806 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6807 | |
Petar Jovanovic | 6f4cdb8 | 2017-05-10 14:28:18 +0000 | [diff] [blame] | 6808 | // Use indirect if the aggregate cannot fit into registers for |
| 6809 | // passing arguments according to the ABI |
| 6810 | unsigned Threshold = IsO32 ? 16 : 64; |
| 6811 | |
| 6812 | if(getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(Threshold)) |
| 6813 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align), true, |
| 6814 | getContext().getTypeAlign(Ty) / 8 > Align); |
| 6815 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6816 | // If we have reached here, aggregates are passed directly by coercing to |
| 6817 | // another structure type. Padding is inserted if the offset of the |
| 6818 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6819 | ABIArgInfo ArgInfo = |
| 6820 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6821 | getPaddingType(OrigOffset, CurrOffset)); |
| 6822 | ArgInfo.setInReg(true); |
| 6823 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6824 | } |
| 6825 | |
| 6826 | // Treat an enum type as its underlying type. |
| 6827 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6828 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6829 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6830 | // All integral types are promoted to the GPR width. |
| 6831 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6832 | return ABIArgInfo::getExtend(); |
| 6833 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6834 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6835 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6836 | } |
| 6837 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6838 | llvm::Type* |
| 6839 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6840 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6841 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6842 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6843 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6844 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6845 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6846 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6847 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6848 | // N32/64 returns struct/classes in floating point registers if the |
| 6849 | // following conditions are met: |
| 6850 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6851 | // 2. The struct/class has one or two fields all of which are floating |
| 6852 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6853 | // 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] | 6854 | // |
| 6855 | // Any other composite results are returned in integer registers. |
| 6856 | // |
| 6857 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6858 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6859 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6860 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6861 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6862 | if (!BT || !BT->isFloatingPoint()) |
| 6863 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6864 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6865 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6866 | } |
| 6867 | |
| 6868 | if (b == e) |
| 6869 | return llvm::StructType::get(getVMContext(), RTList, |
| 6870 | RD->hasAttr<PackedAttr>()); |
| 6871 | |
| 6872 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6873 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6874 | } |
| 6875 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6876 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6877 | return llvm::StructType::get(getVMContext(), RTList); |
| 6878 | } |
| 6879 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6880 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6881 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6882 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6883 | if (RetTy->isVoidType()) |
| 6884 | return ABIArgInfo::getIgnore(); |
| 6885 | |
| 6886 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6887 | // However, N32/N64 ignores zero sized return values. |
| 6888 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6889 | return ABIArgInfo::getIgnore(); |
| 6890 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6891 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6892 | if (Size <= 128) { |
| 6893 | if (RetTy->isAnyComplexType()) |
| 6894 | return ABIArgInfo::getDirect(); |
| 6895 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6896 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6897 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6898 | if (!IsO32 || |
| 6899 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6900 | ABIArgInfo ArgInfo = |
| 6901 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6902 | ArgInfo.setInReg(true); |
| 6903 | return ArgInfo; |
| 6904 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6905 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6906 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6907 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6908 | } |
| 6909 | |
| 6910 | // Treat an enum type as its underlying type. |
| 6911 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6912 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6913 | |
| 6914 | return (RetTy->isPromotableIntegerType() ? |
| 6915 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6916 | } |
| 6917 | |
| 6918 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6919 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6920 | if (!getCXXABI().classifyReturnType(FI)) |
| 6921 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6922 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6923 | // 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] | 6924 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6925 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6926 | for (auto &I : FI.arguments()) |
| 6927 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6928 | } |
| 6929 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6930 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6931 | QualType OrigTy) const { |
| 6932 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6933 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6934 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6935 | // 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] | 6936 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6937 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6938 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6939 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6940 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6941 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6942 | DidPromote = true; |
| 6943 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6944 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6945 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6946 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6947 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6948 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6949 | // The alignment of things in the argument area is never larger than |
| 6950 | // StackAlignInBytes. |
| 6951 | TyInfo.second = |
| 6952 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6953 | |
| 6954 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6955 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6956 | |
| 6957 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6958 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6959 | |
| 6960 | |
| 6961 | // If there was a promotion, "unpromote" into a temporary. |
| 6962 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6963 | if (DidPromote) { |
| 6964 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6965 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6966 | |
| 6967 | // Truncate down to the right width. |
| 6968 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6969 | : CGF.IntPtrTy); |
| 6970 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6971 | if (OrigTy->isPointerType()) |
| 6972 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6973 | |
| 6974 | CGF.Builder.CreateStore(V, Temp); |
| 6975 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6976 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6977 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6978 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6979 | } |
| 6980 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6981 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6982 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6983 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6984 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 6985 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 6986 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6987 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6988 | return false; |
| 6989 | } |
| 6990 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6991 | bool |
| 6992 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6993 | llvm::Value *Address) const { |
| 6994 | // This information comes from gcc's implementation, which seems to |
| 6995 | // as canonical as it gets. |
| 6996 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6997 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 6998 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6999 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7000 | |
| 7001 | // 0-31 are the general purpose registers, $0 - $31. |
| 7002 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 7003 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 7004 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7005 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7006 | |
| 7007 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 7008 | // They are one bit wide and ignored here. |
| 7009 | |
| 7010 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 7011 | // (coprocessor 1 is the FP unit) |
| 7012 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 7013 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 7014 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7015 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7016 | return false; |
| 7017 | } |
| 7018 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7019 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7020 | // AVR ABI Implementation. |
| 7021 | //===----------------------------------------------------------------------===// |
| 7022 | |
| 7023 | namespace { |
| 7024 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7025 | public: |
| 7026 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7027 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 7028 | |
| 7029 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 7030 | CodeGen::CodeGenModule &CGM, |
| 7031 | ForDefinition_t IsForDefinition) const override { |
| 7032 | if (!IsForDefinition) |
| 7033 | return; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7034 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 7035 | if (!FD) return; |
| 7036 | auto *Fn = cast<llvm::Function>(GV); |
| 7037 | |
| 7038 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7039 | Fn->addFnAttr("interrupt"); |
| 7040 | |
| 7041 | if (FD->getAttr<AVRSignalAttr>()) |
| 7042 | Fn->addFnAttr("signal"); |
| 7043 | } |
| 7044 | }; |
| 7045 | } |
| 7046 | |
| 7047 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7048 | // 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] | 7049 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7050 | // handling. |
| 7051 | //===----------------------------------------------------------------------===// |
| 7052 | |
| 7053 | namespace { |
| 7054 | |
| 7055 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7056 | public: |
| 7057 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7058 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7059 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7060 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 7061 | CodeGen::CodeGenModule &M, |
| 7062 | ForDefinition_t IsForDefinition) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7063 | }; |
| 7064 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7065 | void TCETargetCodeGenInfo::setTargetAttributes( |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 7066 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 7067 | ForDefinition_t IsForDefinition) const { |
| 7068 | if (!IsForDefinition) |
| 7069 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7070 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7071 | if (!FD) return; |
| 7072 | |
| 7073 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7074 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7075 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7076 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7077 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7078 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7079 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7080 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7081 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7082 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7083 | llvm::NamedMDNode *OpenCLMetadata = |
| 7084 | M.getModule().getOrInsertNamedMetadata( |
| 7085 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7086 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7087 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7088 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7089 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7090 | Operands.push_back( |
| 7091 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7092 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7093 | Operands.push_back( |
| 7094 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7095 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7096 | Operands.push_back( |
| 7097 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7098 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7099 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7100 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7101 | // (false) for implementing the work_group_size_hint attr later. |
| 7102 | // 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] | 7103 | Operands.push_back( |
| 7104 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7105 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7106 | } |
| 7107 | } |
| 7108 | } |
| 7109 | } |
| 7110 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7111 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7112 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7113 | //===----------------------------------------------------------------------===// |
| 7114 | // Hexagon ABI Implementation |
| 7115 | //===----------------------------------------------------------------------===// |
| 7116 | |
| 7117 | namespace { |
| 7118 | |
| 7119 | class HexagonABIInfo : public ABIInfo { |
| 7120 | |
| 7121 | |
| 7122 | public: |
| 7123 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7124 | |
| 7125 | private: |
| 7126 | |
| 7127 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7128 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7129 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7130 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7131 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7132 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7133 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7134 | }; |
| 7135 | |
| 7136 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7137 | public: |
| 7138 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7139 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7140 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7141 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7142 | return 29; |
| 7143 | } |
| 7144 | }; |
| 7145 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7146 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7147 | |
| 7148 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7149 | if (!getCXXABI().classifyReturnType(FI)) |
| 7150 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7151 | for (auto &I : FI.arguments()) |
| 7152 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7153 | } |
| 7154 | |
| 7155 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7156 | if (!isAggregateTypeForABI(Ty)) { |
| 7157 | // Treat an enum type as its underlying type. |
| 7158 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7159 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7160 | |
| 7161 | return (Ty->isPromotableIntegerType() ? |
| 7162 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 7163 | } |
| 7164 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7165 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7166 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7167 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7168 | // Ignore empty records. |
| 7169 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7170 | return ABIArgInfo::getIgnore(); |
| 7171 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7172 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7173 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7174 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7175 | // Pass in the smallest viable integer type. |
| 7176 | else if (Size > 32) |
| 7177 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7178 | else if (Size > 16) |
| 7179 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7180 | else if (Size > 8) |
| 7181 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7182 | else |
| 7183 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7184 | } |
| 7185 | |
| 7186 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7187 | if (RetTy->isVoidType()) |
| 7188 | return ABIArgInfo::getIgnore(); |
| 7189 | |
| 7190 | // Large vector types should be returned via memory. |
| 7191 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7192 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7193 | |
| 7194 | if (!isAggregateTypeForABI(RetTy)) { |
| 7195 | // Treat an enum type as its underlying type. |
| 7196 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7197 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7198 | |
| 7199 | return (RetTy->isPromotableIntegerType() ? |
| 7200 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 7201 | } |
| 7202 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7203 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7204 | return ABIArgInfo::getIgnore(); |
| 7205 | |
| 7206 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7207 | // are returned indirectly. |
| 7208 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7209 | if (Size <= 64) { |
| 7210 | // Return in the smallest viable integer type. |
| 7211 | if (Size <= 8) |
| 7212 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7213 | if (Size <= 16) |
| 7214 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7215 | if (Size <= 32) |
| 7216 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7217 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7218 | } |
| 7219 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7220 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7221 | } |
| 7222 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7223 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7224 | QualType Ty) const { |
| 7225 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7226 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7227 | getContext().getTypeInfoInChars(Ty), |
| 7228 | CharUnits::fromQuantity(4), |
| 7229 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7230 | } |
| 7231 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7232 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7233 | // Lanai ABI Implementation |
| 7234 | //===----------------------------------------------------------------------===// |
| 7235 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7236 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7237 | class LanaiABIInfo : public DefaultABIInfo { |
| 7238 | public: |
| 7239 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7240 | |
| 7241 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7242 | |
| 7243 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7244 | CCState State(FI.getCallingConvention()); |
| 7245 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7246 | // regparm attribute set. |
| 7247 | if (FI.getHasRegParm()) { |
| 7248 | State.FreeRegs = FI.getRegParm(); |
| 7249 | } else { |
| 7250 | State.FreeRegs = 4; |
| 7251 | } |
| 7252 | |
| 7253 | if (!getCXXABI().classifyReturnType(FI)) |
| 7254 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7255 | for (auto &I : FI.arguments()) |
| 7256 | I.info = classifyArgumentType(I.type, State); |
| 7257 | } |
| 7258 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7259 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7260 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7261 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7262 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7263 | |
| 7264 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7265 | unsigned Size = getContext().getTypeSize(Ty); |
| 7266 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7267 | |
| 7268 | if (SizeInRegs == 0) |
| 7269 | return false; |
| 7270 | |
| 7271 | if (SizeInRegs > State.FreeRegs) { |
| 7272 | State.FreeRegs = 0; |
| 7273 | return false; |
| 7274 | } |
| 7275 | |
| 7276 | State.FreeRegs -= SizeInRegs; |
| 7277 | |
| 7278 | return true; |
| 7279 | } |
| 7280 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7281 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7282 | CCState &State) const { |
| 7283 | if (!ByVal) { |
| 7284 | if (State.FreeRegs) { |
| 7285 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7286 | return getNaturalAlignIndirectInReg(Ty); |
| 7287 | } |
| 7288 | return getNaturalAlignIndirect(Ty, false); |
| 7289 | } |
| 7290 | |
| 7291 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7292 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7293 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7294 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7295 | /*Realign=*/TypeAlign > |
| 7296 | MinABIStackAlignInBytes); |
| 7297 | } |
| 7298 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7299 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7300 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7301 | // Check with the C++ ABI first. |
| 7302 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7303 | if (RT) { |
| 7304 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7305 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7306 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7307 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7308 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7309 | } |
| 7310 | } |
| 7311 | |
| 7312 | if (isAggregateTypeForABI(Ty)) { |
| 7313 | // Structures with flexible arrays are always indirect. |
| 7314 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7315 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7316 | |
| 7317 | // Ignore empty structs/unions. |
| 7318 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7319 | return ABIArgInfo::getIgnore(); |
| 7320 | |
| 7321 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7322 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7323 | if (SizeInRegs <= State.FreeRegs) { |
| 7324 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7325 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7326 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7327 | State.FreeRegs -= SizeInRegs; |
| 7328 | return ABIArgInfo::getDirectInReg(Result); |
| 7329 | } else { |
| 7330 | State.FreeRegs = 0; |
| 7331 | } |
| 7332 | return getIndirectResult(Ty, true, State); |
| 7333 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7334 | |
| 7335 | // Treat an enum type as its underlying type. |
| 7336 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7337 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7338 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7339 | bool InReg = shouldUseInReg(Ty, State); |
| 7340 | if (Ty->isPromotableIntegerType()) { |
| 7341 | if (InReg) |
| 7342 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7343 | return ABIArgInfo::getExtend(); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7344 | } |
| 7345 | if (InReg) |
| 7346 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7347 | return ABIArgInfo::getDirect(); |
| 7348 | } |
| 7349 | |
| 7350 | namespace { |
| 7351 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7352 | public: |
| 7353 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7354 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7355 | }; |
| 7356 | } |
| 7357 | |
| 7358 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7359 | // AMDGPU ABI Implementation |
| 7360 | //===----------------------------------------------------------------------===// |
| 7361 | |
| 7362 | namespace { |
| 7363 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7364 | class AMDGPUABIInfo final : public DefaultABIInfo { |
| 7365 | public: |
| 7366 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7367 | |
| 7368 | private: |
| 7369 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 7370 | |
| 7371 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7372 | }; |
| 7373 | |
| 7374 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7375 | if (!getCXXABI().classifyReturnType(FI)) |
| 7376 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7377 | |
| 7378 | unsigned CC = FI.getCallingConvention(); |
| 7379 | for (auto &Arg : FI.arguments()) |
| 7380 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) |
| 7381 | Arg.info = classifyArgumentType(Arg.type); |
| 7382 | else |
| 7383 | Arg.info = DefaultABIInfo::classifyArgumentType(Arg.type); |
| 7384 | } |
| 7385 | |
| 7386 | /// \brief Classify argument of given type \p Ty. |
| 7387 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty) const { |
| 7388 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7389 | if (!StrTy) { |
| 7390 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7391 | } |
| 7392 | |
| 7393 | // Coerce single element structs to its element. |
| 7394 | if (StrTy->getNumElements() == 1) { |
| 7395 | return ABIArgInfo::getDirect(); |
| 7396 | } |
| 7397 | |
| 7398 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7399 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7400 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 7401 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 7402 | } |
| 7403 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7404 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7405 | public: |
| 7406 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7407 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7408 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 7409 | CodeGen::CodeGenModule &M, |
| 7410 | ForDefinition_t IsForDefinition) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7411 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7412 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7413 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7414 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7415 | |
| 7416 | unsigned getASTAllocaAddressSpace() const override { |
| 7417 | return LangAS::FirstTargetAddressSpace + |
| 7418 | getABIInfo().getDataLayout().getAllocaAddrSpace(); |
| 7419 | } |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7420 | unsigned getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7421 | const VarDecl *D) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7422 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7423 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7424 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7425 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame^] | 7426 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 7427 | ForDefinition_t IsForDefinition) const { |
| 7428 | if (!IsForDefinition) |
| 7429 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7430 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7431 | if (!FD) |
| 7432 | return; |
| 7433 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7434 | llvm::Function *F = cast<llvm::Function>(GV); |
| 7435 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7436 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 7437 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
| 7438 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 7439 | if (ReqdWGS || FlatWGS) { |
| 7440 | unsigned Min = FlatWGS ? FlatWGS->getMin() : 0; |
| 7441 | unsigned Max = FlatWGS ? FlatWGS->getMax() : 0; |
| 7442 | if (ReqdWGS && Min == 0 && Max == 0) |
| 7443 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7444 | |
| 7445 | if (Min != 0) { |
| 7446 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 7447 | |
| 7448 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 7449 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 7450 | } else |
| 7451 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7452 | } |
| 7453 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7454 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 7455 | unsigned Min = Attr->getMin(); |
| 7456 | unsigned Max = Attr->getMax(); |
| 7457 | |
| 7458 | if (Min != 0) { |
| 7459 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 7460 | |
| 7461 | std::string AttrVal = llvm::utostr(Min); |
| 7462 | if (Max != 0) |
| 7463 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 7464 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 7465 | } else |
| 7466 | assert(Max == 0 && "Max must be zero"); |
| 7467 | } |
| 7468 | |
| 7469 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7470 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7471 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7472 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7473 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 7474 | } |
| 7475 | |
| 7476 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7477 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 7478 | |
| 7479 | if (NumVGPR != 0) |
| 7480 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7481 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7482 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7483 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7484 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7485 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7486 | } |
| 7487 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7488 | // Currently LLVM assumes null pointers always have value 0, |
| 7489 | // which results in incorrectly transformed IR. Therefore, instead of |
| 7490 | // emitting null pointers in private and local address spaces, a null |
| 7491 | // pointer in generic address space is emitted which is casted to a |
| 7492 | // pointer in local or private address space. |
| 7493 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 7494 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 7495 | QualType QT) const { |
| 7496 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 7497 | return llvm::ConstantPointerNull::get(PT); |
| 7498 | |
| 7499 | auto &Ctx = CGM.getContext(); |
| 7500 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 7501 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 7502 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 7503 | llvm::ConstantPointerNull::get(NPT), PT); |
| 7504 | } |
| 7505 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7506 | unsigned |
| 7507 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7508 | const VarDecl *D) const { |
| 7509 | assert(!CGM.getLangOpts().OpenCL && |
| 7510 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 7511 | "Address space agnostic languages only"); |
| 7512 | unsigned DefaultGlobalAS = |
| 7513 | LangAS::FirstTargetAddressSpace + |
| 7514 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global); |
| 7515 | if (!D) |
| 7516 | return DefaultGlobalAS; |
| 7517 | |
| 7518 | unsigned AddrSpace = D->getType().getAddressSpace(); |
| 7519 | assert(AddrSpace == LangAS::Default || |
| 7520 | AddrSpace >= LangAS::FirstTargetAddressSpace); |
| 7521 | if (AddrSpace != LangAS::Default) |
| 7522 | return AddrSpace; |
| 7523 | |
| 7524 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 7525 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 7526 | return ConstAS.getValue(); |
| 7527 | } |
| 7528 | return DefaultGlobalAS; |
| 7529 | } |
| 7530 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7531 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 7532 | // SPARC v8 ABI Implementation. |
| 7533 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7534 | // |
| 7535 | // Ensures that complex values are passed in registers. |
| 7536 | // |
| 7537 | namespace { |
| 7538 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 7539 | public: |
| 7540 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7541 | |
| 7542 | private: |
| 7543 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7544 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7545 | }; |
| 7546 | } // end anonymous namespace |
| 7547 | |
| 7548 | |
| 7549 | ABIArgInfo |
| 7550 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 7551 | if (Ty->isAnyComplexType()) { |
| 7552 | return ABIArgInfo::getDirect(); |
| 7553 | } |
| 7554 | else { |
| 7555 | return DefaultABIInfo::classifyReturnType(Ty); |
| 7556 | } |
| 7557 | } |
| 7558 | |
| 7559 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7560 | |
| 7561 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7562 | for (auto &Arg : FI.arguments()) |
| 7563 | Arg.info = classifyArgumentType(Arg.type); |
| 7564 | } |
| 7565 | |
| 7566 | namespace { |
| 7567 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7568 | public: |
| 7569 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7570 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 7571 | }; |
| 7572 | } // end anonymous namespace |
| 7573 | |
| 7574 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7575 | // SPARC v9 ABI Implementation. |
| 7576 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7577 | // |
| 7578 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 7579 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 7580 | // the array, structs larger than 16 bytes are passed indirectly. |
| 7581 | // |
| 7582 | // One case requires special care: |
| 7583 | // |
| 7584 | // struct mixed { |
| 7585 | // int i; |
| 7586 | // float f; |
| 7587 | // }; |
| 7588 | // |
| 7589 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 7590 | // parameter array, but the int is passed in an integer register, and the float |
| 7591 | // is passed in a floating point register. This is represented as two arguments |
| 7592 | // with the LLVM IR inreg attribute: |
| 7593 | // |
| 7594 | // declare void f(i32 inreg %i, float inreg %f) |
| 7595 | // |
| 7596 | // The code generator will only allocate 4 bytes from the parameter array for |
| 7597 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 7598 | // bytes. |
| 7599 | // |
| 7600 | namespace { |
| 7601 | class SparcV9ABIInfo : public ABIInfo { |
| 7602 | public: |
| 7603 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7604 | |
| 7605 | private: |
| 7606 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7607 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7608 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7609 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7610 | |
| 7611 | // Coercion type builder for structs passed in registers. The coercion type |
| 7612 | // serves two purposes: |
| 7613 | // |
| 7614 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 7615 | // in registers. |
| 7616 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 7617 | // code generator knows to pass them in floating point registers. |
| 7618 | // |
| 7619 | // We also compute the InReg flag which indicates that the struct contains |
| 7620 | // aligned 32-bit floats. |
| 7621 | // |
| 7622 | struct CoerceBuilder { |
| 7623 | llvm::LLVMContext &Context; |
| 7624 | const llvm::DataLayout &DL; |
| 7625 | SmallVector<llvm::Type*, 8> Elems; |
| 7626 | uint64_t Size; |
| 7627 | bool InReg; |
| 7628 | |
| 7629 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 7630 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 7631 | |
| 7632 | // Pad Elems with integers until Size is ToSize. |
| 7633 | void pad(uint64_t ToSize) { |
| 7634 | assert(ToSize >= Size && "Cannot remove elements"); |
| 7635 | if (ToSize == Size) |
| 7636 | return; |
| 7637 | |
| 7638 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7639 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7640 | if (Aligned > Size && Aligned <= ToSize) { |
| 7641 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 7642 | Size = Aligned; |
| 7643 | } |
| 7644 | |
| 7645 | // Add whole 64-bit words. |
| 7646 | while (Size + 64 <= ToSize) { |
| 7647 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 7648 | Size += 64; |
| 7649 | } |
| 7650 | |
| 7651 | // Final in-word padding. |
| 7652 | if (Size < ToSize) { |
| 7653 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 7654 | Size = ToSize; |
| 7655 | } |
| 7656 | } |
| 7657 | |
| 7658 | // Add a floating point element at Offset. |
| 7659 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 7660 | // Unaligned floats are treated as integers. |
| 7661 | if (Offset % Bits) |
| 7662 | return; |
| 7663 | // The InReg flag is only required if there are any floats < 64 bits. |
| 7664 | if (Bits < 64) |
| 7665 | InReg = true; |
| 7666 | pad(Offset); |
| 7667 | Elems.push_back(Ty); |
| 7668 | Size = Offset + Bits; |
| 7669 | } |
| 7670 | |
| 7671 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 7672 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 7673 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 7674 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 7675 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 7676 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 7677 | switch (ElemTy->getTypeID()) { |
| 7678 | case llvm::Type::StructTyID: |
| 7679 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 7680 | break; |
| 7681 | case llvm::Type::FloatTyID: |
| 7682 | addFloat(ElemOffset, ElemTy, 32); |
| 7683 | break; |
| 7684 | case llvm::Type::DoubleTyID: |
| 7685 | addFloat(ElemOffset, ElemTy, 64); |
| 7686 | break; |
| 7687 | case llvm::Type::FP128TyID: |
| 7688 | addFloat(ElemOffset, ElemTy, 128); |
| 7689 | break; |
| 7690 | case llvm::Type::PointerTyID: |
| 7691 | if (ElemOffset % 64 == 0) { |
| 7692 | pad(ElemOffset); |
| 7693 | Elems.push_back(ElemTy); |
| 7694 | Size += 64; |
| 7695 | } |
| 7696 | break; |
| 7697 | default: |
| 7698 | break; |
| 7699 | } |
| 7700 | } |
| 7701 | } |
| 7702 | |
| 7703 | // Check if Ty is a usable substitute for the coercion type. |
| 7704 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 7705 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7706 | } |
| 7707 | |
| 7708 | // Get the coercion type as a literal struct type. |
| 7709 | llvm::Type *getType() const { |
| 7710 | if (Elems.size() == 1) |
| 7711 | return Elems.front(); |
| 7712 | else |
| 7713 | return llvm::StructType::get(Context, Elems); |
| 7714 | } |
| 7715 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7716 | }; |
| 7717 | } // end anonymous namespace |
| 7718 | |
| 7719 | ABIArgInfo |
| 7720 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 7721 | if (Ty->isVoidType()) |
| 7722 | return ABIArgInfo::getIgnore(); |
| 7723 | |
| 7724 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7725 | |
| 7726 | // Anything too big to fit in registers is passed with an explicit indirect |
| 7727 | // pointer / sret pointer. |
| 7728 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7729 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7730 | |
| 7731 | // Treat an enum type as its underlying type. |
| 7732 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7733 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7734 | |
| 7735 | // Integer types smaller than a register are extended. |
| 7736 | if (Size < 64 && Ty->isIntegerType()) |
| 7737 | return ABIArgInfo::getExtend(); |
| 7738 | |
| 7739 | // Other non-aggregates go in registers. |
| 7740 | if (!isAggregateTypeForABI(Ty)) |
| 7741 | return ABIArgInfo::getDirect(); |
| 7742 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7743 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 7744 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 7745 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7746 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7747 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7748 | // 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] | 7749 | // Build a coercion type from the LLVM struct type. |
| 7750 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7751 | if (!StrTy) |
| 7752 | return ABIArgInfo::getDirect(); |
| 7753 | |
| 7754 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 7755 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7756 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7757 | |
| 7758 | // Try to use the original type for coercion. |
| 7759 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 7760 | |
| 7761 | if (CB.InReg) |
| 7762 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 7763 | else |
| 7764 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7765 | } |
| 7766 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7767 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7768 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7769 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 7770 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7771 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7772 | AI.setCoerceToType(ArgTy); |
| 7773 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7774 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7775 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7776 | CGBuilderTy &Builder = CGF.Builder; |
| 7777 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 7778 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 7779 | |
| 7780 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 7781 | |
| 7782 | Address ArgAddr = Address::invalid(); |
| 7783 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7784 | switch (AI.getKind()) { |
| 7785 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 7786 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 7787 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7788 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 7789 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7790 | case ABIArgInfo::Extend: { |
| 7791 | Stride = SlotSize; |
| 7792 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 7793 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7794 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7795 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7796 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7797 | case ABIArgInfo::Direct: { |
| 7798 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7799 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7800 | ArgAddr = Addr; |
| 7801 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7802 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7803 | |
| 7804 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7805 | Stride = SlotSize; |
| 7806 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 7807 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 7808 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7809 | break; |
| 7810 | |
| 7811 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7812 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
| 7815 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7816 | llvm::Value *NextPtr = |
| 7817 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 7818 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7819 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7820 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7821 | } |
| 7822 | |
| 7823 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7824 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7825 | for (auto &I : FI.arguments()) |
| 7826 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7827 | } |
| 7828 | |
| 7829 | namespace { |
| 7830 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7831 | public: |
| 7832 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7833 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7834 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7835 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7836 | return 14; |
| 7837 | } |
| 7838 | |
| 7839 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7840 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7841 | }; |
| 7842 | } // end anonymous namespace |
| 7843 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7844 | bool |
| 7845 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7846 | llvm::Value *Address) const { |
| 7847 | // This is calculated from the LLVM and GCC tables and verified |
| 7848 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 7849 | |
| 7850 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 7851 | |
| 7852 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 7853 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 7854 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 7855 | |
| 7856 | // 0-31: the 8-byte general-purpose registers |
| 7857 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 7858 | |
| 7859 | // 32-63: f0-31, the 4-byte floating-point registers |
| 7860 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 7861 | |
| 7862 | // Y = 64 |
| 7863 | // PSR = 65 |
| 7864 | // WIM = 66 |
| 7865 | // TBR = 67 |
| 7866 | // PC = 68 |
| 7867 | // NPC = 69 |
| 7868 | // FSR = 70 |
| 7869 | // CSR = 71 |
| 7870 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7871 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7872 | // 72-87: d0-15, the 8-byte floating-point registers |
| 7873 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 7874 | |
| 7875 | return false; |
| 7876 | } |
| 7877 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7878 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7879 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7880 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7881 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7882 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7883 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7884 | |
| 7885 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 7886 | /// it by reference between functions that append to it. |
| 7887 | typedef llvm::SmallString<128> SmallStringEnc; |
| 7888 | |
| 7889 | /// TypeStringCache caches the meta encodings of Types. |
| 7890 | /// |
| 7891 | /// The reason for caching TypeStrings is two fold: |
| 7892 | /// 1. To cache a type's encoding for later uses; |
| 7893 | /// 2. As a means to break recursive member type inclusion. |
| 7894 | /// |
| 7895 | /// A cache Entry can have a Status of: |
| 7896 | /// NonRecursive: The type encoding is not recursive; |
| 7897 | /// Recursive: The type encoding is recursive; |
| 7898 | /// Incomplete: An incomplete TypeString; |
| 7899 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 7900 | /// Recursive type encoding. |
| 7901 | /// |
| 7902 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 7903 | /// as possible. Whilst it may contain types which are recursive, the type |
| 7904 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 7905 | /// the type is encountered. |
| 7906 | /// |
| 7907 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 7908 | /// possible. The type itself is recursive and it may contain other types which |
| 7909 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 7910 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 7911 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 7912 | /// |
| 7913 | /// An Incomplete entry is always a RecordType and only encodes its |
| 7914 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 7915 | /// are placed into the cache during type expansion as a means to identify and |
| 7916 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 7917 | /// the entry becomes IncompleteUsed. |
| 7918 | /// |
| 7919 | /// During the expansion of a RecordType's members: |
| 7920 | /// |
| 7921 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 7922 | /// cached encoding is used; |
| 7923 | /// |
| 7924 | /// If the cache contains a Recursive encoding for the member type, the |
| 7925 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 7926 | /// |
| 7927 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 7928 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 7929 | /// |
| 7930 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 7931 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 7932 | /// it is swapped back in; |
| 7933 | /// |
| 7934 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 7935 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 7936 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 7937 | /// |
| 7938 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 7939 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 7940 | /// Else the member is part of a recursive type and thus the recursion has |
| 7941 | /// been exited too soon for the encoding to be correct for the member. |
| 7942 | /// |
| 7943 | class TypeStringCache { |
| 7944 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 7945 | struct Entry { |
| 7946 | std::string Str; // The encoded TypeString for the type. |
| 7947 | enum Status State; // Information about the encoding in 'Str'. |
| 7948 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 7949 | // during the expansion of RecordType's members. |
| 7950 | }; |
| 7951 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 7952 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 7953 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 7954 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 7955 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7956 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 7957 | bool removeIncomplete(const IdentifierInfo *ID); |
| 7958 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7959 | bool IsRecursive); |
| 7960 | StringRef lookupStr(const IdentifierInfo *ID); |
| 7961 | }; |
| 7962 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7963 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7964 | /// FieldEncoding is a helper for this ordering process. |
| 7965 | class FieldEncoding { |
| 7966 | bool HasName; |
| 7967 | std::string Enc; |
| 7968 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 7969 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 7970 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7971 | bool operator<(const FieldEncoding &rhs) const { |
| 7972 | if (HasName != rhs.HasName) return HasName; |
| 7973 | return Enc < rhs.Enc; |
| 7974 | } |
| 7975 | }; |
| 7976 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7977 | class XCoreABIInfo : public DefaultABIInfo { |
| 7978 | public: |
| 7979 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7980 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7981 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7982 | }; |
| 7983 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7984 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7985 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7986 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7987 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7988 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 7989 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7990 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7991 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7992 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7993 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7994 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 7995 | // TODO: this implementation is likely now redundant with the default |
| 7996 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7997 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7998 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7999 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8000 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8001 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8002 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 8003 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8004 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8005 | // Handle the argument. |
| 8006 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8007 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8008 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8009 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8010 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8011 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8012 | |
| 8013 | Address Val = Address::invalid(); |
| 8014 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8015 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8016 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8017 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8018 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8019 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8020 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8021 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 8022 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8023 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8024 | case ABIArgInfo::Extend: |
| 8025 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8026 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 8027 | ArgSize = CharUnits::fromQuantity( |
| 8028 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8029 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8030 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8031 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8032 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 8033 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 8034 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8035 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8036 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8037 | |
| 8038 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8039 | if (!ArgSize.isZero()) { |
| 8040 | llvm::Value *APN = |
| 8041 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 8042 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8043 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8044 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8045 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8046 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8047 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8048 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8049 | /// into the cache as a means to identify and break recursion. |
| 8050 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8051 | /// be reinserted by removeIncomplete(). |
| 8052 | /// All other types of encoding should have been used rather than arriving here. |
| 8053 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8054 | std::string StubEnc) { |
| 8055 | if (!ID) |
| 8056 | return; |
| 8057 | Entry &E = Map[ID]; |
| 8058 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8059 | "Incorrectly use of addIncomplete"); |
| 8060 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8061 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8062 | E.Str.swap(StubEnc); |
| 8063 | E.State = Incomplete; |
| 8064 | ++IncompleteCount; |
| 8065 | } |
| 8066 | |
| 8067 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8068 | /// must be removed from the cache. |
| 8069 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8070 | /// Returns true if the RecordType was defined recursively. |
| 8071 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8072 | if (!ID) |
| 8073 | return false; |
| 8074 | auto I = Map.find(ID); |
| 8075 | assert(I != Map.end() && "Entry not present"); |
| 8076 | Entry &E = I->second; |
| 8077 | assert( (E.State == Incomplete || |
| 8078 | E.State == IncompleteUsed) && |
| 8079 | "Entry must be an incomplete type"); |
| 8080 | bool IsRecursive = false; |
| 8081 | if (E.State == IncompleteUsed) { |
| 8082 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8083 | IsRecursive = true; |
| 8084 | --IncompleteUsedCount; |
| 8085 | } |
| 8086 | if (E.Swapped.empty()) |
| 8087 | Map.erase(I); |
| 8088 | else { |
| 8089 | // Swap the Recursive back. |
| 8090 | E.Swapped.swap(E.Str); |
| 8091 | E.Swapped.clear(); |
| 8092 | E.State = Recursive; |
| 8093 | } |
| 8094 | --IncompleteCount; |
| 8095 | return IsRecursive; |
| 8096 | } |
| 8097 | |
| 8098 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8099 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8100 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8101 | bool IsRecursive) { |
| 8102 | if (!ID || IncompleteUsedCount) |
| 8103 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8104 | Entry &E = Map[ID]; |
| 8105 | if (IsRecursive && !E.Str.empty()) { |
| 8106 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8107 | "This is not the same Recursive entry"); |
| 8108 | // The parent container was not recursive after all, so we could have used |
| 8109 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8110 | // we started viz: IncompleteCount!=0. |
| 8111 | return; |
| 8112 | } |
| 8113 | assert(E.Str.empty() && "Entry already present"); |
| 8114 | E.Str = Str.str(); |
| 8115 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8116 | } |
| 8117 | |
| 8118 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8119 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8120 | /// encoding is Recursive, return an empty StringRef. |
| 8121 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8122 | if (!ID) |
| 8123 | return StringRef(); // We have no key. |
| 8124 | auto I = Map.find(ID); |
| 8125 | if (I == Map.end()) |
| 8126 | return StringRef(); // We have no encoding. |
| 8127 | Entry &E = I->second; |
| 8128 | if (E.State == Recursive && IncompleteCount) |
| 8129 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8130 | |
| 8131 | if (E.State == Incomplete) { |
| 8132 | // The incomplete type is being used to break out of recursion. |
| 8133 | E.State = IncompleteUsed; |
| 8134 | ++IncompleteUsedCount; |
| 8135 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8136 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
| 8139 | /// The XCore ABI includes a type information section that communicates symbol |
| 8140 | /// type information to the linker. The linker uses this information to verify |
| 8141 | /// safety/correctness of things such as array bound and pointers et al. |
| 8142 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8143 | /// This type information (TypeString) is emitted into meta data for all global |
| 8144 | /// symbols: definitions, declarations, functions & variables. |
| 8145 | /// |
| 8146 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8147 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8148 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8149 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8150 | /// |
| 8151 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8152 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8153 | |
| 8154 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8155 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8156 | CodeGen::CodeGenModule &CGM) const { |
| 8157 | SmallStringEnc Enc; |
| 8158 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8159 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8160 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8161 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8162 | llvm::NamedMDNode *MD = |
| 8163 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8164 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8165 | } |
| 8166 | } |
| 8167 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8168 | //===----------------------------------------------------------------------===// |
| 8169 | // SPIR ABI Implementation |
| 8170 | //===----------------------------------------------------------------------===// |
| 8171 | |
| 8172 | namespace { |
| 8173 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8174 | public: |
| 8175 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8176 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8177 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8178 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8179 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8180 | } // End anonymous namespace. |
| 8181 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8182 | namespace clang { |
| 8183 | namespace CodeGen { |
| 8184 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8185 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8186 | SPIRABI.computeInfo(FI); |
| 8187 | } |
| 8188 | } |
| 8189 | } |
| 8190 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8191 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8192 | return llvm::CallingConv::SPIR_KERNEL; |
| 8193 | } |
| 8194 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8195 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8196 | const CodeGen::CodeGenModule &CGM, |
| 8197 | TypeStringCache &TSC); |
| 8198 | |
| 8199 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8200 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8201 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8202 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8203 | const RecordDecl *RD, |
| 8204 | const CodeGen::CodeGenModule &CGM, |
| 8205 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8206 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8207 | SmallStringEnc Enc; |
| 8208 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8209 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8210 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8211 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8212 | Enc += "b("; |
| 8213 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8214 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8215 | Enc += ':'; |
| 8216 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8217 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8218 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8219 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8220 | Enc += ')'; |
| 8221 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 8222 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8223 | } |
| 8224 | return true; |
| 8225 | } |
| 8226 | |
| 8227 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 8228 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 8229 | /// Union types have their fields ordered according to the ABI. |
| 8230 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 8231 | const CodeGen::CodeGenModule &CGM, |
| 8232 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 8233 | // Append the cached TypeString if we have one. |
| 8234 | StringRef TypeString = TSC.lookupStr(ID); |
| 8235 | if (!TypeString.empty()) { |
| 8236 | Enc += TypeString; |
| 8237 | return true; |
| 8238 | } |
| 8239 | |
| 8240 | // Start to emit an incomplete TypeString. |
| 8241 | size_t Start = Enc.size(); |
| 8242 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 8243 | Enc += '('; |
| 8244 | if (ID) |
| 8245 | Enc += ID->getName(); |
| 8246 | Enc += "){"; |
| 8247 | |
| 8248 | // We collect all encoded fields and order as necessary. |
| 8249 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8250 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 8251 | if (RD && !RD->field_empty()) { |
| 8252 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 8253 | // so that recursive calls to this RecordType will use it whilst building a |
| 8254 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8255 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8256 | std::string StubEnc(Enc.substr(Start).str()); |
| 8257 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 8258 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 8259 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 8260 | (void) TSC.removeIncomplete(ID); |
| 8261 | return false; |
| 8262 | } |
| 8263 | IsRecursive = TSC.removeIncomplete(ID); |
| 8264 | // The ABI requires unions to be sorted but not structures. |
| 8265 | // See FieldEncoding::operator< for sort algorithm. |
| 8266 | if (RT->isUnionType()) |
| 8267 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8268 | // We can now complete the TypeString. |
| 8269 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8270 | for (unsigned I = 0; I != E; ++I) { |
| 8271 | if (I) |
| 8272 | Enc += ','; |
| 8273 | Enc += FE[I].str(); |
| 8274 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8275 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8276 | Enc += '}'; |
| 8277 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 8278 | return true; |
| 8279 | } |
| 8280 | |
| 8281 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 8282 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 8283 | TypeStringCache &TSC, |
| 8284 | const IdentifierInfo *ID) { |
| 8285 | // Append the cached TypeString if we have one. |
| 8286 | StringRef TypeString = TSC.lookupStr(ID); |
| 8287 | if (!TypeString.empty()) { |
| 8288 | Enc += TypeString; |
| 8289 | return true; |
| 8290 | } |
| 8291 | |
| 8292 | size_t Start = Enc.size(); |
| 8293 | Enc += "e("; |
| 8294 | if (ID) |
| 8295 | Enc += ID->getName(); |
| 8296 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8297 | |
| 8298 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8299 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8300 | SmallVector<FieldEncoding, 16> FE; |
| 8301 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 8302 | ++I) { |
| 8303 | SmallStringEnc EnumEnc; |
| 8304 | EnumEnc += "m("; |
| 8305 | EnumEnc += I->getName(); |
| 8306 | EnumEnc += "){"; |
| 8307 | I->getInitVal().toString(EnumEnc); |
| 8308 | EnumEnc += '}'; |
| 8309 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 8310 | } |
| 8311 | std::sort(FE.begin(), FE.end()); |
| 8312 | unsigned E = FE.size(); |
| 8313 | for (unsigned I = 0; I != E; ++I) { |
| 8314 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8315 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8316 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8317 | } |
| 8318 | } |
| 8319 | Enc += '}'; |
| 8320 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 8321 | return true; |
| 8322 | } |
| 8323 | |
| 8324 | /// Appends type's qualifier to Enc. |
| 8325 | /// This is done prior to appending the type's encoding. |
| 8326 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 8327 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 8328 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8329 | int Lookup = 0; |
| 8330 | if (QT.isConstQualified()) |
| 8331 | Lookup += 1<<0; |
| 8332 | if (QT.isRestrictQualified()) |
| 8333 | Lookup += 1<<1; |
| 8334 | if (QT.isVolatileQualified()) |
| 8335 | Lookup += 1<<2; |
| 8336 | Enc += Table[Lookup]; |
| 8337 | } |
| 8338 | |
| 8339 | /// Appends built-in types to Enc. |
| 8340 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 8341 | const char *EncType; |
| 8342 | switch (BT->getKind()) { |
| 8343 | case BuiltinType::Void: |
| 8344 | EncType = "0"; |
| 8345 | break; |
| 8346 | case BuiltinType::Bool: |
| 8347 | EncType = "b"; |
| 8348 | break; |
| 8349 | case BuiltinType::Char_U: |
| 8350 | EncType = "uc"; |
| 8351 | break; |
| 8352 | case BuiltinType::UChar: |
| 8353 | EncType = "uc"; |
| 8354 | break; |
| 8355 | case BuiltinType::SChar: |
| 8356 | EncType = "sc"; |
| 8357 | break; |
| 8358 | case BuiltinType::UShort: |
| 8359 | EncType = "us"; |
| 8360 | break; |
| 8361 | case BuiltinType::Short: |
| 8362 | EncType = "ss"; |
| 8363 | break; |
| 8364 | case BuiltinType::UInt: |
| 8365 | EncType = "ui"; |
| 8366 | break; |
| 8367 | case BuiltinType::Int: |
| 8368 | EncType = "si"; |
| 8369 | break; |
| 8370 | case BuiltinType::ULong: |
| 8371 | EncType = "ul"; |
| 8372 | break; |
| 8373 | case BuiltinType::Long: |
| 8374 | EncType = "sl"; |
| 8375 | break; |
| 8376 | case BuiltinType::ULongLong: |
| 8377 | EncType = "ull"; |
| 8378 | break; |
| 8379 | case BuiltinType::LongLong: |
| 8380 | EncType = "sll"; |
| 8381 | break; |
| 8382 | case BuiltinType::Float: |
| 8383 | EncType = "ft"; |
| 8384 | break; |
| 8385 | case BuiltinType::Double: |
| 8386 | EncType = "d"; |
| 8387 | break; |
| 8388 | case BuiltinType::LongDouble: |
| 8389 | EncType = "ld"; |
| 8390 | break; |
| 8391 | default: |
| 8392 | return false; |
| 8393 | } |
| 8394 | Enc += EncType; |
| 8395 | return true; |
| 8396 | } |
| 8397 | |
| 8398 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 8399 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 8400 | const CodeGen::CodeGenModule &CGM, |
| 8401 | TypeStringCache &TSC) { |
| 8402 | Enc += "p("; |
| 8403 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 8404 | return false; |
| 8405 | Enc += ')'; |
| 8406 | return true; |
| 8407 | } |
| 8408 | |
| 8409 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8410 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 8411 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8412 | const CodeGen::CodeGenModule &CGM, |
| 8413 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 8414 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 8415 | return false; |
| 8416 | Enc += "a("; |
| 8417 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 8418 | CAT->getSize().toStringUnsigned(Enc); |
| 8419 | else |
| 8420 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 8421 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8422 | // The Qualifiers should be attached to the type rather than the array. |
| 8423 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8424 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 8425 | return false; |
| 8426 | Enc += ')'; |
| 8427 | return true; |
| 8428 | } |
| 8429 | |
| 8430 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 8431 | /// and the arguments. |
| 8432 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 8433 | const CodeGen::CodeGenModule &CGM, |
| 8434 | TypeStringCache &TSC) { |
| 8435 | Enc += "f{"; |
| 8436 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 8437 | return false; |
| 8438 | Enc += "}("; |
| 8439 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 8440 | // N.B. we are only interested in the adjusted param types. |
| 8441 | auto I = FPT->param_type_begin(); |
| 8442 | auto E = FPT->param_type_end(); |
| 8443 | if (I != E) { |
| 8444 | do { |
| 8445 | if (!appendType(Enc, *I, CGM, TSC)) |
| 8446 | return false; |
| 8447 | ++I; |
| 8448 | if (I != E) |
| 8449 | Enc += ','; |
| 8450 | } while (I != E); |
| 8451 | if (FPT->isVariadic()) |
| 8452 | Enc += ",va"; |
| 8453 | } else { |
| 8454 | if (FPT->isVariadic()) |
| 8455 | Enc += "va"; |
| 8456 | else |
| 8457 | Enc += '0'; |
| 8458 | } |
| 8459 | } |
| 8460 | Enc += ')'; |
| 8461 | return true; |
| 8462 | } |
| 8463 | |
| 8464 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 8465 | /// type encodings. |
| 8466 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8467 | const CodeGen::CodeGenModule &CGM, |
| 8468 | TypeStringCache &TSC) { |
| 8469 | |
| 8470 | QualType QT = QType.getCanonicalType(); |
| 8471 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8472 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 8473 | // The Qualifiers should be attached to the type rather than the array. |
| 8474 | // Thus we don't call appendQualifier() here. |
| 8475 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 8476 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8477 | appendQualifier(Enc, QT); |
| 8478 | |
| 8479 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 8480 | return appendBuiltinType(Enc, BT); |
| 8481 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8482 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 8483 | return appendPointerType(Enc, PT, CGM, TSC); |
| 8484 | |
| 8485 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 8486 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 8487 | |
| 8488 | if (const RecordType *RT = QT->getAsStructureType()) |
| 8489 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8490 | |
| 8491 | if (const RecordType *RT = QT->getAsUnionType()) |
| 8492 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8493 | |
| 8494 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 8495 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 8496 | |
| 8497 | return false; |
| 8498 | } |
| 8499 | |
| 8500 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8501 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 8502 | if (!D) |
| 8503 | return false; |
| 8504 | |
| 8505 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8506 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 8507 | return false; |
| 8508 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 8509 | } |
| 8510 | |
| 8511 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8512 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 8513 | return false; |
| 8514 | QualType QT = VD->getType().getCanonicalType(); |
| 8515 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 8516 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8517 | // The Qualifiers should be attached to the type rather than the array. |
| 8518 | // Thus we don't call appendQualifier() here. |
| 8519 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8520 | } |
| 8521 | return appendType(Enc, QT, CGM, TSC); |
| 8522 | } |
| 8523 | return false; |
| 8524 | } |
| 8525 | |
| 8526 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8527 | //===----------------------------------------------------------------------===// |
| 8528 | // Driver code |
| 8529 | //===----------------------------------------------------------------------===// |
| 8530 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8531 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 8532 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8533 | } |
| 8534 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 8535 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8536 | if (TheTargetCodeGenInfo) |
| 8537 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8538 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8539 | // Helper to set the unique_ptr while still keeping the return value. |
| 8540 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 8541 | this->TheTargetCodeGenInfo.reset(P); |
| 8542 | return *P; |
| 8543 | }; |
| 8544 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 8545 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 8546 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8547 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8548 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8549 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 8550 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8551 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 8552 | case llvm::Triple::mips: |
| 8553 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 8554 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8555 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 8556 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8557 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 8558 | case llvm::Triple::mips64: |
| 8559 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8560 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8561 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 8562 | case llvm::Triple::avr: |
| 8563 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 8564 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 8565 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 8566 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8567 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 8568 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8569 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 8570 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 8571 | return SetCGInfo( |
| 8572 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8573 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8574 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8575 | } |
| 8576 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8577 | case llvm::Triple::wasm32: |
| 8578 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8579 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8580 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8581 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 8582 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8583 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8584 | case llvm::Triple::thumbeb: { |
| 8585 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 8586 | return SetCGInfo( |
| 8587 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 8588 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8589 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8590 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 8591 | StringRef ABIStr = getTarget().getABI(); |
| 8592 | if (ABIStr == "apcs-gnu") |
| 8593 | Kind = ARMABIInfo::APCS; |
| 8594 | else if (ABIStr == "aapcs16") |
| 8595 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 8596 | else if (CodeGenOpts.FloatABI == "hard" || |
| 8597 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8598 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 8599 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8600 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8601 | Kind = ARMABIInfo::AAPCS_VFP; |
| 8602 | |
| 8603 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 8604 | } |
| 8605 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8606 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8607 | return SetCGInfo( |
| 8608 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 8609 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8610 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8611 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8612 | if (getTarget().getABI() == "elfv2") |
| 8613 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8614 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8615 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8616 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8617 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 8618 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8619 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8620 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8621 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 8622 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8623 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8624 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8625 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8626 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8627 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8628 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8629 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 8630 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8631 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8632 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 8633 | case llvm::Triple::nvptx: |
| 8634 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8635 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 8636 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8637 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8638 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8639 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8640 | case llvm::Triple::systemz: { |
| 8641 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8642 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8643 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 8644 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8645 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 8646 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8647 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8648 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8649 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8650 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 8651 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8652 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 8653 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 8654 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8655 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8656 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 8657 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8658 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8659 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8660 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 8661 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8662 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 8663 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8664 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8665 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8666 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8667 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8668 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8669 | X86AVXABILevel AVXLevel = |
| 8670 | (ABI == "avx512" |
| 8671 | ? X86AVXABILevel::AVX512 |
| 8672 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8673 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8674 | switch (Triple.getOS()) { |
| 8675 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8676 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 8677 | case llvm::Triple::PS4: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8678 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8679 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8680 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8681 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8682 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 8683 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8684 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 8685 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8686 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8687 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8688 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 8689 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8690 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 8691 | case llvm::Triple::sparc: |
| 8692 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8693 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8694 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8695 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8696 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8697 | case llvm::Triple::spir: |
| 8698 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8699 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8700 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8701 | } |