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, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1040 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1041 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1042 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1043 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1044 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1045 | return 4; |
| 1046 | } |
| 1047 | |
| 1048 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1049 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1050 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1051 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1052 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1053 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1054 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1055 | } |
| 1056 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1057 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1058 | std::string &Constraints, |
| 1059 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1060 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1061 | std::vector<LValue> &ResultRegDests, |
| 1062 | std::string &AsmString, |
| 1063 | unsigned NumOutputs) const override; |
| 1064 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1065 | llvm::Constant * |
| 1066 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1067 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1068 | (0x06 << 8) | // .+0x08 |
| 1069 | ('F' << 16) | |
| 1070 | ('T' << 24); |
| 1071 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1072 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1073 | |
| 1074 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1075 | return "movl\t%ebp, %ebp" |
| 1076 | "\t\t## marker for objc_retainAutoreleaseReturnValue"; |
| 1077 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1078 | }; |
| 1079 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1080 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1081 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1082 | /// Rewrite input constraint references after adding some output constraints. |
| 1083 | /// In the case where there is one output and one input and we add one output, |
| 1084 | /// we need to replace all operand references greater than or equal to 1: |
| 1085 | /// mov $0, $1 |
| 1086 | /// mov eax, $1 |
| 1087 | /// The result will be: |
| 1088 | /// mov $0, $2 |
| 1089 | /// mov eax, $2 |
| 1090 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1091 | unsigned NumNewOuts, |
| 1092 | std::string &AsmString) { |
| 1093 | std::string Buf; |
| 1094 | llvm::raw_string_ostream OS(Buf); |
| 1095 | size_t Pos = 0; |
| 1096 | while (Pos < AsmString.size()) { |
| 1097 | size_t DollarStart = AsmString.find('$', Pos); |
| 1098 | if (DollarStart == std::string::npos) |
| 1099 | DollarStart = AsmString.size(); |
| 1100 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1101 | if (DollarEnd == std::string::npos) |
| 1102 | DollarEnd = AsmString.size(); |
| 1103 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1104 | Pos = DollarEnd; |
| 1105 | size_t NumDollars = DollarEnd - DollarStart; |
| 1106 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1107 | // We have an operand reference. |
| 1108 | size_t DigitStart = Pos; |
| 1109 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1110 | if (DigitEnd == std::string::npos) |
| 1111 | DigitEnd = AsmString.size(); |
| 1112 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1113 | unsigned OperandIndex; |
| 1114 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1115 | if (OperandIndex >= FirstIn) |
| 1116 | OperandIndex += NumNewOuts; |
| 1117 | OS << OperandIndex; |
| 1118 | } else { |
| 1119 | OS << OperandStr; |
| 1120 | } |
| 1121 | Pos = DigitEnd; |
| 1122 | } |
| 1123 | } |
| 1124 | AsmString = std::move(OS.str()); |
| 1125 | } |
| 1126 | |
| 1127 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1128 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1129 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1130 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1131 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1132 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1133 | unsigned NumOutputs) const { |
| 1134 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1135 | |
| 1136 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1137 | // larger. |
| 1138 | if (!Constraints.empty()) |
| 1139 | Constraints += ','; |
| 1140 | if (RetWidth <= 32) { |
| 1141 | Constraints += "={eax}"; |
| 1142 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1143 | } else { |
| 1144 | // Use the 'A' constraint for EAX:EDX. |
| 1145 | Constraints += "=A"; |
| 1146 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1147 | } |
| 1148 | |
| 1149 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1150 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1151 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1152 | |
| 1153 | // Coerce the integer by bitcasting the return slot pointer. |
| 1154 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1155 | CoerceTy->getPointerTo())); |
| 1156 | ResultRegDests.push_back(ReturnSlot); |
| 1157 | |
| 1158 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1159 | } |
| 1160 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1161 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1162 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1163 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1164 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1165 | uint64_t Size = Context.getTypeSize(Ty); |
| 1166 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1167 | // For i386, type must be register sized. |
| 1168 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1169 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1170 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1171 | |
| 1172 | if (Ty->isVectorType()) { |
| 1173 | // 64- and 128- bit vectors inside structures are not returned in |
| 1174 | // registers. |
| 1175 | if (Size == 64 || Size == 128) |
| 1176 | return false; |
| 1177 | |
| 1178 | return true; |
| 1179 | } |
| 1180 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1181 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1182 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1183 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1184 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1185 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1186 | return true; |
| 1187 | |
| 1188 | // Arrays are treated like records. |
| 1189 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1190 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1191 | |
| 1192 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1193 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1194 | if (!RT) return false; |
| 1195 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1196 | // FIXME: Traverse bases here too. |
| 1197 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1198 | // Structure types are passed in register if all fields would be |
| 1199 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1200 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1201 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1202 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1203 | continue; |
| 1204 | |
| 1205 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1206 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1207 | return false; |
| 1208 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1209 | return true; |
| 1210 | } |
| 1211 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1212 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1213 | // Treat complex types as the element type. |
| 1214 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1215 | Ty = CTy->getElementType(); |
| 1216 | |
| 1217 | // Check for a type which we know has a simple scalar argument-passing |
| 1218 | // convention without any padding. (We're specifically looking for 32 |
| 1219 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1220 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1221 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1222 | return false; |
| 1223 | |
| 1224 | uint64_t Size = Context.getTypeSize(Ty); |
| 1225 | return Size == 32 || Size == 64; |
| 1226 | } |
| 1227 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1228 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1229 | uint64_t &Size) { |
| 1230 | for (const auto *FD : RD->fields()) { |
| 1231 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1232 | // argument is smaller than 32-bits, expanding the struct will create |
| 1233 | // alignment padding. |
| 1234 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1235 | return false; |
| 1236 | |
| 1237 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1238 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1239 | // counts as "basic" is more complicated than what we were doing previously. |
| 1240 | if (FD->isBitField()) |
| 1241 | return false; |
| 1242 | |
| 1243 | Size += Context.getTypeSize(FD->getType()); |
| 1244 | } |
| 1245 | return true; |
| 1246 | } |
| 1247 | |
| 1248 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1249 | uint64_t &Size) { |
| 1250 | // Don't do this if there are any non-empty bases. |
| 1251 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1252 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1253 | Size)) |
| 1254 | return false; |
| 1255 | } |
| 1256 | if (!addFieldSizes(Context, RD, Size)) |
| 1257 | return false; |
| 1258 | return true; |
| 1259 | } |
| 1260 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1261 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1262 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1263 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1264 | /// optimizations. |
| 1265 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1266 | // We can only expand structure types. |
| 1267 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1268 | if (!RT) |
| 1269 | return false; |
| 1270 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1271 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1272 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1273 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1274 | // On non-Windows, we have to conservatively match our old bitcode |
| 1275 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1276 | if (!CXXRD->isCLike()) |
| 1277 | return false; |
| 1278 | } else { |
| 1279 | // Don't do this for dynamic classes. |
| 1280 | if (CXXRD->isDynamicClass()) |
| 1281 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1282 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1283 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1284 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1285 | } else { |
| 1286 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1287 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | // We can do this if there was no alignment padding. |
| 1291 | return Size == getContext().getTypeSize(Ty); |
| 1292 | } |
| 1293 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1294 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1295 | // If the return value is indirect, then the hidden argument is consuming one |
| 1296 | // integer register. |
| 1297 | if (State.FreeRegs) { |
| 1298 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1299 | if (!IsMCUABI) |
| 1300 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1301 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1302 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1305 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1306 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1307 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1308 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1309 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1310 | const Type *Base = nullptr; |
| 1311 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1312 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1313 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1314 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1315 | // The LLVM struct type for such an aggregate should lower properly. |
| 1316 | return ABIArgInfo::getDirect(); |
| 1317 | } |
| 1318 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1319 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1320 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1321 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1322 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1323 | |
| 1324 | // 128-bit vectors are a special case; they are returned in |
| 1325 | // registers and we need to make sure to pick a type the LLVM |
| 1326 | // backend will like. |
| 1327 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1328 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1329 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1330 | |
| 1331 | // Always return in register if it fits in a general purpose |
| 1332 | // register, or if it is 64 bits and has a single element. |
| 1333 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1334 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1335 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1336 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1337 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1338 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1342 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1343 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1344 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1345 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1346 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1347 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1348 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1349 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1350 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1351 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1352 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1353 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1354 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1355 | // Ignore empty structs/unions. |
| 1356 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1357 | return ABIArgInfo::getIgnore(); |
| 1358 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1359 | // Small structures which are register sized are generally returned |
| 1360 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1361 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1362 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1363 | |
| 1364 | // As a special-case, if the struct is a "single-element" struct, and |
| 1365 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1366 | // floating-point register. (MSVC does not apply this special case.) |
| 1367 | // We apply a similar transformation for pointer types to improve the |
| 1368 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1369 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1370 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1371 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1372 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1373 | |
| 1374 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1375 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1376 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1379 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1380 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1381 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1382 | // Treat an enum type as its underlying type. |
| 1383 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1384 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1385 | |
| 1386 | return (RetTy->isPromotableIntegerType() ? |
| 1387 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1390 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1391 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1392 | } |
| 1393 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1394 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1395 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1396 | if (!RT) |
| 1397 | return 0; |
| 1398 | const RecordDecl *RD = RT->getDecl(); |
| 1399 | |
| 1400 | // If this is a C++ record, check the bases first. |
| 1401 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1402 | for (const auto &I : CXXRD->bases()) |
| 1403 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1404 | return false; |
| 1405 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1406 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1407 | QualType FT = i->getType(); |
| 1408 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1409 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1410 | return true; |
| 1411 | |
| 1412 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1413 | return true; |
| 1414 | } |
| 1415 | |
| 1416 | return false; |
| 1417 | } |
| 1418 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1419 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1420 | unsigned Align) const { |
| 1421 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1422 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1423 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1424 | return 0; // Use default alignment. |
| 1425 | |
| 1426 | // On non-Darwin, the stack type alignment is always 4. |
| 1427 | if (!IsDarwinVectorABI) { |
| 1428 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1429 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1430 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1431 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1432 | // 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] | 1433 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1434 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1435 | return 16; |
| 1436 | |
| 1437 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1440 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1441 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1442 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1443 | if (State.FreeRegs) { |
| 1444 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1445 | if (!IsMCUABI) |
| 1446 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1447 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1448 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1449 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1450 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1451 | // Compute the byval alignment. |
| 1452 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1453 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1454 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1455 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1456 | |
| 1457 | // If the stack alignment is less than the type alignment, realign the |
| 1458 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1459 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1460 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1461 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1464 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1465 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1466 | if (!T) |
| 1467 | T = Ty.getTypePtr(); |
| 1468 | |
| 1469 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1470 | BuiltinType::Kind K = BT->getKind(); |
| 1471 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1472 | return Float; |
| 1473 | } |
| 1474 | return Integer; |
| 1475 | } |
| 1476 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1477 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1478 | if (!IsSoftFloatABI) { |
| 1479 | Class C = classify(Ty); |
| 1480 | if (C == Float) |
| 1481 | return false; |
| 1482 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1483 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1484 | unsigned Size = getContext().getTypeSize(Ty); |
| 1485 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1486 | |
| 1487 | if (SizeInRegs == 0) |
| 1488 | return false; |
| 1489 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1490 | if (!IsMCUABI) { |
| 1491 | if (SizeInRegs > State.FreeRegs) { |
| 1492 | State.FreeRegs = 0; |
| 1493 | return false; |
| 1494 | } |
| 1495 | } else { |
| 1496 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1497 | // earlier parameters that are passed on the stack. Also, |
| 1498 | // it does not allow passing >8-byte structs in-register, |
| 1499 | // even if there are 3 free registers available. |
| 1500 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1501 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1502 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1503 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1504 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1505 | return true; |
| 1506 | } |
| 1507 | |
| 1508 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
| 1509 | bool &InReg, |
| 1510 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1511 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1512 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1513 | // (HFAs) have already been dealt with at this point. |
| 1514 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1515 | return false; |
| 1516 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1517 | NeedsPadding = false; |
| 1518 | InReg = !IsMCUABI; |
| 1519 | |
| 1520 | if (!updateFreeRegs(Ty, State)) |
| 1521 | return false; |
| 1522 | |
| 1523 | if (IsMCUABI) |
| 1524 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1525 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1526 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1527 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1528 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1529 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1530 | NeedsPadding = true; |
| 1531 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1532 | return false; |
| 1533 | } |
| 1534 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1535 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1538 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1539 | if (!updateFreeRegs(Ty, State)) |
| 1540 | return false; |
| 1541 | |
| 1542 | if (IsMCUABI) |
| 1543 | return false; |
| 1544 | |
| 1545 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1546 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1547 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1548 | if (getContext().getTypeSize(Ty) > 32) |
| 1549 | return false; |
| 1550 | |
| 1551 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
| 1552 | Ty->isReferenceType()); |
| 1553 | } |
| 1554 | |
| 1555 | return true; |
| 1556 | } |
| 1557 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1558 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1559 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1560 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1561 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1562 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1563 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1564 | // Check with the C++ ABI first. |
| 1565 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1566 | if (RT) { |
| 1567 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1568 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1569 | return getIndirectResult(Ty, false, State); |
| 1570 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1571 | // The field index doesn't matter, we'll fix it up later. |
| 1572 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1573 | } |
| 1574 | } |
| 1575 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1576 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1577 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1578 | const Type *Base = nullptr; |
| 1579 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1580 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1581 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1582 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1583 | if (State.FreeSSERegs >= NumElts) { |
| 1584 | State.FreeSSERegs -= NumElts; |
| 1585 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1586 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1587 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1588 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1589 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1593 | // Structures with flexible arrays are always indirect. |
| 1594 | // FIXME: This should not be byval! |
| 1595 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1596 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1597 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1598 | // Ignore empty structs/unions on non-Windows. |
| 1599 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1600 | return ABIArgInfo::getIgnore(); |
| 1601 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1602 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1603 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1604 | bool NeedsPadding = false; |
| 1605 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1606 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1607 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1608 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1609 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1610 | if (InReg) |
| 1611 | return ABIArgInfo::getDirectInReg(Result); |
| 1612 | else |
| 1613 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1614 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1615 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1616 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1617 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1618 | // of those arguments will match the struct. This is important because the |
| 1619 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1620 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1621 | // Don't do this for the MCU if there are still free integer registers |
| 1622 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1623 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1624 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1625 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1626 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1627 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1628 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1629 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1630 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1631 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1634 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1635 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1636 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1637 | if (IsDarwinVectorABI) { |
| 1638 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1639 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1640 | (Size == 64 && VT->getNumElements() == 1)) |
| 1641 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1642 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1643 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1644 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1645 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1646 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1647 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1648 | return ABIArgInfo::getDirect(); |
| 1649 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1650 | |
| 1651 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1652 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1653 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1654 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1655 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1656 | |
| 1657 | if (Ty->isPromotableIntegerType()) { |
| 1658 | if (InReg) |
| 1659 | return ABIArgInfo::getExtendInReg(); |
| 1660 | return ABIArgInfo::getExtend(); |
| 1661 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1662 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1663 | if (InReg) |
| 1664 | return ABIArgInfo::getDirectInReg(); |
| 1665 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1668 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1669 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1670 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1671 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1672 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1673 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1674 | // registers. |
| 1675 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1676 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1677 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1678 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1679 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1680 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1681 | // First pass do all the vector types. |
| 1682 | const Type *Base = nullptr; |
| 1683 | uint64_t NumElts = 0; |
| 1684 | const QualType& Ty = I.type; |
| 1685 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1686 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1687 | if (State.FreeSSERegs >= NumElts) { |
| 1688 | State.FreeSSERegs -= NumElts; |
| 1689 | I.info = ABIArgInfo::getDirect(); |
| 1690 | } else { |
| 1691 | I.info = classifyArgumentType(Ty, State); |
| 1692 | } |
| 1693 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1694 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1695 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1696 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1697 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1698 | // Second pass, do the rest! |
| 1699 | const Type *Base = nullptr; |
| 1700 | uint64_t NumElts = 0; |
| 1701 | const QualType& Ty = I.type; |
| 1702 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1703 | |
| 1704 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1705 | // Assign true HVAs (non vector/native FP types). |
| 1706 | if (State.FreeSSERegs >= NumElts) { |
| 1707 | State.FreeSSERegs -= NumElts; |
| 1708 | I.info = getDirectX86Hva(); |
| 1709 | } else { |
| 1710 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1711 | } |
| 1712 | } else if (!IsHva) { |
| 1713 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1714 | I.info = classifyArgumentType(Ty, State); |
| 1715 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1716 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1720 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1721 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1722 | if (IsMCUABI) |
| 1723 | State.FreeRegs = 3; |
| 1724 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1725 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1726 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1727 | State.FreeRegs = 2; |
| 1728 | State.FreeSSERegs = 6; |
| 1729 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1730 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1731 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1732 | State.FreeRegs = 5; |
| 1733 | State.FreeSSERegs = 8; |
| 1734 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1735 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1736 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1737 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1738 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1739 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1740 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1741 | // return value was sret and put it in a register ourselves if appropriate. |
| 1742 | if (State.FreeRegs) { |
| 1743 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1744 | if (!IsMCUABI) |
| 1745 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1746 | } |
| 1747 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1748 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1749 | // The chain argument effectively gives us another free register. |
| 1750 | if (FI.isChainCall()) |
| 1751 | ++State.FreeRegs; |
| 1752 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1753 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1754 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1755 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1756 | } else { |
| 1757 | // If not vectorcall, revert to normal behavior. |
| 1758 | for (auto &I : FI.arguments()) { |
| 1759 | I.info = classifyArgumentType(I.type, State); |
| 1760 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1761 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1765 | // all the memory arguments to use inalloca. |
| 1766 | if (UsedInAlloca) |
| 1767 | rewriteWithInAlloca(FI); |
| 1768 | } |
| 1769 | |
| 1770 | void |
| 1771 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1772 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1773 | QualType Type) const { |
| 1774 | // Arguments are always 4-byte-aligned. |
| 1775 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1776 | |
| 1777 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1778 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1779 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1780 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1781 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1782 | // Insert padding bytes to respect alignment. |
| 1783 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1784 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1785 | if (StackOffset != FieldEnd) { |
| 1786 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1787 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1788 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1789 | FrameFields.push_back(Ty); |
| 1790 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1793 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1794 | // Leave ignored and inreg arguments alone. |
| 1795 | switch (Info.getKind()) { |
| 1796 | case ABIArgInfo::InAlloca: |
| 1797 | return true; |
| 1798 | case ABIArgInfo::Indirect: |
| 1799 | assert(Info.getIndirectByVal()); |
| 1800 | return true; |
| 1801 | case ABIArgInfo::Ignore: |
| 1802 | return false; |
| 1803 | case ABIArgInfo::Direct: |
| 1804 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1805 | if (Info.getInReg()) |
| 1806 | return false; |
| 1807 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1808 | case ABIArgInfo::Expand: |
| 1809 | case ABIArgInfo::CoerceAndExpand: |
| 1810 | // These are aggregate types which are never passed in registers when |
| 1811 | // inalloca is involved. |
| 1812 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1813 | } |
| 1814 | llvm_unreachable("invalid enum"); |
| 1815 | } |
| 1816 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1817 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1818 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1819 | |
| 1820 | // Build a packed struct type for all of the arguments in memory. |
| 1821 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1822 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1823 | // The stack alignment is always 4. |
| 1824 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1825 | |
| 1826 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1827 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1828 | |
| 1829 | // Put 'this' into the struct before 'sret', if necessary. |
| 1830 | bool IsThisCall = |
| 1831 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1832 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1833 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1834 | isArgInAlloca(I->info)) { |
| 1835 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1836 | ++I; |
| 1837 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1838 | |
| 1839 | // 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] | 1840 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1841 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1842 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1843 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1844 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1848 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1849 | ++I; |
| 1850 | |
| 1851 | // Put arguments passed in memory into the struct. |
| 1852 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1853 | if (isArgInAlloca(I->info)) |
| 1854 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1858 | /*isPacked=*/true), |
| 1859 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1862 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1863 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1864 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1865 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1866 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1867 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1868 | // |
| 1869 | // Just messing with TypeInfo like this works because we never pass |
| 1870 | // anything indirectly. |
| 1871 | TypeInfo.second = CharUnits::fromQuantity( |
| 1872 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1873 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1874 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1875 | TypeInfo, CharUnits::fromQuantity(4), |
| 1876 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1879 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1880 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1881 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1882 | |
| 1883 | switch (Opts.getStructReturnConvention()) { |
| 1884 | case CodeGenOptions::SRCK_Default: |
| 1885 | break; |
| 1886 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1887 | return false; |
| 1888 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1889 | return true; |
| 1890 | } |
| 1891 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1892 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1893 | return true; |
| 1894 | |
| 1895 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1896 | case llvm::Triple::DragonFly: |
| 1897 | case llvm::Triple::FreeBSD: |
| 1898 | case llvm::Triple::OpenBSD: |
| 1899 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1900 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1901 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1902 | default: |
| 1903 | return false; |
| 1904 | } |
| 1905 | } |
| 1906 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1907 | void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1908 | llvm::GlobalValue *GV, |
| 1909 | CodeGen::CodeGenModule &CGM) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1910 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1911 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1912 | // Get the LLVM function. |
| 1913 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1914 | |
| 1915 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1916 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1917 | B.addStackAlignmentAttr(16); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1918 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1919 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1920 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1921 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1922 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1923 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1927 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1928 | CodeGen::CodeGenFunction &CGF, |
| 1929 | llvm::Value *Address) const { |
| 1930 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1931 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1932 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1933 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1934 | // 0-7 are the eight integer registers; the order is different |
| 1935 | // on Darwin (for EH), but the range is the same. |
| 1936 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1937 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1938 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1939 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1940 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1941 | // These have size 16, which is sizeof(long double) on |
| 1942 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1943 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1944 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1945 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1946 | } else { |
| 1947 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1948 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1949 | Builder.CreateAlignedStore( |
| 1950 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1951 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1952 | |
| 1953 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1954 | // These have size 12, which is sizeof(long double) on |
| 1955 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1956 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1957 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1958 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1959 | |
| 1960 | return false; |
| 1961 | } |
| 1962 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1963 | //===----------------------------------------------------------------------===// |
| 1964 | // X86-64 ABI Implementation |
| 1965 | //===----------------------------------------------------------------------===// |
| 1966 | |
| 1967 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1968 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1969 | /// The AVX ABI level for X86 targets. |
| 1970 | enum class X86AVXABILevel { |
| 1971 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1972 | AVX, |
| 1973 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1974 | }; |
| 1975 | |
| 1976 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1977 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1978 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1979 | case X86AVXABILevel::AVX512: |
| 1980 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1981 | case X86AVXABILevel::AVX: |
| 1982 | return 256; |
| 1983 | case X86AVXABILevel::None: |
| 1984 | return 128; |
| 1985 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1986 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1989 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1990 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1991 | enum Class { |
| 1992 | Integer = 0, |
| 1993 | SSE, |
| 1994 | SSEUp, |
| 1995 | X87, |
| 1996 | X87Up, |
| 1997 | ComplexX87, |
| 1998 | NoClass, |
| 1999 | Memory |
| 2000 | }; |
| 2001 | |
| 2002 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2003 | /// |
| 2004 | /// Merge an accumulating classification \arg Accum with a field |
| 2005 | /// classification \arg Field. |
| 2006 | /// |
| 2007 | /// \param Accum - The accumulating classification. This should |
| 2008 | /// always be either NoClass or the result of a previous merge |
| 2009 | /// call. In addition, this should never be Memory (the caller |
| 2010 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2011 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2012 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2013 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2014 | /// |
| 2015 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2016 | /// final MEMORY or SSE classes when necessary. |
| 2017 | /// |
| 2018 | /// \param AggregateSize - The size of the current aggregate in |
| 2019 | /// the classification process. |
| 2020 | /// |
| 2021 | /// \param Lo - The classification for the parts of the type |
| 2022 | /// residing in the low word of the containing object. |
| 2023 | /// |
| 2024 | /// \param Hi - The classification for the parts of the type |
| 2025 | /// residing in the higher words of the containing object. |
| 2026 | /// |
| 2027 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2028 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2029 | /// classify - Determine the x86_64 register classes in which the |
| 2030 | /// given type T should be passed. |
| 2031 | /// |
| 2032 | /// \param Lo - The classification for the parts of the type |
| 2033 | /// residing in the low word of the containing object. |
| 2034 | /// |
| 2035 | /// \param Hi - The classification for the parts of the type |
| 2036 | /// residing in the high word of the containing object. |
| 2037 | /// |
| 2038 | /// \param OffsetBase - The bit offset of this type in the |
| 2039 | /// containing object. Some parameters are classified different |
| 2040 | /// depending on whether they straddle an eightbyte boundary. |
| 2041 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2042 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2043 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2044 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2045 | /// If a word is unused its result will be NoClass; if a type should |
| 2046 | /// be passed in Memory then at least the classification of \arg Lo |
| 2047 | /// will be Memory. |
| 2048 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2049 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2050 | /// |
| 2051 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2052 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2053 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2054 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2055 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2056 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2057 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2058 | unsigned IROffset, QualType SourceTy, |
| 2059 | unsigned SourceOffset) const; |
| 2060 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2061 | unsigned IROffset, QualType SourceTy, |
| 2062 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2063 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2064 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2065 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2066 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2067 | |
| 2068 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2069 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2070 | /// |
| 2071 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2072 | /// available. |
| 2073 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2074 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2075 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2076 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2077 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2078 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2079 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2080 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2081 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2082 | unsigned &NeededSSE) const; |
| 2083 | |
| 2084 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2085 | unsigned &NeededSSE) const; |
| 2086 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2087 | bool IsIllegalVectorType(QualType Ty) const; |
| 2088 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2089 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2090 | /// unfortunately in ways that were not always consistent with |
| 2091 | /// certain previous compilers. In particular, platforms which |
| 2092 | /// required strict binary compatibility with older versions of GCC |
| 2093 | /// may need to exempt themselves. |
| 2094 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2095 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2098 | /// GCC classifies <1 x long long> as SSE but compatibility with older clang |
| 2099 | // compilers require us to classify it as INTEGER. |
| 2100 | bool classifyIntegerMMXAsSSE() const { |
| 2101 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2102 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2103 | return false; |
| 2104 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2105 | return false; |
| 2106 | return true; |
| 2107 | } |
| 2108 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2109 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2110 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2111 | // 64-bit hardware. |
| 2112 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2113 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2114 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2115 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2116 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2117 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2118 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2119 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2120 | bool isPassedUsingAVXType(QualType type) const { |
| 2121 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2122 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2123 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2124 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2125 | if (info.isDirect()) { |
| 2126 | llvm::Type *ty = info.getCoerceToType(); |
| 2127 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2128 | return (vectorTy->getBitWidth() > 128); |
| 2129 | } |
| 2130 | return false; |
| 2131 | } |
| 2132 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2133 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2134 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2135 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2136 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2137 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2138 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2139 | |
| 2140 | bool has64BitPointers() const { |
| 2141 | return Has64BitPointers; |
| 2142 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2143 | |
| 2144 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 2145 | ArrayRef<llvm::Type*> scalars, |
| 2146 | bool asReturnValue) const override { |
| 2147 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2148 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2149 | bool isSwiftErrorInRegister() const override { |
| 2150 | return true; |
| 2151 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2152 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2153 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2154 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2155 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2156 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2157 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2158 | : SwiftABIInfo(CGT), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2159 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2160 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2161 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2162 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2163 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2164 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2165 | |
| 2166 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2167 | // FIXME: Assumes vectorcall is in use. |
| 2168 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2169 | } |
| 2170 | |
| 2171 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2172 | uint64_t NumMembers) const override { |
| 2173 | // FIXME: Assumes vectorcall is in use. |
| 2174 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2175 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2176 | |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2177 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 2178 | ArrayRef<llvm::Type *> scalars, |
| 2179 | bool asReturnValue) const override { |
| 2180 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2181 | } |
| 2182 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2183 | bool isSwiftErrorInRegister() const override { |
| 2184 | return true; |
| 2185 | } |
| 2186 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2187 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2188 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2189 | bool IsVectorCall, bool IsRegCall) const; |
| 2190 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2191 | const ABIArgInfo ¤t) const; |
| 2192 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2193 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2194 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2195 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2196 | }; |
| 2197 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2198 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2199 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2200 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2201 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2202 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2203 | const X86_64ABIInfo &getABIInfo() const { |
| 2204 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2205 | } |
| 2206 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2207 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2208 | return 7; |
| 2209 | } |
| 2210 | |
| 2211 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2212 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2213 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2214 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2215 | // 0-15 are the 16 integer registers. |
| 2216 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2217 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2218 | return false; |
| 2219 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2220 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2221 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2222 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2223 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2224 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2225 | } |
| 2226 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2227 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2228 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2229 | // The default CC on x86-64 sets %al to the number of SSA |
| 2230 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2231 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2232 | // that when AVX types are involved: the ABI explicitly states it is |
| 2233 | // undefined, and it doesn't work in practice because of how the ABI |
| 2234 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2235 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2236 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2237 | for (CallArgList::const_iterator |
| 2238 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2239 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2240 | HasAVXType = true; |
| 2241 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2242 | } |
| 2243 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2244 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2245 | if (!HasAVXType) |
| 2246 | return true; |
| 2247 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2248 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2249 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2252 | llvm::Constant * |
| 2253 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2254 | unsigned Sig; |
| 2255 | if (getABIInfo().has64BitPointers()) |
| 2256 | Sig = (0xeb << 0) | // jmp rel8 |
| 2257 | (0x0a << 8) | // .+0x0c |
| 2258 | ('F' << 16) | |
| 2259 | ('T' << 24); |
| 2260 | else |
| 2261 | Sig = (0xeb << 0) | // jmp rel8 |
| 2262 | (0x06 << 8) | // .+0x08 |
| 2263 | ('F' << 16) | |
| 2264 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2265 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2266 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2267 | |
| 2268 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2269 | CodeGen::CodeGenModule &CGM) const override { |
| 2270 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2271 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2272 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2273 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2274 | } |
| 2275 | } |
| 2276 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2277 | }; |
| 2278 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2279 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2280 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2281 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2282 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2283 | |
| 2284 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 2285 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2286 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 2287 | // If the argument contains a space, enclose it in quotes. |
| 2288 | if (Lib.find(" ") != StringRef::npos) |
| 2289 | Opt += "\"" + Lib.str() + "\""; |
| 2290 | else |
| 2291 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2292 | } |
| 2293 | }; |
| 2294 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2295 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2296 | // If the argument does not end in .lib, automatically add the suffix. |
| 2297 | // If the argument contains a space, enclose it in quotes. |
| 2298 | // This matches the behavior of MSVC. |
| 2299 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2300 | std::string ArgStr = Quote ? "\"" : ""; |
| 2301 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 2302 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2303 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2304 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2305 | return ArgStr; |
| 2306 | } |
| 2307 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2308 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2309 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2310 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2311 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2312 | unsigned NumRegisterParameters) |
| 2313 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2314 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2315 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2316 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2317 | CodeGen::CodeGenModule &CGM) const override; |
| 2318 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2319 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2320 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2321 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2322 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2323 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2324 | |
| 2325 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2326 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2327 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2328 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2329 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2330 | }; |
| 2331 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2332 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 2333 | llvm::GlobalValue *GV, |
| 2334 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 2335 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2336 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 2337 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2338 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2339 | Fn->addFnAttr("stack-probe-size", |
| 2340 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | } |
| 2344 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2345 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2346 | llvm::GlobalValue *GV, |
| 2347 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2348 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2349 | |
| 2350 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2351 | } |
| 2352 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2353 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2354 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2355 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2356 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2357 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2358 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2359 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2360 | CodeGen::CodeGenModule &CGM) const override; |
| 2361 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2362 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2363 | return 7; |
| 2364 | } |
| 2365 | |
| 2366 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2367 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2368 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2369 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2370 | // 0-15 are the 16 integer registers. |
| 2371 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2372 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2373 | return false; |
| 2374 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2375 | |
| 2376 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2377 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2378 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2379 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2380 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2381 | |
| 2382 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2383 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2384 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2385 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2386 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2387 | }; |
| 2388 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2389 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2390 | llvm::GlobalValue *GV, |
| 2391 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2392 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2393 | |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2394 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2395 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2396 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2397 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2398 | } |
| 2399 | } |
| 2400 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2401 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2402 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2403 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2404 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2405 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2406 | Class &Hi) const { |
| 2407 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2408 | // |
| 2409 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2410 | // memory. |
| 2411 | // |
| 2412 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2413 | // memory. |
| 2414 | // |
| 2415 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2416 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2417 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2418 | // ABI working for processors that don't support the __m256 type. |
| 2419 | // |
| 2420 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2421 | // |
| 2422 | // Some of these are enforced by the merging logic. Others can arise |
| 2423 | // only with unions; for example: |
| 2424 | // union { _Complex double; unsigned; } |
| 2425 | // |
| 2426 | // Note that clauses (b) and (c) were added in 0.98. |
| 2427 | // |
| 2428 | if (Hi == Memory) |
| 2429 | Lo = Memory; |
| 2430 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2431 | Lo = Memory; |
| 2432 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2433 | Lo = Memory; |
| 2434 | if (Hi == SSEUp && Lo != SSE) |
| 2435 | Hi = SSE; |
| 2436 | } |
| 2437 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2438 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2439 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2440 | // classified recursively so that always two fields are |
| 2441 | // considered. The resulting class is calculated according to |
| 2442 | // the classes of the fields in the eightbyte: |
| 2443 | // |
| 2444 | // (a) If both classes are equal, this is the resulting class. |
| 2445 | // |
| 2446 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2447 | // the other class. |
| 2448 | // |
| 2449 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2450 | // class. |
| 2451 | // |
| 2452 | // (d) If one of the classes is INTEGER, the result is the |
| 2453 | // INTEGER. |
| 2454 | // |
| 2455 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2456 | // MEMORY is used as class. |
| 2457 | // |
| 2458 | // (f) Otherwise class SSE is used. |
| 2459 | |
| 2460 | // Accum should never be memory (we should have returned) or |
| 2461 | // ComplexX87 (because this cannot be passed in a structure). |
| 2462 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2463 | "Invalid accumulated classification during merge."); |
| 2464 | if (Accum == Field || Field == NoClass) |
| 2465 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2466 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2467 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2468 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2469 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2470 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2471 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2472 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2473 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2474 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2475 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2478 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2479 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2480 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2481 | // Class pairs with appropriate constructor methods for the various |
| 2482 | // situations. |
| 2483 | |
| 2484 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2485 | // shouldn't be passed in registers for example, so there is no chance they |
| 2486 | // can straddle an eightbyte. Verify & simplify. |
| 2487 | |
| 2488 | Lo = Hi = NoClass; |
| 2489 | |
| 2490 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2491 | Current = Memory; |
| 2492 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2493 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2494 | BuiltinType::Kind k = BT->getKind(); |
| 2495 | |
| 2496 | if (k == BuiltinType::Void) { |
| 2497 | Current = NoClass; |
| 2498 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2499 | Lo = Integer; |
| 2500 | Hi = Integer; |
| 2501 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2502 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2503 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2504 | Current = SSE; |
| 2505 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2506 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2507 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2508 | Lo = SSE; |
| 2509 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2510 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2511 | Lo = X87; |
| 2512 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2513 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2514 | Current = SSE; |
| 2515 | } else |
| 2516 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2517 | } |
| 2518 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2519 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2520 | return; |
| 2521 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2522 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2523 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2524 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2525 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2526 | return; |
| 2527 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2528 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2529 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2530 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2531 | return; |
| 2532 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2533 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2534 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2535 | if (Ty->isMemberFunctionPointerType()) { |
| 2536 | if (Has64BitPointers) { |
| 2537 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2538 | // Lo and Hi now. |
| 2539 | Lo = Hi = Integer; |
| 2540 | } else { |
| 2541 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2542 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2543 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2544 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2545 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2546 | Lo = Hi = Integer; |
| 2547 | } else { |
| 2548 | Current = Integer; |
| 2549 | } |
| 2550 | } |
| 2551 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2552 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2553 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2554 | return; |
| 2555 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2556 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2557 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2558 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2559 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2560 | // gcc passes the following as integer: |
| 2561 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2562 | // 2 bytes - <2 x char>, <1 x short> |
| 2563 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2564 | Current = Integer; |
| 2565 | |
| 2566 | // If this type crosses an eightbyte boundary, it should be |
| 2567 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2568 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2569 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2570 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2571 | Hi = Lo; |
| 2572 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2573 | QualType ElementType = VT->getElementType(); |
| 2574 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2575 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2576 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2577 | return; |
| 2578 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2579 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2580 | // pass them as integer. For platforms where clang is the de facto |
| 2581 | // platform compiler, we must continue to use integer. |
| 2582 | if (!classifyIntegerMMXAsSSE() && |
| 2583 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2584 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2585 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2586 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2587 | Current = Integer; |
| 2588 | else |
| 2589 | Current = SSE; |
| 2590 | |
| 2591 | // If this type crosses an eightbyte boundary, it should be |
| 2592 | // split. |
| 2593 | if (OffsetBase && OffsetBase != 64) |
| 2594 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2595 | } else if (Size == 128 || |
| 2596 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2597 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2598 | // least significant one belongs to class SSE and all the others to class |
| 2599 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2600 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2601 | // This design isn't correct for 256-bits, but since there're no cases |
| 2602 | // where the upper parts would need to be inspected, avoid adding |
| 2603 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2604 | // |
| 2605 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2606 | // registers if they are "named", i.e. not part of the "..." of a |
| 2607 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2608 | // |
| 2609 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2610 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2611 | Lo = SSE; |
| 2612 | Hi = SSEUp; |
| 2613 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2614 | return; |
| 2615 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2616 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2617 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2618 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2619 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2620 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2621 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2622 | if (Size <= 64) |
| 2623 | Current = Integer; |
| 2624 | else if (Size <= 128) |
| 2625 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2626 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2627 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2628 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2629 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2630 | } else if (ET == getContext().LongDoubleTy) { |
| 2631 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2632 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2633 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2634 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2635 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2636 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2637 | Lo = Hi = SSE; |
| 2638 | else |
| 2639 | llvm_unreachable("unexpected long double representation!"); |
| 2640 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2641 | |
| 2642 | // If this complex type crosses an eightbyte boundary then it |
| 2643 | // should be split. |
| 2644 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2645 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2646 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2647 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2648 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2649 | return; |
| 2650 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2651 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2652 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2653 | // Arrays are treated like structures. |
| 2654 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2655 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2656 | |
| 2657 | // 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] | 2658 | // than eight eightbytes, ..., it has class MEMORY. |
| 2659 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2660 | return; |
| 2661 | |
| 2662 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2663 | // fields, it has class MEMORY. |
| 2664 | // |
| 2665 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2666 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2667 | return; |
| 2668 | |
| 2669 | // Otherwise implement simplified merge. We could be smarter about |
| 2670 | // this, but it isn't worth it and would be harder to verify. |
| 2671 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2672 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2673 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2674 | |
| 2675 | // The only case a 256-bit wide vector could be used is when the array |
| 2676 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2677 | // 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] | 2678 | // |
| 2679 | if (Size > 128 && |
| 2680 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2681 | return; |
| 2682 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2683 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2684 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2685 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2686 | Lo = merge(Lo, FieldLo); |
| 2687 | Hi = merge(Hi, FieldHi); |
| 2688 | if (Lo == Memory || Hi == Memory) |
| 2689 | break; |
| 2690 | } |
| 2691 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2692 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2693 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2694 | return; |
| 2695 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2696 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2697 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2698 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2699 | |
| 2700 | // 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] | 2701 | // than eight eightbytes, ..., it has class MEMORY. |
| 2702 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2703 | return; |
| 2704 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2705 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2706 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2707 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2708 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2709 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2710 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2711 | const RecordDecl *RD = RT->getDecl(); |
| 2712 | |
| 2713 | // Assume variable sized types are passed in memory. |
| 2714 | if (RD->hasFlexibleArrayMember()) |
| 2715 | return; |
| 2716 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2717 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2718 | |
| 2719 | // Reset Lo class, this will be recomputed. |
| 2720 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2721 | |
| 2722 | // If this is a C++ record, classify the bases first. |
| 2723 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2724 | for (const auto &I : CXXRD->bases()) { |
| 2725 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2726 | "Unexpected base class!"); |
| 2727 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2728 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2729 | |
| 2730 | // Classify this field. |
| 2731 | // |
| 2732 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2733 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2734 | // initialized to class NO_CLASS. |
| 2735 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2736 | uint64_t Offset = |
| 2737 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2738 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2739 | Lo = merge(Lo, FieldLo); |
| 2740 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2741 | if (Lo == Memory || Hi == Memory) { |
| 2742 | postMerge(Size, Lo, Hi); |
| 2743 | return; |
| 2744 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2745 | } |
| 2746 | } |
| 2747 | |
| 2748 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2749 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2750 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2751 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2752 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2753 | bool BitField = i->isBitField(); |
| 2754 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2755 | // Ignore padding bit-fields. |
| 2756 | if (BitField && i->isUnnamedBitfield()) |
| 2757 | continue; |
| 2758 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2759 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2760 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2761 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2762 | // The only case a 256-bit wide vector could be used is when the struct |
| 2763 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2764 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2765 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2766 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2767 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2768 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2769 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2770 | return; |
| 2771 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2772 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2773 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2774 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2775 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2776 | return; |
| 2777 | } |
| 2778 | |
| 2779 | // Classify this field. |
| 2780 | // |
| 2781 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2782 | // exceeds a single eightbyte, each is classified |
| 2783 | // separately. Each eightbyte gets initialized to class |
| 2784 | // NO_CLASS. |
| 2785 | Class FieldLo, FieldHi; |
| 2786 | |
| 2787 | // Bit-fields require special handling, they do not force the |
| 2788 | // structure to be passed in memory even if unaligned, and |
| 2789 | // therefore they can straddle an eightbyte. |
| 2790 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2791 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2792 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2793 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2794 | |
| 2795 | uint64_t EB_Lo = Offset / 64; |
| 2796 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2797 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2798 | if (EB_Lo) { |
| 2799 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2800 | FieldLo = NoClass; |
| 2801 | FieldHi = Integer; |
| 2802 | } else { |
| 2803 | FieldLo = Integer; |
| 2804 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2805 | } |
| 2806 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2807 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2808 | Lo = merge(Lo, FieldLo); |
| 2809 | Hi = merge(Hi, FieldHi); |
| 2810 | if (Lo == Memory || Hi == Memory) |
| 2811 | break; |
| 2812 | } |
| 2813 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2814 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2818 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2819 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2820 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2821 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2822 | // Treat an enum type as its underlying type. |
| 2823 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2824 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2825 | |
| 2826 | return (Ty->isPromotableIntegerType() ? |
| 2827 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2828 | } |
| 2829 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2830 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2833 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2834 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2835 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2836 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2837 | if (Size <= 64 || Size > LargestVector) |
| 2838 | return true; |
| 2839 | } |
| 2840 | |
| 2841 | return false; |
| 2842 | } |
| 2843 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2844 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2845 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2846 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2847 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2848 | // |
| 2849 | // This assumption is optimistic, as there could be free registers available |
| 2850 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2851 | // the argument in the free register. This does not seem to happen currently, |
| 2852 | // but this code would be much safer if we could mark the argument with |
| 2853 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2854 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2855 | // Treat an enum type as its underlying type. |
| 2856 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2857 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2858 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2859 | return (Ty->isPromotableIntegerType() ? |
| 2860 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2861 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2862 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2863 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2864 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2865 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2866 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2867 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2868 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2869 | |
| 2870 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2871 | // is important for good codegen. |
| 2872 | // |
| 2873 | // We do this by coercing the value into a scalar type which the backend can |
| 2874 | // handle naturally (i.e., without using byval). |
| 2875 | // |
| 2876 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2877 | // free integer registers. Doing this when there are free integer registers |
| 2878 | // would require more care, as we would have to ensure that the coerced value |
| 2879 | // did not claim the unused register. That would require either reording the |
| 2880 | // arguments to the function (so that any subsequent inreg values came first), |
| 2881 | // or only doing this optimization when there were no following arguments that |
| 2882 | // might be inreg. |
| 2883 | // |
| 2884 | // We currently expect it to be rare (particularly in well written code) for |
| 2885 | // arguments to be passed on the stack when there are still free integer |
| 2886 | // registers available (this would typically imply large structs being passed |
| 2887 | // by value), so this seems like a fair tradeoff for now. |
| 2888 | // |
| 2889 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2890 | // attributes. See PR12193. |
| 2891 | if (freeIntRegs == 0) { |
| 2892 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2893 | |
| 2894 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2895 | // type, which will end up on the stack (with alignment 8). |
| 2896 | if (Align == 8 && Size <= 64) |
| 2897 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2898 | Size)); |
| 2899 | } |
| 2900 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2901 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2904 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2905 | /// 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] | 2906 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2907 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2908 | // vectors; strip them off if present. |
| 2909 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2910 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2911 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2912 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2913 | if (isa<llvm::VectorType>(IRType) || |
| 2914 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2915 | return IRType; |
| 2916 | |
| 2917 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2918 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2919 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2920 | |
| 2921 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2922 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2923 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2926 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2927 | /// is known to either be off the end of the specified type or being in |
| 2928 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2929 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2930 | /// classification that put one of the two halves in the INTEGER class. |
| 2931 | /// |
| 2932 | /// It is conservatively correct to return false. |
| 2933 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2934 | unsigned EndBit, ASTContext &Context) { |
| 2935 | // If the bytes being queried are off the end of the type, there is no user |
| 2936 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2937 | // types that don't contain interesting padding. |
| 2938 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2939 | if (TySize <= StartBit) |
| 2940 | return true; |
| 2941 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2942 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2943 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2944 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2945 | |
| 2946 | // Check each element to see if the element overlaps with the queried range. |
| 2947 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2948 | // If the element is after the span we care about, then we're done.. |
| 2949 | unsigned EltOffset = i*EltSize; |
| 2950 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2951 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2952 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2953 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2954 | EndBit-EltOffset, Context)) |
| 2955 | return false; |
| 2956 | } |
| 2957 | // If it overlaps no elements, then it is safe to process as padding. |
| 2958 | return true; |
| 2959 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2960 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2961 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2962 | const RecordDecl *RD = RT->getDecl(); |
| 2963 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2964 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2965 | // If this is a C++ record, check the bases first. |
| 2966 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2967 | for (const auto &I : CXXRD->bases()) { |
| 2968 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2969 | "Unexpected base class!"); |
| 2970 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2971 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2972 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2973 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2974 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2975 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2976 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2977 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2978 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2979 | EndBit-BaseOffset, Context)) |
| 2980 | return false; |
| 2981 | } |
| 2982 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2983 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2984 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2985 | // this could be sped up a lot by being smarter about queried fields, |
| 2986 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2987 | // much. |
| 2988 | unsigned idx = 0; |
| 2989 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2990 | i != e; ++i, ++idx) { |
| 2991 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2992 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2993 | // If we found a field after the region we care about, then we're done. |
| 2994 | if (FieldOffset >= EndBit) break; |
| 2995 | |
| 2996 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2997 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2998 | Context)) |
| 2999 | return false; |
| 3000 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3001 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3002 | // If nothing in this record overlapped the area of interest, then we're |
| 3003 | // clean. |
| 3004 | return true; |
| 3005 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3006 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3007 | return false; |
| 3008 | } |
| 3009 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3010 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3011 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3012 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3013 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3014 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3015 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3016 | // Base case if we find a float. |
| 3017 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3018 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3019 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3020 | // 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] | 3021 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3022 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3023 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3024 | IROffset -= SL->getElementOffset(Elt); |
| 3025 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3026 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3027 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3028 | // 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] | 3029 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3030 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3031 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3032 | IROffset -= IROffset/EltSize*EltSize; |
| 3033 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3034 | } |
| 3035 | |
| 3036 | return false; |
| 3037 | } |
| 3038 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3039 | |
| 3040 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3041 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3042 | llvm::Type *X86_64ABIInfo:: |
| 3043 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3044 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3045 | // 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] | 3046 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3047 | // structs that contain 3 floats. |
| 3048 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3049 | SourceOffset*8+64, getContext())) |
| 3050 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3051 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3052 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3053 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3054 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3055 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3056 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3057 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3058 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3059 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3060 | } |
| 3061 | |
| 3062 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3063 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3064 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3065 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3066 | /// 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] | 3067 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3068 | /// etc). |
| 3069 | /// |
| 3070 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3071 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3072 | /// the 8-byte value references. PrefType may be null. |
| 3073 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3074 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3075 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3076 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3077 | llvm::Type *X86_64ABIInfo:: |
| 3078 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3079 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3080 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3081 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3082 | if (IROffset == 0) { |
| 3083 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3084 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3085 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3086 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3087 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3088 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3089 | // goodness in the source type is just tail padding. This is allowed to |
| 3090 | // kick in for struct {double,int} on the int, but not on |
| 3091 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3092 | // have to do this analysis on the source type because we can't depend on |
| 3093 | // unions being lowered a specific way etc. |
| 3094 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3095 | IRType->isIntegerTy(32) || |
| 3096 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3097 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3098 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3099 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3100 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3101 | SourceOffset*8+64, getContext())) |
| 3102 | return IRType; |
| 3103 | } |
| 3104 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3105 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3106 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3107 | // 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] | 3108 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3109 | if (IROffset < SL->getSizeInBytes()) { |
| 3110 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3111 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3112 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3113 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3114 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3115 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3116 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3117 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3118 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3119 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3120 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3121 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3122 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3123 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3124 | } |
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 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3127 | // 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] | 3128 | unsigned TySizeInBytes = |
| 3129 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3130 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3131 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3132 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3133 | // It is always safe to classify this as an integer type up to i64 that |
| 3134 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3135 | return llvm::IntegerType::get(getVMContext(), |
| 3136 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3139 | |
| 3140 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3141 | /// be used as elements of a two register pair to pass or return, return a |
| 3142 | /// first class aggregate to represent them. For example, if the low part of |
| 3143 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3144 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3145 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3146 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3147 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3148 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3149 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3150 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3151 | // the second element at offset 8. Check for this: |
| 3152 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3153 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3154 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3155 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3156 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3157 | // To handle this, we have to increase the size of the low part so that the |
| 3158 | // second element will start at an 8 byte offset. We can't increase the size |
| 3159 | // of the second element because it might make us access off the end of the |
| 3160 | // struct. |
| 3161 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3162 | // There are usually two sorts of types the ABI generation code can produce |
| 3163 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3164 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3165 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3166 | // Promote these to a larger type. |
| 3167 | if (Lo->isFloatTy()) |
| 3168 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3169 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3170 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3171 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3172 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3173 | } |
| 3174 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3175 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3176 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3177 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3178 | // Verify that the second element is at an 8-byte offset. |
| 3179 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3180 | "Invalid x86-64 argument pair!"); |
| 3181 | return Result; |
| 3182 | } |
| 3183 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3184 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3185 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3186 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3187 | // classification algorithm. |
| 3188 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3189 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3190 | |
| 3191 | // Check some invariants. |
| 3192 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3193 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3194 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3195 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3196 | switch (Lo) { |
| 3197 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3198 | if (Hi == NoClass) |
| 3199 | return ABIArgInfo::getIgnore(); |
| 3200 | // If the low part is just padding, it takes no register, leave ResType |
| 3201 | // null. |
| 3202 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3203 | "Unknown missing lo part"); |
| 3204 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3205 | |
| 3206 | case SSEUp: |
| 3207 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3208 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3209 | |
| 3210 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3211 | // hidden argument. |
| 3212 | case Memory: |
| 3213 | return getIndirectReturnResult(RetTy); |
| 3214 | |
| 3215 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3216 | // available register of the sequence %rax, %rdx is used. |
| 3217 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3218 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3219 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3220 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3221 | // so that the parameter gets the right LLVM IR attributes. |
| 3222 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3223 | // Treat an enum type as its underlying type. |
| 3224 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3225 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3226 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3227 | if (RetTy->isIntegralOrEnumerationType() && |
| 3228 | RetTy->isPromotableIntegerType()) |
| 3229 | return ABIArgInfo::getExtend(); |
| 3230 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3231 | break; |
| 3232 | |
| 3233 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3234 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3235 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3236 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3237 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3238 | |
| 3239 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3240 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3241 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3242 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3243 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3244 | |
| 3245 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3246 | // part of the value is returned in %st0 and the imaginary part in |
| 3247 | // %st1. |
| 3248 | case ComplexX87: |
| 3249 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3250 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3251 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3252 | break; |
| 3253 | } |
| 3254 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3255 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3256 | switch (Hi) { |
| 3257 | // Memory was handled previously and X87 should |
| 3258 | // never occur as a hi class. |
| 3259 | case Memory: |
| 3260 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3261 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3262 | |
| 3263 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3264 | case NoClass: |
| 3265 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3266 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3267 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3268 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3269 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3270 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3271 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3272 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3273 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3274 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3275 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3276 | break; |
| 3277 | |
| 3278 | // 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] | 3279 | // is passed in the next available eightbyte chunk if the last used |
| 3280 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3281 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3282 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3283 | case SSEUp: |
| 3284 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3285 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3286 | break; |
| 3287 | |
| 3288 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3289 | // returned together with the previous X87 value in %st0. |
| 3290 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3291 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3292 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3293 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3294 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3295 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3296 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3297 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3298 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3299 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3300 | break; |
| 3301 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3302 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3303 | // 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] | 3304 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3305 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3306 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3307 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3308 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3309 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3312 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3313 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3314 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3315 | const |
| 3316 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3317 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3318 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3319 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3320 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3321 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3322 | // Check some invariants. |
| 3323 | // FIXME: Enforce these by construction. |
| 3324 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3325 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3326 | |
| 3327 | neededInt = 0; |
| 3328 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3329 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3330 | switch (Lo) { |
| 3331 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3332 | if (Hi == NoClass) |
| 3333 | return ABIArgInfo::getIgnore(); |
| 3334 | // If the low part is just padding, it takes no register, leave ResType |
| 3335 | // null. |
| 3336 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3337 | "Unknown missing lo part"); |
| 3338 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3339 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3340 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3341 | // on the stack. |
| 3342 | case Memory: |
| 3343 | |
| 3344 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3345 | // COMPLEX_X87, it is passed in memory. |
| 3346 | case X87: |
| 3347 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3348 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3349 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3350 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3351 | |
| 3352 | case SSEUp: |
| 3353 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3354 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3355 | |
| 3356 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3357 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3358 | // and %r9 is used. |
| 3359 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3360 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3361 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3362 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3363 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3364 | |
| 3365 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3366 | // so that the parameter gets the right LLVM IR attributes. |
| 3367 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3368 | // Treat an enum type as its underlying type. |
| 3369 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3370 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3371 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3372 | if (Ty->isIntegralOrEnumerationType() && |
| 3373 | Ty->isPromotableIntegerType()) |
| 3374 | return ABIArgInfo::getExtend(); |
| 3375 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3376 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3377 | break; |
| 3378 | |
| 3379 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3380 | // available SSE register is used, the registers are taken in the |
| 3381 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3382 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3383 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3384 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3385 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3386 | break; |
| 3387 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3388 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3389 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3390 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3391 | switch (Hi) { |
| 3392 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3393 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3394 | // which is passed in memory. |
| 3395 | case Memory: |
| 3396 | case X87: |
| 3397 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3398 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3399 | |
| 3400 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3401 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3402 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3403 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3404 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3405 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3406 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3407 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3408 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3409 | break; |
| 3410 | |
| 3411 | // X87Up generally doesn't occur here (long double is passed in |
| 3412 | // memory), except in situations involving unions. |
| 3413 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3414 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3415 | HighPart = GetSSETypeAtOffset(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); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3419 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3420 | ++neededSSE; |
| 3421 | break; |
| 3422 | |
| 3423 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3424 | // 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] | 3425 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3426 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3427 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3428 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3429 | break; |
| 3430 | } |
| 3431 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3432 | // If a high part was specified, merge it together with the low part. It is |
| 3433 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3434 | // first class struct aggregate with the high and low part: {low, high} |
| 3435 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3436 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3437 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3438 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3439 | } |
| 3440 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3441 | ABIArgInfo |
| 3442 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3443 | unsigned &NeededSSE) const { |
| 3444 | auto RT = Ty->getAs<RecordType>(); |
| 3445 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3446 | |
| 3447 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3448 | return getIndirectReturnResult(Ty); |
| 3449 | |
| 3450 | // Sum up bases |
| 3451 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3452 | if (CXXRD->isDynamicClass()) { |
| 3453 | NeededInt = NeededSSE = 0; |
| 3454 | return getIndirectReturnResult(Ty); |
| 3455 | } |
| 3456 | |
| 3457 | for (const auto &I : CXXRD->bases()) |
| 3458 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3459 | .isIndirect()) { |
| 3460 | NeededInt = NeededSSE = 0; |
| 3461 | return getIndirectReturnResult(Ty); |
| 3462 | } |
| 3463 | } |
| 3464 | |
| 3465 | // Sum up members |
| 3466 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3467 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3468 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3469 | .isIndirect()) { |
| 3470 | NeededInt = NeededSSE = 0; |
| 3471 | return getIndirectReturnResult(Ty); |
| 3472 | } |
| 3473 | } else { |
| 3474 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3475 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3476 | LocalNeededSSE, true) |
| 3477 | .isIndirect()) { |
| 3478 | NeededInt = NeededSSE = 0; |
| 3479 | return getIndirectReturnResult(Ty); |
| 3480 | } |
| 3481 | NeededInt += LocalNeededInt; |
| 3482 | NeededSSE += LocalNeededSSE; |
| 3483 | } |
| 3484 | } |
| 3485 | |
| 3486 | return ABIArgInfo::getDirect(); |
| 3487 | } |
| 3488 | |
| 3489 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3490 | unsigned &NeededInt, |
| 3491 | unsigned &NeededSSE) const { |
| 3492 | |
| 3493 | NeededInt = 0; |
| 3494 | NeededSSE = 0; |
| 3495 | |
| 3496 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3497 | } |
| 3498 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3499 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3500 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3501 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3502 | |
| 3503 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3504 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3505 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3506 | unsigned NeededInt, NeededSSE; |
| 3507 | |
| 3508 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3509 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3510 | FI.getReturnInfo() = |
| 3511 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3512 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3513 | FreeIntRegs -= NeededInt; |
| 3514 | FreeSSERegs -= NeededSSE; |
| 3515 | } else { |
| 3516 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3517 | } |
| 3518 | } else if (!getCXXABI().classifyReturnType(FI)) |
| 3519 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3520 | |
| 3521 | // If the return value is indirect, then the hidden argument is consuming one |
| 3522 | // integer register. |
| 3523 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3524 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3525 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3526 | // The chain argument effectively gives us another free register. |
| 3527 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3528 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3529 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3530 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3531 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3532 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3533 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3534 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3535 | it != ie; ++it, ++ArgNo) { |
| 3536 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3537 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3538 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3539 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3540 | else |
| 3541 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3542 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3543 | |
| 3544 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3545 | // eightbyte of an argument, the whole argument is passed on the |
| 3546 | // stack. If registers have already been assigned for some |
| 3547 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3548 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3549 | FreeIntRegs -= NeededInt; |
| 3550 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3551 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3552 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3553 | } |
| 3554 | } |
| 3555 | } |
| 3556 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3557 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3558 | Address VAListAddr, QualType Ty) { |
| 3559 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3560 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3561 | llvm::Value *overflow_arg_area = |
| 3562 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3563 | |
| 3564 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3565 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3566 | // It isn't stated explicitly in the standard, but in practice we use |
| 3567 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3568 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3569 | if (Align > CharUnits::fromQuantity(8)) { |
| 3570 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3571 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
| 3574 | // 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] | 3575 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3576 | llvm::Value *Res = |
| 3577 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3578 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3579 | |
| 3580 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3581 | // l->overflow_arg_area + sizeof(type). |
| 3582 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3583 | // an 8 byte boundary. |
| 3584 | |
| 3585 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3586 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3587 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3588 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3589 | "overflow_arg_area.next"); |
| 3590 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3591 | |
| 3592 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3593 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3596 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3597 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3598 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3599 | // struct { |
| 3600 | // i32 gp_offset; |
| 3601 | // i32 fp_offset; |
| 3602 | // i8* overflow_arg_area; |
| 3603 | // i8* reg_save_area; |
| 3604 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3605 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3606 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3607 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3608 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3609 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3610 | |
| 3611 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3612 | // in the registers. If not go to step 7. |
| 3613 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3614 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3615 | |
| 3616 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3617 | // general purpose registers needed to pass type and num_fp to hold |
| 3618 | // the number of floating point registers needed. |
| 3619 | |
| 3620 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3621 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3622 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3623 | // |
| 3624 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3625 | // register save space). |
| 3626 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3627 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3628 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3629 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3630 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3631 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3632 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3633 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3634 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3635 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3636 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3640 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3641 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3642 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3643 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3644 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3645 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3646 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3647 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3648 | } |
| 3649 | |
| 3650 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3651 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3652 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3653 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3654 | |
| 3655 | // Emit code to load the value if it was passed in registers. |
| 3656 | |
| 3657 | CGF.EmitBlock(InRegBlock); |
| 3658 | |
| 3659 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3660 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3661 | // copying to a temporary location in case the parameter is passed |
| 3662 | // in different register classes or requires an alignment greater |
| 3663 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3664 | // |
| 3665 | // FIXME: This really results in shameful code when we end up needing to |
| 3666 | // collect arguments from different places; often what should result in a |
| 3667 | // simple assembling of a structure from scattered addresses has many more |
| 3668 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3669 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3670 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3671 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3672 | "reg_save_area"); |
| 3673 | |
| 3674 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3675 | if (neededInt && neededSSE) { |
| 3676 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3677 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3678 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3679 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3680 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3681 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3682 | llvm::Type *TyLo = ST->getElementType(0); |
| 3683 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3684 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3685 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3686 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3687 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3688 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3689 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3690 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3691 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3692 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3693 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3694 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3695 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3696 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3697 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3698 | CGF.Builder.CreateStore(V, |
| 3699 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3700 | |
| 3701 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3702 | V = CGF.Builder.CreateAlignedLoad( |
| 3703 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3704 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3705 | CharUnits Offset = CharUnits::fromQuantity( |
| 3706 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3707 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3708 | |
| 3709 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3710 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3711 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3712 | CharUnits::fromQuantity(8)); |
| 3713 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3714 | |
| 3715 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3716 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3717 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3718 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3719 | CharUnits TyAlign = SizeAlign.second; |
| 3720 | |
| 3721 | // Copy into a temporary if the type is more aligned than the |
| 3722 | // register save area. |
| 3723 | if (TyAlign.getQuantity() > 8) { |
| 3724 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3725 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3726 | RegAddr = Tmp; |
| 3727 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3728 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3729 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3730 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3731 | CharUnits::fromQuantity(16)); |
| 3732 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3733 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3734 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3735 | // SSE registers are spaced 16 bytes apart in the register save |
| 3736 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3737 | // The ABI isn't explicit about this, but it seems reasonable |
| 3738 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3739 | // naturally 16-byte aligned and the prologue is expected to store |
| 3740 | // all the SSE registers to the RSA. |
| 3741 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3742 | CharUnits::fromQuantity(16)); |
| 3743 | Address RegAddrHi = |
| 3744 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3745 | CharUnits::fromQuantity(16)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3746 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3747 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3748 | llvm::Value *V; |
| 3749 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3750 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3751 | V = CGF.Builder.CreateLoad( |
| 3752 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3753 | CGF.Builder.CreateStore(V, |
| 3754 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3755 | V = CGF.Builder.CreateLoad( |
| 3756 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3757 | CGF.Builder.CreateStore(V, |
| 3758 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3759 | |
| 3760 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3764 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3765 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3766 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3767 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3768 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3769 | gp_offset_p); |
| 3770 | } |
| 3771 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3772 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3773 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3774 | fp_offset_p); |
| 3775 | } |
| 3776 | CGF.EmitBranch(ContBlock); |
| 3777 | |
| 3778 | // Emit code to load the value if it was passed in memory. |
| 3779 | |
| 3780 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3781 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3782 | |
| 3783 | // Return the appropriate result. |
| 3784 | |
| 3785 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3786 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3787 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3788 | return ResAddr; |
| 3789 | } |
| 3790 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3791 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3792 | QualType Ty) const { |
| 3793 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3794 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3795 | CharUnits::fromQuantity(8), |
| 3796 | /*allowHigherAlign*/ false); |
| 3797 | } |
| 3798 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3799 | ABIArgInfo |
| 3800 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3801 | const ABIArgInfo ¤t) const { |
| 3802 | // Assumes vectorCall calling convention. |
| 3803 | const Type *Base = nullptr; |
| 3804 | uint64_t NumElts = 0; |
| 3805 | |
| 3806 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3807 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3808 | FreeSSERegs -= NumElts; |
| 3809 | return getDirectX86Hva(); |
| 3810 | } |
| 3811 | return current; |
| 3812 | } |
| 3813 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3814 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3815 | bool IsReturnType, bool IsVectorCall, |
| 3816 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3817 | |
| 3818 | if (Ty->isVoidType()) |
| 3819 | return ABIArgInfo::getIgnore(); |
| 3820 | |
| 3821 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3822 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3823 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3824 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3825 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3826 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3827 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3828 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3829 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3830 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3831 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3832 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3836 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
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 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3839 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3840 | const Type *Base = nullptr; |
| 3841 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3842 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3843 | // other targets. |
| 3844 | if ((IsVectorCall || IsRegCall) && |
| 3845 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3846 | if (IsRegCall) { |
| 3847 | if (FreeSSERegs >= NumElts) { |
| 3848 | FreeSSERegs -= NumElts; |
| 3849 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3850 | return ABIArgInfo::getDirect(); |
| 3851 | return ABIArgInfo::getExpand(); |
| 3852 | } |
| 3853 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3854 | } else if (IsVectorCall) { |
| 3855 | if (FreeSSERegs >= NumElts && |
| 3856 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3857 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3858 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3859 | } else if (IsReturnType) { |
| 3860 | return ABIArgInfo::getExpand(); |
| 3861 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3862 | // HVAs are delayed and reclassified in the 2nd step. |
| 3863 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3864 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3865 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3868 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3869 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3870 | // directly. |
| 3871 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3872 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3873 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3876 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3877 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3878 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3879 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3880 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3881 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3882 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3883 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3886 | // Bool type is always extended to the ABI, other builtin types are not |
| 3887 | // extended. |
| 3888 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3889 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3890 | return ABIArgInfo::getExtend(); |
| 3891 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3892 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3893 | // passes them indirectly through memory. |
| 3894 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3895 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3896 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3897 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3898 | } |
| 3899 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3900 | return ABIArgInfo::getDirect(); |
| 3901 | } |
| 3902 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3903 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 3904 | unsigned FreeSSERegs, |
| 3905 | bool IsVectorCall, |
| 3906 | bool IsRegCall) const { |
| 3907 | unsigned Count = 0; |
| 3908 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 3909 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 3910 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3911 | if (Count < VectorcallMaxParamNumAsReg) |
| 3912 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 3913 | else { |
| 3914 | // Since these cannot be passed in registers, pretend no registers |
| 3915 | // are left. |
| 3916 | unsigned ZeroSSERegsAvail = 0; |
| 3917 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 3918 | IsVectorCall, IsRegCall); |
| 3919 | } |
| 3920 | ++Count; |
| 3921 | } |
| 3922 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3923 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 3924 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3925 | } |
| 3926 | } |
| 3927 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3928 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3929 | bool IsVectorCall = |
| 3930 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3931 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3932 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3933 | unsigned FreeSSERegs = 0; |
| 3934 | if (IsVectorCall) { |
| 3935 | // We can use up to 4 SSE return registers with vectorcall. |
| 3936 | FreeSSERegs = 4; |
| 3937 | } else if (IsRegCall) { |
| 3938 | // RegCall gives us 16 SSE registers. |
| 3939 | FreeSSERegs = 16; |
| 3940 | } |
| 3941 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3942 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3943 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 3944 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3945 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3946 | if (IsVectorCall) { |
| 3947 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3948 | FreeSSERegs = 6; |
| 3949 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3950 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3951 | FreeSSERegs = 16; |
| 3952 | } |
| 3953 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3954 | if (IsVectorCall) { |
| 3955 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 3956 | } else { |
| 3957 | for (auto &I : FI.arguments()) |
| 3958 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 3959 | } |
| 3960 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3963 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3964 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 3965 | |
| 3966 | bool IsIndirect = false; |
| 3967 | |
| 3968 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3969 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 3970 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 3971 | uint64_t Width = getContext().getTypeSize(Ty); |
| 3972 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 3973 | } |
| 3974 | |
| 3975 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3976 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3977 | CharUnits::fromQuantity(8), |
| 3978 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3979 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3980 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3981 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3982 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3983 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3984 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3985 | bool IsSoftFloatABI; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3986 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3987 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 3988 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3989 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3990 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3991 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3992 | }; |
| 3993 | |
| 3994 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3995 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3996 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 3997 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3998 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3999 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4000 | // This is recovered from gcc output. |
| 4001 | return 1; // r1 is the dedicated stack pointer |
| 4002 | } |
| 4003 | |
| 4004 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4005 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4006 | }; |
| 4007 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4008 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4009 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4010 | // TODO: this implementation is now likely redundant with |
| 4011 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4012 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4013 | QualType Ty) const { |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4014 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4015 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4016 | // TODO: Implement this. For now ignore. |
| 4017 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4018 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4021 | // struct __va_list_tag { |
| 4022 | // unsigned char gpr; |
| 4023 | // unsigned char fpr; |
| 4024 | // unsigned short reserved; |
| 4025 | // void *overflow_arg_area; |
| 4026 | // void *reg_save_area; |
| 4027 | // }; |
| 4028 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4029 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4030 | bool isInt = |
| 4031 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4032 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4033 | |
| 4034 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4035 | // with the argument-lowering code. |
| 4036 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4037 | |
| 4038 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4039 | |
| 4040 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4041 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4042 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4043 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 4044 | } else { |
| 4045 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4046 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4047 | |
| 4048 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4049 | |
| 4050 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4051 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4052 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4053 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4054 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4055 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4056 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4057 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4058 | |
| 4059 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4060 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4061 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4062 | |
| 4063 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4064 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4065 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4066 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4067 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4068 | // Case 1: consume registers. |
| 4069 | Address RegAddr = Address::invalid(); |
| 4070 | { |
| 4071 | CGF.EmitBlock(UsingRegs); |
| 4072 | |
| 4073 | Address RegSaveAreaPtr = |
| 4074 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 4075 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4076 | CharUnits::fromQuantity(8)); |
| 4077 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4078 | |
| 4079 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4080 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4081 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4082 | CharUnits::fromQuantity(32)); |
| 4083 | } |
| 4084 | |
| 4085 | // Get the address of the saved value by scaling the number of |
| 4086 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4087 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4088 | llvm::Value *RegOffset = |
| 4089 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4090 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4091 | RegAddr.getPointer(), RegOffset), |
| 4092 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4093 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4094 | |
| 4095 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4096 | NumRegs = |
| 4097 | Builder.CreateAdd(NumRegs, |
| 4098 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4099 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4100 | |
| 4101 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4102 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4103 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4104 | // Case 2: consume space in the overflow area. |
| 4105 | Address MemAddr = Address::invalid(); |
| 4106 | { |
| 4107 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4108 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4109 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4110 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4111 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4112 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4113 | |
| 4114 | CharUnits Size; |
| 4115 | if (!isIndirect) { |
| 4116 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4117 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4118 | } else { |
| 4119 | Size = CGF.getPointerSize(); |
| 4120 | } |
| 4121 | |
| 4122 | Address OverflowAreaAddr = |
| 4123 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4124 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4125 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4126 | // Round up address of argument to alignment |
| 4127 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4128 | if (Align > OverflowAreaAlign) { |
| 4129 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4130 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4131 | Align); |
| 4132 | } |
| 4133 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4134 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4135 | |
| 4136 | // Increase the overflow area. |
| 4137 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4138 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4139 | CGF.EmitBranch(Cont); |
| 4140 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4141 | |
| 4142 | CGF.EmitBlock(Cont); |
| 4143 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4144 | // Merge the cases with a phi. |
| 4145 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4146 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4147 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4148 | // Load the pointer if the argument was passed indirectly. |
| 4149 | if (isIndirect) { |
| 4150 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4151 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | return Result; |
| 4155 | } |
| 4156 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4157 | bool |
| 4158 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4159 | llvm::Value *Address) const { |
| 4160 | // This is calculated from the LLVM and GCC tables and verified |
| 4161 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4162 | |
| 4163 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4164 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4165 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4166 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4167 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4168 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4169 | |
| 4170 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4171 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4172 | |
| 4173 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4174 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4175 | |
| 4176 | // 64-76 are various 4-byte special-purpose registers: |
| 4177 | // 64: mq |
| 4178 | // 65: lr |
| 4179 | // 66: ctr |
| 4180 | // 67: ap |
| 4181 | // 68-75 cr0-7 |
| 4182 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4183 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4184 | |
| 4185 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4186 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4187 | |
| 4188 | // 109: vrsave |
| 4189 | // 110: vscr |
| 4190 | // 111: spe_acc |
| 4191 | // 112: spefscr |
| 4192 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4193 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4194 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4195 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4198 | // PowerPC-64 |
| 4199 | |
| 4200 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4201 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4202 | class PPC64_SVR4_ABIInfo : public ABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4203 | public: |
| 4204 | enum ABIKind { |
| 4205 | ELFv1 = 0, |
| 4206 | ELFv2 |
| 4207 | }; |
| 4208 | |
| 4209 | private: |
| 4210 | static const unsigned GPRBits = 64; |
| 4211 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4212 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4213 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4214 | |
| 4215 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4216 | // will be passed in a QPX register. |
| 4217 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4218 | if (!HasQPX) |
| 4219 | return false; |
| 4220 | |
| 4221 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4222 | unsigned NumElements = VT->getNumElements(); |
| 4223 | if (NumElements == 1) |
| 4224 | return false; |
| 4225 | |
| 4226 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4227 | if (getContext().getTypeSize(Ty) <= 256) |
| 4228 | return true; |
| 4229 | } else if (VT->getElementType()-> |
| 4230 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4231 | if (getContext().getTypeSize(Ty) <= 128) |
| 4232 | return true; |
| 4233 | } |
| 4234 | } |
| 4235 | |
| 4236 | return false; |
| 4237 | } |
| 4238 | |
| 4239 | bool IsQPXVectorTy(QualType Ty) const { |
| 4240 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4241 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4242 | |
| 4243 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4244 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4245 | bool SoftFloatABI) |
| 4246 | : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
| 4247 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4248 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4249 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4250 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4251 | |
| 4252 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4253 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4254 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4255 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4256 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4257 | uint64_t Members) const override; |
| 4258 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4259 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4260 | // Example: For aggregate arguments that fit in a register, we could |
| 4261 | // use getDirectInReg (as is done below for structs containing a single |
| 4262 | // floating-point value) to avoid pushing them to memory on function |
| 4263 | // entry. This would require changing the logic in PPCISelLowering |
| 4264 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4265 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4266 | if (!getCXXABI().classifyReturnType(FI)) |
| 4267 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4268 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4269 | // We rely on the default argument classification for the most part. |
| 4270 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4271 | // 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] | 4272 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4273 | if (T) { |
| 4274 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4275 | if (IsQPXVectorTy(T) || |
| 4276 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4277 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4278 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4279 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4280 | continue; |
| 4281 | } |
| 4282 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4283 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4284 | } |
| 4285 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4286 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4287 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4288 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4289 | }; |
| 4290 | |
| 4291 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4292 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4293 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4294 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4295 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4296 | bool SoftFloatABI) |
| 4297 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4298 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4299 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4300 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4301 | // This is recovered from gcc output. |
| 4302 | return 1; // r1 is the dedicated stack pointer |
| 4303 | } |
| 4304 | |
| 4305 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4306 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4307 | }; |
| 4308 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4309 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4310 | public: |
| 4311 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4312 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4313 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4314 | // This is recovered from gcc output. |
| 4315 | return 1; // r1 is the dedicated stack pointer |
| 4316 | } |
| 4317 | |
| 4318 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4319 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4320 | }; |
| 4321 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4322 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4323 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4324 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4325 | // extended to 64 bits. |
| 4326 | bool |
| 4327 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4328 | // Treat an enum type as its underlying type. |
| 4329 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4330 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4331 | |
| 4332 | // Promotable integer types are required to be promoted by the ABI. |
| 4333 | if (Ty->isPromotableIntegerType()) |
| 4334 | return true; |
| 4335 | |
| 4336 | // In addition to the usual promotable integer types, we also need to |
| 4337 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4338 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4339 | switch (BT->getKind()) { |
| 4340 | case BuiltinType::Int: |
| 4341 | case BuiltinType::UInt: |
| 4342 | return true; |
| 4343 | default: |
| 4344 | break; |
| 4345 | } |
| 4346 | |
| 4347 | return false; |
| 4348 | } |
| 4349 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4350 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4351 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4352 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4353 | // Complex types are passed just like their elements. |
| 4354 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4355 | Ty = CTy->getElementType(); |
| 4356 | |
| 4357 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4358 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4359 | if (IsQPXVectorTy(Ty)) { |
| 4360 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4361 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4362 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4363 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4364 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4365 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4366 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4367 | |
| 4368 | // For single-element float/vector structs, we consider the whole type |
| 4369 | // to have the same alignment requirements as its single element. |
| 4370 | const Type *AlignAsType = nullptr; |
| 4371 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4372 | if (EltType) { |
| 4373 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4374 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4375 | getContext().getTypeSize(EltType) == 128) || |
| 4376 | (BT && BT->isFloatingPoint())) |
| 4377 | AlignAsType = EltType; |
| 4378 | } |
| 4379 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4380 | // Likewise for ELFv2 homogeneous aggregates. |
| 4381 | const Type *Base = nullptr; |
| 4382 | uint64_t Members = 0; |
| 4383 | if (!AlignAsType && Kind == ELFv2 && |
| 4384 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4385 | AlignAsType = Base; |
| 4386 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4387 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4388 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4389 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4390 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4391 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4392 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4393 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4394 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4395 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4396 | |
| 4397 | // Otherwise, we only need alignment for any aggregate type that |
| 4398 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4399 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4400 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4401 | return CharUnits::fromQuantity(32); |
| 4402 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4403 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4404 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4405 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4406 | } |
| 4407 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4408 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4409 | /// aggregate. Base is set to the base element type, and Members is set |
| 4410 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4411 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4412 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4413 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4414 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4415 | if (NElements == 0) |
| 4416 | return false; |
| 4417 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4418 | return false; |
| 4419 | Members *= NElements; |
| 4420 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4421 | const RecordDecl *RD = RT->getDecl(); |
| 4422 | if (RD->hasFlexibleArrayMember()) |
| 4423 | return false; |
| 4424 | |
| 4425 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4426 | |
| 4427 | // If this is a C++ record, check the bases first. |
| 4428 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4429 | for (const auto &I : CXXRD->bases()) { |
| 4430 | // Ignore empty records. |
| 4431 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4432 | continue; |
| 4433 | |
| 4434 | uint64_t FldMembers; |
| 4435 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4436 | return false; |
| 4437 | |
| 4438 | Members += FldMembers; |
| 4439 | } |
| 4440 | } |
| 4441 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4442 | for (const auto *FD : RD->fields()) { |
| 4443 | // Ignore (non-zero arrays of) empty records. |
| 4444 | QualType FT = FD->getType(); |
| 4445 | while (const ConstantArrayType *AT = |
| 4446 | getContext().getAsConstantArrayType(FT)) { |
| 4447 | if (AT->getSize().getZExtValue() == 0) |
| 4448 | return false; |
| 4449 | FT = AT->getElementType(); |
| 4450 | } |
| 4451 | if (isEmptyRecord(getContext(), FT, true)) |
| 4452 | continue; |
| 4453 | |
| 4454 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4455 | if (getContext().getLangOpts().CPlusPlus && |
| 4456 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4457 | continue; |
| 4458 | |
| 4459 | uint64_t FldMembers; |
| 4460 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4461 | return false; |
| 4462 | |
| 4463 | Members = (RD->isUnion() ? |
| 4464 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4465 | } |
| 4466 | |
| 4467 | if (!Base) |
| 4468 | return false; |
| 4469 | |
| 4470 | // Ensure there is no padding. |
| 4471 | if (getContext().getTypeSize(Base) * Members != |
| 4472 | getContext().getTypeSize(Ty)) |
| 4473 | return false; |
| 4474 | } else { |
| 4475 | Members = 1; |
| 4476 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4477 | Members = 2; |
| 4478 | Ty = CT->getElementType(); |
| 4479 | } |
| 4480 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4481 | // Most ABIs only support float, double, and some vector type widths. |
| 4482 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4483 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4484 | |
| 4485 | // The base type must be the same for all members. Types that |
| 4486 | // agree in both total size and mode (float vs. vector) are |
| 4487 | // treated as being equivalent here. |
| 4488 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4489 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4490 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4491 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4492 | // so make sure to widen it explicitly. |
| 4493 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4494 | QualType EltTy = VT->getElementType(); |
| 4495 | unsigned NumElements = |
| 4496 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4497 | Base = getContext() |
| 4498 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4499 | .getTypePtr(); |
| 4500 | } |
| 4501 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4502 | |
| 4503 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4504 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4505 | return false; |
| 4506 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4507 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4508 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4509 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4510 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4511 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4512 | // double, long double, or 128-bit vectors. |
| 4513 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4514 | if (BT->getKind() == BuiltinType::Float || |
| 4515 | BT->getKind() == BuiltinType::Double || |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4516 | BT->getKind() == BuiltinType::LongDouble) { |
| 4517 | if (IsSoftFloatABI) |
| 4518 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4519 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4520 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4521 | } |
| 4522 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4523 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4524 | return true; |
| 4525 | } |
| 4526 | return false; |
| 4527 | } |
| 4528 | |
| 4529 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4530 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4531 | // Vector types require one register, floating point types require one |
| 4532 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4533 | uint32_t NumRegs = |
| 4534 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4535 | |
| 4536 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4537 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4540 | ABIArgInfo |
| 4541 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4542 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4543 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4544 | if (Ty->isAnyComplexType()) |
| 4545 | return ABIArgInfo::getDirect(); |
| 4546 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4547 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4548 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4549 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4550 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4551 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4552 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4553 | else if (Size < 128) { |
| 4554 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4555 | return ABIArgInfo::getDirect(CoerceTy); |
| 4556 | } |
| 4557 | } |
| 4558 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4559 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4560 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4561 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4562 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4563 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4564 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4565 | |
| 4566 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4567 | const Type *Base = nullptr; |
| 4568 | uint64_t Members = 0; |
| 4569 | if (Kind == ELFv2 && |
| 4570 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4571 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4572 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4573 | return ABIArgInfo::getDirect(CoerceTy); |
| 4574 | } |
| 4575 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4576 | // If an aggregate may end up fully in registers, we do not |
| 4577 | // use the ByVal method, but pass the aggregate as array. |
| 4578 | // This is usually beneficial since we avoid forcing the |
| 4579 | // back-end to store the argument to memory. |
| 4580 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4581 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4582 | llvm::Type *CoerceTy; |
| 4583 | |
| 4584 | // Types up to 8 bytes are passed as integer type (which will be |
| 4585 | // properly aligned in the argument save area doubleword). |
| 4586 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4587 | CoerceTy = |
| 4588 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4589 | // Larger types are passed as arrays, with the base type selected |
| 4590 | // according to the required alignment in the save area. |
| 4591 | else { |
| 4592 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4593 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4594 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4595 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4596 | } |
| 4597 | |
| 4598 | return ABIArgInfo::getDirect(CoerceTy); |
| 4599 | } |
| 4600 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4601 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4602 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4603 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4604 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | return (isPromotableTypeForABI(Ty) ? |
| 4608 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4609 | } |
| 4610 | |
| 4611 | ABIArgInfo |
| 4612 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4613 | if (RetTy->isVoidType()) |
| 4614 | return ABIArgInfo::getIgnore(); |
| 4615 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4616 | if (RetTy->isAnyComplexType()) |
| 4617 | return ABIArgInfo::getDirect(); |
| 4618 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4619 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4620 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4621 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4622 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4623 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4624 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4625 | else if (Size < 128) { |
| 4626 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4627 | return ABIArgInfo::getDirect(CoerceTy); |
| 4628 | } |
| 4629 | } |
| 4630 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4631 | if (isAggregateTypeForABI(RetTy)) { |
| 4632 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4633 | const Type *Base = nullptr; |
| 4634 | uint64_t Members = 0; |
| 4635 | if (Kind == ELFv2 && |
| 4636 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4637 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4638 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4639 | return ABIArgInfo::getDirect(CoerceTy); |
| 4640 | } |
| 4641 | |
| 4642 | // ELFv2 small aggregates are returned in up to two registers. |
| 4643 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4644 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4645 | if (Bits == 0) |
| 4646 | return ABIArgInfo::getIgnore(); |
| 4647 | |
| 4648 | llvm::Type *CoerceTy; |
| 4649 | if (Bits > GPRBits) { |
| 4650 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4651 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4652 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4653 | CoerceTy = |
| 4654 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4655 | return ABIArgInfo::getDirect(CoerceTy); |
| 4656 | } |
| 4657 | |
| 4658 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4659 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4660 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4661 | |
| 4662 | return (isPromotableTypeForABI(RetTy) ? |
| 4663 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4664 | } |
| 4665 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4666 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4667 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4668 | QualType Ty) const { |
| 4669 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4670 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4671 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4672 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4673 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4674 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4675 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4676 | // in separate doublewords. However, Clang expects us to produce a |
| 4677 | // pointer to a structure with the two parts packed tightly. So generate |
| 4678 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4679 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4680 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4681 | CharUnits EltSize = TypeInfo.first / 2; |
| 4682 | if (EltSize < SlotSize) { |
| 4683 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4684 | SlotSize * 2, SlotSize, |
| 4685 | SlotSize, /*AllowHigher*/ true); |
| 4686 | |
| 4687 | Address RealAddr = Addr; |
| 4688 | Address ImagAddr = RealAddr; |
| 4689 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4690 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4691 | SlotSize - EltSize); |
| 4692 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4693 | 2 * SlotSize - EltSize); |
| 4694 | } else { |
| 4695 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4696 | } |
| 4697 | |
| 4698 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4699 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4700 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4701 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4702 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4703 | |
| 4704 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4705 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4706 | /*init*/ true); |
| 4707 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4708 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4711 | // Otherwise, just use the general rule. |
| 4712 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4713 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
| 4716 | static bool |
| 4717 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4718 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4719 | // This is calculated from the LLVM and GCC tables and verified |
| 4720 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4721 | |
| 4722 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4723 | |
| 4724 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4725 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4726 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4727 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4728 | |
| 4729 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4730 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4731 | |
| 4732 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4733 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4734 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4735 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4736 | // 64: mq |
| 4737 | // 65: lr |
| 4738 | // 66: ctr |
| 4739 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4740 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4741 | |
| 4742 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4743 | // 68-75 cr0-7 |
| 4744 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4745 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4746 | |
| 4747 | // 77-108: v0-31, the 16-byte vector registers |
| 4748 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4749 | |
| 4750 | // 109: vrsave |
| 4751 | // 110: vscr |
| 4752 | // 111: spe_acc |
| 4753 | // 112: spefscr |
| 4754 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4755 | // 114: tfhar |
| 4756 | // 115: tfiar |
| 4757 | // 116: texasr |
| 4758 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4759 | |
| 4760 | return false; |
| 4761 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4762 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4763 | bool |
| 4764 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4765 | CodeGen::CodeGenFunction &CGF, |
| 4766 | llvm::Value *Address) const { |
| 4767 | |
| 4768 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4769 | } |
| 4770 | |
| 4771 | bool |
| 4772 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4773 | llvm::Value *Address) const { |
| 4774 | |
| 4775 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4776 | } |
| 4777 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4778 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4779 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4780 | //===----------------------------------------------------------------------===// |
| 4781 | |
| 4782 | namespace { |
| 4783 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4784 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4785 | public: |
| 4786 | enum ABIKind { |
| 4787 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4788 | DarwinPCS, |
| 4789 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4790 | }; |
| 4791 | |
| 4792 | private: |
| 4793 | ABIKind Kind; |
| 4794 | |
| 4795 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4796 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4797 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4798 | |
| 4799 | private: |
| 4800 | ABIKind getABIKind() const { return Kind; } |
| 4801 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4802 | |
| 4803 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4804 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4805 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4806 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4807 | uint64_t Members) const override; |
| 4808 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4809 | bool isIllegalVectorType(QualType Ty) const; |
| 4810 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4811 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4812 | if (!getCXXABI().classifyReturnType(FI)) |
| 4813 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4814 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4815 | for (auto &it : FI.arguments()) |
| 4816 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4819 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4820 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4821 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4822 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4823 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4824 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4825 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4826 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4827 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 4828 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4829 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4830 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4831 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4832 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4833 | QualType Ty) const override; |
| 4834 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4835 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 4836 | ArrayRef<llvm::Type*> scalars, |
| 4837 | bool asReturnValue) const override { |
| 4838 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4839 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 4840 | bool isSwiftErrorInRegister() const override { |
| 4841 | return true; |
| 4842 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 4843 | |
| 4844 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 4845 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4846 | }; |
| 4847 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4848 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4849 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4850 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4851 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4852 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4853 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Saleem Abdulrasool | 40db477 | 2017-02-11 23:03:13 +0000 | [diff] [blame] | 4854 | return "mov\tfp, fp\t\t# marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4855 | } |
| 4856 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4857 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4858 | return 31; |
| 4859 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4860 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4861 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4862 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 4863 | |
| 4864 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 4865 | public: |
| 4866 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 4867 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 4868 | |
| 4869 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 4870 | llvm::SmallString<24> &Opt) const override { |
| 4871 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 4872 | } |
| 4873 | |
| 4874 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 4875 | llvm::SmallString<32> &Opt) const override { |
| 4876 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 4877 | } |
| 4878 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4879 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4880 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4881 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4882 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4883 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4884 | // Handle illegal vector types here. |
| 4885 | if (isIllegalVectorType(Ty)) { |
| 4886 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4887 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 4888 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4889 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 4890 | return ABIArgInfo::getDirect(ResType); |
| 4891 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4892 | if (Size <= 32) { |
| 4893 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4894 | return ABIArgInfo::getDirect(ResType); |
| 4895 | } |
| 4896 | if (Size == 64) { |
| 4897 | llvm::Type *ResType = |
| 4898 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4899 | return ABIArgInfo::getDirect(ResType); |
| 4900 | } |
| 4901 | if (Size == 128) { |
| 4902 | llvm::Type *ResType = |
| 4903 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4904 | return ABIArgInfo::getDirect(ResType); |
| 4905 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4906 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4907 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4908 | |
| 4909 | if (!isAggregateTypeForABI(Ty)) { |
| 4910 | // Treat an enum type as its underlying type. |
| 4911 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4912 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4913 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4914 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4915 | ? ABIArgInfo::getExtend() |
| 4916 | : ABIArgInfo::getDirect()); |
| 4917 | } |
| 4918 | |
| 4919 | // Structures with either a non-trivial destructor or a non-trivial |
| 4920 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4921 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4922 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4923 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4927 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4928 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4929 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 4930 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4931 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4932 | return ABIArgInfo::getIgnore(); |
| 4933 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4934 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 4935 | // 0. |
| 4936 | if (IsEmpty && Size == 0) |
| 4937 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4938 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4939 | } |
| 4940 | |
| 4941 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4942 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4943 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4944 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4945 | return ABIArgInfo::getDirect( |
| 4946 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
| 4949 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4950 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 4951 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 4952 | // same size and alignment. |
| 4953 | if (getTarget().isRenderScriptTarget()) { |
| 4954 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 4955 | } |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4956 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 4957 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4958 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4959 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4960 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4961 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4962 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4963 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4964 | } |
| 4965 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4966 | } |
| 4967 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4968 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4969 | } |
| 4970 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4971 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4972 | if (RetTy->isVoidType()) |
| 4973 | return ABIArgInfo::getIgnore(); |
| 4974 | |
| 4975 | // Large vector types should be returned via memory. |
| 4976 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4977 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4978 | |
| 4979 | if (!isAggregateTypeForABI(RetTy)) { |
| 4980 | // Treat an enum type as its underlying type. |
| 4981 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4982 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4983 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4984 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4985 | ? ABIArgInfo::getExtend() |
| 4986 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4987 | } |
| 4988 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4989 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4990 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4991 | return ABIArgInfo::getIgnore(); |
| 4992 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4993 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4994 | uint64_t Members = 0; |
| 4995 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4996 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4997 | return ABIArgInfo::getDirect(); |
| 4998 | |
| 4999 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5000 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5001 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5002 | // same size and alignment. |
| 5003 | if (getTarget().isRenderScriptTarget()) { |
| 5004 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5005 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5006 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5007 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5008 | |
| 5009 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5010 | // For aggregates with 16-byte alignment, we use i128. |
| 5011 | if (Alignment < 128 && Size == 128) { |
| 5012 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5013 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5014 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5015 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5016 | } |
| 5017 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5018 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5021 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5022 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5023 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5024 | // Check whether VT is legal. |
| 5025 | unsigned NumElements = VT->getNumElements(); |
| 5026 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5027 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5028 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5029 | return true; |
| 5030 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5031 | } |
| 5032 | return false; |
| 5033 | } |
| 5034 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5035 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5036 | llvm::Type *eltTy, |
| 5037 | unsigned elts) const { |
| 5038 | if (!llvm::isPowerOf2_32(elts)) |
| 5039 | return false; |
| 5040 | if (totalSize.getQuantity() != 8 && |
| 5041 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5042 | return false; |
| 5043 | return true; |
| 5044 | } |
| 5045 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5046 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5047 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5048 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5049 | // but with the difference that any floating-point type is allowed, |
| 5050 | // including __fp16. |
| 5051 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5052 | if (BT->isFloatingPoint()) |
| 5053 | return true; |
| 5054 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5055 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5056 | if (VecSize == 64 || VecSize == 128) |
| 5057 | return true; |
| 5058 | } |
| 5059 | return false; |
| 5060 | } |
| 5061 | |
| 5062 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5063 | uint64_t Members) const { |
| 5064 | return Members <= 4; |
| 5065 | } |
| 5066 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5067 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5068 | QualType Ty, |
| 5069 | CodeGenFunction &CGF) const { |
| 5070 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5071 | bool IsIndirect = AI.isIndirect(); |
| 5072 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5073 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5074 | if (IsIndirect) |
| 5075 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5076 | else if (AI.getCoerceToType()) |
| 5077 | BaseTy = AI.getCoerceToType(); |
| 5078 | |
| 5079 | unsigned NumRegs = 1; |
| 5080 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5081 | BaseTy = ArrTy->getElementType(); |
| 5082 | NumRegs = ArrTy->getNumElements(); |
| 5083 | } |
| 5084 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5085 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5086 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5087 | // Standard, section B.4: |
| 5088 | // |
| 5089 | // struct { |
| 5090 | // void *__stack; |
| 5091 | // void *__gr_top; |
| 5092 | // void *__vr_top; |
| 5093 | // int __gr_offs; |
| 5094 | // int __vr_offs; |
| 5095 | // }; |
| 5096 | |
| 5097 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5098 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5099 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5100 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5101 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5102 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5103 | CharUnits TyAlign = TyInfo.second; |
| 5104 | |
| 5105 | Address reg_offs_p = Address::invalid(); |
| 5106 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5107 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5108 | CharUnits reg_top_offset; |
| 5109 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5110 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5111 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5112 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5113 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5114 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5115 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5116 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5117 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5118 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5119 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5120 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5121 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5122 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 5123 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5124 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5125 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5126 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5127 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | //======================================= |
| 5131 | // Find out where argument was passed |
| 5132 | //======================================= |
| 5133 | |
| 5134 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5135 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5136 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5137 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5138 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5139 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5140 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5141 | |
| 5142 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5143 | |
| 5144 | // 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] | 5145 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5146 | CGF.EmitBlock(MaybeRegBlock); |
| 5147 | |
| 5148 | // Integer arguments may need to correct register alignment (for example a |
| 5149 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5150 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5151 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5152 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5153 | |
| 5154 | reg_offs = CGF.Builder.CreateAdd( |
| 5155 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5156 | "align_regoffs"); |
| 5157 | reg_offs = CGF.Builder.CreateAnd( |
| 5158 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5159 | "aligned_regoffs"); |
| 5160 | } |
| 5161 | |
| 5162 | // 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] | 5163 | // The fact that this is done unconditionally reflects the fact that |
| 5164 | // allocating an argument to the stack also uses up all the remaining |
| 5165 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5166 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5167 | NewOffset = CGF.Builder.CreateAdd( |
| 5168 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5169 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5170 | |
| 5171 | // Now we're in a position to decide whether this argument really was in |
| 5172 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5173 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5174 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5175 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5176 | |
| 5177 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5178 | |
| 5179 | //======================================= |
| 5180 | // Argument was in registers |
| 5181 | //======================================= |
| 5182 | |
| 5183 | // Now we emit the code for if the argument was originally passed in |
| 5184 | // registers. First start the appropriate block: |
| 5185 | CGF.EmitBlock(InRegBlock); |
| 5186 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5187 | llvm::Value *reg_top = nullptr; |
| 5188 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 5189 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5190 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5191 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5192 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5193 | Address RegAddr = Address::invalid(); |
| 5194 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5195 | |
| 5196 | if (IsIndirect) { |
| 5197 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5198 | // stored registers or on the stack will actually be a struct **. |
| 5199 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5200 | } |
| 5201 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5202 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5203 | uint64_t NumMembers = 0; |
| 5204 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5205 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5206 | // Homogeneous aggregates passed in registers will have their elements split |
| 5207 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5208 | // qN+1, ...). We reload and store into a temporary local variable |
| 5209 | // contiguously. |
| 5210 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5211 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5212 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5213 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5214 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5215 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5216 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5217 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5218 | int Offset = 0; |
| 5219 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5220 | BaseTyInfo.first.getQuantity() < 16) |
| 5221 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5222 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5223 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5224 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5225 | Address LoadAddr = |
| 5226 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5227 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5228 | |
| 5229 | Address StoreAddr = |
| 5230 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5231 | |
| 5232 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5233 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5234 | } |
| 5235 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5236 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5237 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5238 | // Otherwise the object is contiguous in memory. |
| 5239 | |
| 5240 | // It might be right-aligned in its slot. |
| 5241 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5242 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5243 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5244 | TyInfo.first < SlotSize) { |
| 5245 | CharUnits Offset = SlotSize - TyInfo.first; |
| 5246 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5247 | } |
| 5248 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5249 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5250 | } |
| 5251 | |
| 5252 | CGF.EmitBranch(ContBlock); |
| 5253 | |
| 5254 | //======================================= |
| 5255 | // Argument was on the stack |
| 5256 | //======================================= |
| 5257 | CGF.EmitBlock(OnStackBlock); |
| 5258 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5259 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 5260 | CharUnits::Zero(), "stack_p"); |
| 5261 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5262 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5263 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5264 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5265 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5266 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5267 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5268 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5269 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5270 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5271 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5272 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5273 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5274 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5275 | "align_stack"); |
| 5276 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5277 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5278 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5279 | Address OnStackAddr(OnStackPtr, |
| 5280 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5281 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5282 | // All stack slots are multiples of 8 bytes. |
| 5283 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5284 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5285 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5286 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5287 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5288 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5289 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5290 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5291 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5292 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5293 | |
| 5294 | // Write the new value of __stack for the next call to va_arg |
| 5295 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5296 | |
| 5297 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5298 | TyInfo.first < StackSlotSize) { |
| 5299 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 5300 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5301 | } |
| 5302 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5303 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5304 | |
| 5305 | CGF.EmitBranch(ContBlock); |
| 5306 | |
| 5307 | //======================================= |
| 5308 | // Tidy up |
| 5309 | //======================================= |
| 5310 | CGF.EmitBlock(ContBlock); |
| 5311 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5312 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5313 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5314 | |
| 5315 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5316 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 5317 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5318 | |
| 5319 | return ResAddr; |
| 5320 | } |
| 5321 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5322 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5323 | CodeGenFunction &CGF) const { |
| 5324 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5325 | // illegal vector types. Lower VAArg here for these cases and use |
| 5326 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5327 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5328 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5329 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5330 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5331 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5332 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5333 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5334 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5335 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5336 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5339 | // The size of the actual thing passed, which might end up just |
| 5340 | // being a pointer for indirect types. |
| 5341 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5342 | |
| 5343 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5344 | // aggregates should be passed indirectly. |
| 5345 | bool IsIndirect = false; |
| 5346 | if (TyInfo.first.getQuantity() > 16) { |
| 5347 | const Type *Base = nullptr; |
| 5348 | uint64_t Members = 0; |
| 5349 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5352 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5353 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5354 | } |
| 5355 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5356 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5357 | QualType Ty) const { |
| 5358 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5359 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5360 | CharUnits::fromQuantity(8), |
| 5361 | /*allowHigherAlign*/ false); |
| 5362 | } |
| 5363 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5364 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5365 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5366 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5367 | |
| 5368 | namespace { |
| 5369 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5370 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5371 | public: |
| 5372 | enum ABIKind { |
| 5373 | APCS = 0, |
| 5374 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5375 | AAPCS_VFP = 2, |
| 5376 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5377 | }; |
| 5378 | |
| 5379 | private: |
| 5380 | ABIKind Kind; |
| 5381 | |
| 5382 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5383 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5384 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5385 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5386 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5387 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5388 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5389 | switch (getTarget().getTriple().getEnvironment()) { |
| 5390 | case llvm::Triple::Android: |
| 5391 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5392 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5393 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5394 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5395 | case llvm::Triple::MuslEABI: |
| 5396 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5397 | return true; |
| 5398 | default: |
| 5399 | return false; |
| 5400 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5401 | } |
| 5402 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5403 | bool isEABIHF() const { |
| 5404 | switch (getTarget().getTriple().getEnvironment()) { |
| 5405 | case llvm::Triple::EABIHF: |
| 5406 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5407 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5408 | return true; |
| 5409 | default: |
| 5410 | return false; |
| 5411 | } |
| 5412 | } |
| 5413 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5414 | ABIKind getABIKind() const { return Kind; } |
| 5415 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5416 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 5417 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5418 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5419 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5420 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5421 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5422 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5423 | uint64_t Members) const override; |
| 5424 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5425 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5426 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5427 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5428 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5429 | |
| 5430 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5431 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5432 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5433 | |
| 5434 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 5435 | ArrayRef<llvm::Type*> scalars, |
| 5436 | bool asReturnValue) const override { |
| 5437 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5438 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5439 | bool isSwiftErrorInRegister() const override { |
| 5440 | return true; |
| 5441 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5442 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5443 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5444 | }; |
| 5445 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5446 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5447 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5448 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5449 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5450 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5451 | const ARMABIInfo &getABIInfo() const { |
| 5452 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5453 | } |
| 5454 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5455 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5456 | return 13; |
| 5457 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5458 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5459 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5460 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 5461 | } |
| 5462 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5463 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5464 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5465 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5466 | |
| 5467 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5468 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5469 | return false; |
| 5470 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5471 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5472 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5473 | if (getABIInfo().isEABI()) return 88; |
| 5474 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5475 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5476 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5477 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5478 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5479 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5480 | if (!FD) |
| 5481 | return; |
| 5482 | |
| 5483 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5484 | if (!Attr) |
| 5485 | return; |
| 5486 | |
| 5487 | const char *Kind; |
| 5488 | switch (Attr->getInterrupt()) { |
| 5489 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5490 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5491 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5492 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5493 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5494 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5495 | } |
| 5496 | |
| 5497 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5498 | |
| 5499 | Fn->addFnAttr("interrupt", Kind); |
| 5500 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5501 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5502 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5503 | return; |
| 5504 | |
| 5505 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5506 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5507 | // the backend to perform a realignment as part of the function prologue. |
| 5508 | llvm::AttrBuilder B; |
| 5509 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5510 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5511 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5512 | }; |
| 5513 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5514 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5515 | public: |
| 5516 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5517 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5518 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5519 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5520 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5521 | |
| 5522 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5523 | llvm::SmallString<24> &Opt) const override { |
| 5524 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5525 | } |
| 5526 | |
| 5527 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5528 | llvm::SmallString<32> &Opt) const override { |
| 5529 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5530 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5531 | }; |
| 5532 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5533 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5534 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5535 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5536 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 5537 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5538 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5539 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5540 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5541 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5542 | FI.getReturnInfo() = |
| 5543 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5544 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5545 | for (auto &I : FI.arguments()) |
| 5546 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5547 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5548 | // Always honor user-specified calling convention. |
| 5549 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5550 | return; |
| 5551 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5552 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5553 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5554 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5555 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5556 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5557 | /// Return the default calling convention that LLVM will use. |
| 5558 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5559 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5560 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5561 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5562 | else if (isEABI()) |
| 5563 | return llvm::CallingConv::ARM_AAPCS; |
| 5564 | else |
| 5565 | return llvm::CallingConv::ARM_APCS; |
| 5566 | } |
| 5567 | |
| 5568 | /// Return the calling convention that our ABI would like us to use |
| 5569 | /// as the C calling convention. |
| 5570 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5571 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5572 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5573 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5574 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5575 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5576 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5577 | llvm_unreachable("bad ABI kind"); |
| 5578 | } |
| 5579 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5580 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5581 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5582 | |
| 5583 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5584 | // they'd just match what LLVM will infer from the triple. |
| 5585 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5586 | if (abiCC != getLLVMDefaultCC()) |
| 5587 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5588 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5589 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 5590 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 5591 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
| 5592 | switch (getABIKind()) { |
| 5593 | case APCS: |
| 5594 | case AAPCS16_VFP: |
| 5595 | if (abiCC != getLLVMDefaultCC()) |
| 5596 | BuiltinCC = abiCC; |
| 5597 | break; |
| 5598 | case AAPCS: |
| 5599 | case AAPCS_VFP: |
| 5600 | BuiltinCC = llvm::CallingConv::ARM_AAPCS; |
| 5601 | break; |
| 5602 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5605 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5606 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5607 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5608 | // A single-precision floating-point type (including promoted |
| 5609 | // half-precision types); A double-precision floating-point type; |
| 5610 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5611 | // with a Base Type of a single- or double-precision floating-point type, |
| 5612 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5613 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5614 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5615 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5616 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5617 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5618 | // Handle illegal vector types here. |
| 5619 | if (isIllegalVectorType(Ty)) { |
| 5620 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5621 | if (Size <= 32) { |
| 5622 | llvm::Type *ResType = |
| 5623 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5624 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5625 | } |
| 5626 | if (Size == 64) { |
| 5627 | llvm::Type *ResType = llvm::VectorType::get( |
| 5628 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5629 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5630 | } |
| 5631 | if (Size == 128) { |
| 5632 | llvm::Type *ResType = llvm::VectorType::get( |
| 5633 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5634 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5635 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5636 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5637 | } |
| 5638 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5639 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 5640 | // unspecified. This is not done for OpenCL as it handles the half type |
| 5641 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5642 | if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5643 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5644 | llvm::Type::getFloatTy(getVMContext()) : |
| 5645 | llvm::Type::getInt32Ty(getVMContext()); |
| 5646 | return ABIArgInfo::getDirect(ResType); |
| 5647 | } |
| 5648 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5649 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5650 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5651 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5652 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5653 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5654 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5655 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5656 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5657 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5658 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5659 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5660 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5661 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5662 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5663 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5664 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5665 | return ABIArgInfo::getIgnore(); |
| 5666 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5667 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5668 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5669 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5670 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5671 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5672 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5673 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5674 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5675 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5676 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5677 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5678 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5679 | // this convention even for a variadic function: the backend will use GPRs |
| 5680 | // if needed. |
| 5681 | const Type *Base = nullptr; |
| 5682 | uint64_t Members = 0; |
| 5683 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5684 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5685 | llvm::Type *Ty = |
| 5686 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5687 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5688 | } |
| 5689 | } |
| 5690 | |
| 5691 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5692 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5693 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5694 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5695 | // and a pointer is passed. |
| 5696 | return ABIArgInfo::getIndirect( |
| 5697 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5698 | } |
| 5699 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5700 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5701 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5702 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5703 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5704 | uint64_t ABIAlign = 4; |
| 5705 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5706 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5707 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5708 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5709 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5710 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5711 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5712 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5713 | /*ByVal=*/true, |
| 5714 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5715 | } |
| 5716 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5717 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5718 | // same size and alignment. |
| 5719 | if (getTarget().isRenderScriptTarget()) { |
| 5720 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5721 | } |
| 5722 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5723 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5724 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5725 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5726 | // FIXME: Try to match the types of the arguments more accurately where |
| 5727 | // we can. |
| 5728 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5729 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5730 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5731 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5732 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5733 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5734 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5735 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5736 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5737 | } |
| 5738 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5739 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5740 | llvm::LLVMContext &VMContext) { |
| 5741 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5742 | // is called integer-like if its size is less than or equal to one word, and |
| 5743 | // the offset of each of its addressable sub-fields is zero. |
| 5744 | |
| 5745 | uint64_t Size = Context.getTypeSize(Ty); |
| 5746 | |
| 5747 | // Check that the type fits in a word. |
| 5748 | if (Size > 32) |
| 5749 | return false; |
| 5750 | |
| 5751 | // FIXME: Handle vector types! |
| 5752 | if (Ty->isVectorType()) |
| 5753 | return false; |
| 5754 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5755 | // Float types are never treated as "integer like". |
| 5756 | if (Ty->isRealFloatingType()) |
| 5757 | return false; |
| 5758 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5759 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5760 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5761 | return true; |
| 5762 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5763 | // Small complex integer types are "integer like". |
| 5764 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5765 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5766 | |
| 5767 | // Single element and zero sized arrays should be allowed, by the definition |
| 5768 | // above, but they are not. |
| 5769 | |
| 5770 | // Otherwise, it must be a record type. |
| 5771 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5772 | if (!RT) return false; |
| 5773 | |
| 5774 | // Ignore records with flexible arrays. |
| 5775 | const RecordDecl *RD = RT->getDecl(); |
| 5776 | if (RD->hasFlexibleArrayMember()) |
| 5777 | return false; |
| 5778 | |
| 5779 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5780 | // like". |
| 5781 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5782 | |
| 5783 | bool HadField = false; |
| 5784 | unsigned idx = 0; |
| 5785 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5786 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5787 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5788 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5789 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5790 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5791 | // struct { int : 0; int x } |
| 5792 | // is non-integer like according to gcc. |
| 5793 | if (FD->isBitField()) { |
| 5794 | if (!RD->isUnion()) |
| 5795 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5796 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5797 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5798 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5799 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5800 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5801 | } |
| 5802 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5803 | // Check if this field is at offset 0. |
| 5804 | if (Layout.getFieldOffset(idx) != 0) |
| 5805 | return false; |
| 5806 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5807 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5808 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5809 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5810 | // Only allow at most one field in a structure. This doesn't match the |
| 5811 | // wording above, but follows gcc in situations with a field following an |
| 5812 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5813 | if (!RD->isUnion()) { |
| 5814 | if (HadField) |
| 5815 | return false; |
| 5816 | |
| 5817 | HadField = true; |
| 5818 | } |
| 5819 | } |
| 5820 | |
| 5821 | return true; |
| 5822 | } |
| 5823 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5824 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5825 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5826 | bool IsEffectivelyAAPCS_VFP = |
| 5827 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5828 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5829 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5830 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5831 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5832 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5833 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5834 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5835 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5836 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5837 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5838 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5839 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5840 | if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5841 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5842 | llvm::Type::getFloatTy(getVMContext()) : |
| 5843 | llvm::Type::getInt32Ty(getVMContext()); |
| 5844 | return ABIArgInfo::getDirect(ResType); |
| 5845 | } |
| 5846 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5847 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5848 | // Treat an enum type as its underlying type. |
| 5849 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5850 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5851 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5852 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5853 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5854 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5855 | |
| 5856 | // Are we following APCS? |
| 5857 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5858 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5859 | return ABIArgInfo::getIgnore(); |
| 5860 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5861 | // Complex types are all returned as packed integers. |
| 5862 | // |
| 5863 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5864 | // correctly. |
| 5865 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5866 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5867 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5868 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5869 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5870 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5871 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5872 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5873 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5874 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5875 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5876 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5877 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5878 | } |
| 5879 | |
| 5880 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5881 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5882 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5883 | |
| 5884 | // Otherwise this is an AAPCS variant. |
| 5885 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5886 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5887 | return ABIArgInfo::getIgnore(); |
| 5888 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5889 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5890 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5891 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5892 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5893 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5894 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5895 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5896 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5897 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5900 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5901 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5902 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5903 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5904 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 5905 | // same size and alignment. |
| 5906 | if (getTarget().isRenderScriptTarget()) { |
| 5907 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5908 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5909 | if (getDataLayout().isBigEndian()) |
| 5910 | // 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] | 5911 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5912 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5913 | // Return in the smallest viable integer type. |
| 5914 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5915 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5916 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5917 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5918 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5919 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5920 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5921 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5922 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5923 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5924 | } |
| 5925 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5926 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5927 | } |
| 5928 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5929 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5930 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 5931 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 5932 | if (isAndroid()) { |
| 5933 | // Android shipped using Clang 3.1, which supported a slightly different |
| 5934 | // vector ABI. The primary differences were that 3-element vector types |
| 5935 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 5936 | // accepts that legacy behavior for Android only. |
| 5937 | // Check whether VT is legal. |
| 5938 | unsigned NumElements = VT->getNumElements(); |
| 5939 | // NumElements should be power of 2 or equal to 3. |
| 5940 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 5941 | return true; |
| 5942 | } else { |
| 5943 | // Check whether VT is legal. |
| 5944 | unsigned NumElements = VT->getNumElements(); |
| 5945 | uint64_t Size = getContext().getTypeSize(VT); |
| 5946 | // NumElements should be power of 2. |
| 5947 | if (!llvm::isPowerOf2_32(NumElements)) |
| 5948 | return true; |
| 5949 | // Size should be greater than 32 bits. |
| 5950 | return Size <= 32; |
| 5951 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5952 | } |
| 5953 | return false; |
| 5954 | } |
| 5955 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5956 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 5957 | llvm::Type *eltTy, |
| 5958 | unsigned numElts) const { |
| 5959 | if (!llvm::isPowerOf2_32(numElts)) |
| 5960 | return false; |
| 5961 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 5962 | if (size > 64) |
| 5963 | return false; |
| 5964 | if (vectorSize.getQuantity() != 8 && |
| 5965 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 5966 | return false; |
| 5967 | return true; |
| 5968 | } |
| 5969 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5970 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5971 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 5972 | // double, or 64-bit or 128-bit vectors. |
| 5973 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5974 | if (BT->getKind() == BuiltinType::Float || |
| 5975 | BT->getKind() == BuiltinType::Double || |
| 5976 | BT->getKind() == BuiltinType::LongDouble) |
| 5977 | return true; |
| 5978 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5979 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5980 | if (VecSize == 64 || VecSize == 128) |
| 5981 | return true; |
| 5982 | } |
| 5983 | return false; |
| 5984 | } |
| 5985 | |
| 5986 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5987 | uint64_t Members) const { |
| 5988 | return Members <= 4; |
| 5989 | } |
| 5990 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5991 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5992 | QualType Ty) const { |
| 5993 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5994 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5995 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5996 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5997 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 5998 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5999 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6000 | } |
| 6001 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6002 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 6003 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6004 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6005 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6006 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6007 | const Type *Base = nullptr; |
| 6008 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6009 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 6010 | IsIndirect = true; |
| 6011 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6012 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6013 | // allocated by the caller. |
| 6014 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 6015 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6016 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6017 | IsIndirect = true; |
| 6018 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6019 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6020 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6021 | // 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] | 6022 | // Our callers should be prepared to handle an under-aligned address. |
| 6023 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6024 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6025 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6026 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6027 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6028 | // ARMv7k allows type alignment up to 16 bytes. |
| 6029 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6030 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6031 | } else { |
| 6032 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6033 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6034 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6035 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6036 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6037 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6038 | } |
| 6039 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6040 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6041 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6042 | //===----------------------------------------------------------------------===// |
| 6043 | |
| 6044 | namespace { |
| 6045 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6046 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6047 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6048 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6049 | |
| 6050 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6051 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6052 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6053 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6054 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6055 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6056 | }; |
| 6057 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6058 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6059 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6060 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6061 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6062 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6063 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6064 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6065 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6066 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6067 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6068 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6069 | }; |
| 6070 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6071 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6072 | if (RetTy->isVoidType()) |
| 6073 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6074 | |
| 6075 | // note: this is different from default ABI |
| 6076 | if (!RetTy->isScalarType()) |
| 6077 | return ABIArgInfo::getDirect(); |
| 6078 | |
| 6079 | // Treat an enum type as its underlying type. |
| 6080 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6081 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6082 | |
| 6083 | return (RetTy->isPromotableIntegerType() ? |
| 6084 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6085 | } |
| 6086 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6087 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6088 | // Treat an enum type as its underlying type. |
| 6089 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6090 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6091 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6092 | // Return aggregates type as indirect by value |
| 6093 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6094 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6095 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6096 | return (Ty->isPromotableIntegerType() ? |
| 6097 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6098 | } |
| 6099 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6100 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6101 | if (!getCXXABI().classifyReturnType(FI)) |
| 6102 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6103 | for (auto &I : FI.arguments()) |
| 6104 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6105 | |
| 6106 | // Always honor user-specified calling convention. |
| 6107 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6108 | return; |
| 6109 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6110 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6111 | } |
| 6112 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6113 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6114 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6115 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6116 | } |
| 6117 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6118 | void NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6119 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6120 | CodeGen::CodeGenModule &M) const{ |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6121 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6122 | if (!FD) return; |
| 6123 | |
| 6124 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6125 | |
| 6126 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6127 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6128 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6129 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6130 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6131 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6132 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6133 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6134 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6135 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6136 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6137 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6138 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6139 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6140 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6141 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6142 | // __global__ functions cannot be called from the device, we do not |
| 6143 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6144 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6145 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6146 | addNVVMMetadata(F, "kernel", 1); |
| 6147 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6148 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6149 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6150 | llvm::APSInt MaxThreads(32); |
| 6151 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6152 | if (MaxThreads > 0) |
| 6153 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6154 | |
| 6155 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6156 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6157 | // we don't have to add a PTX directive. |
| 6158 | if (Attr->getMinBlocks()) { |
| 6159 | llvm::APSInt MinBlocks(32); |
| 6160 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6161 | if (MinBlocks > 0) |
| 6162 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6163 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6164 | } |
| 6165 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6166 | } |
| 6167 | } |
| 6168 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6169 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6170 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6171 | llvm::Module *M = F->getParent(); |
| 6172 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6173 | |
| 6174 | // Get "nvvm.annotations" metadata node |
| 6175 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6176 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6177 | llvm::Metadata *MDVals[] = { |
| 6178 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6179 | llvm::ConstantAsMetadata::get( |
| 6180 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6181 | // Append metadata to nvvm.annotations |
| 6182 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6183 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6184 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6185 | |
| 6186 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6187 | // SystemZ ABI Implementation |
| 6188 | //===----------------------------------------------------------------------===// |
| 6189 | |
| 6190 | namespace { |
| 6191 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6192 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6193 | bool HasVector; |
| 6194 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6195 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6196 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6197 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6198 | |
| 6199 | bool isPromotableIntegerType(QualType Ty) const; |
| 6200 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6201 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6202 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6203 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6204 | |
| 6205 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6206 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6207 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6208 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6209 | if (!getCXXABI().classifyReturnType(FI)) |
| 6210 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6211 | for (auto &I : FI.arguments()) |
| 6212 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6213 | } |
| 6214 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6215 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6216 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6217 | |
| 6218 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 6219 | ArrayRef<llvm::Type*> scalars, |
| 6220 | bool asReturnValue) const override { |
| 6221 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6222 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6223 | bool isSwiftErrorInRegister() const override { |
| 6224 | return true; |
| 6225 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6226 | }; |
| 6227 | |
| 6228 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6229 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6230 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6231 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6232 | }; |
| 6233 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6234 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6235 | |
| 6236 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6237 | // Treat an enum type as its underlying type. |
| 6238 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6239 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6240 | |
| 6241 | // Promotable integer types are required to be promoted by the ABI. |
| 6242 | if (Ty->isPromotableIntegerType()) |
| 6243 | return true; |
| 6244 | |
| 6245 | // 32-bit values must also be promoted. |
| 6246 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6247 | switch (BT->getKind()) { |
| 6248 | case BuiltinType::Int: |
| 6249 | case BuiltinType::UInt: |
| 6250 | return true; |
| 6251 | default: |
| 6252 | return false; |
| 6253 | } |
| 6254 | return false; |
| 6255 | } |
| 6256 | |
| 6257 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6258 | return (Ty->isAnyComplexType() || |
| 6259 | Ty->isVectorType() || |
| 6260 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6261 | } |
| 6262 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6263 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6264 | return (HasVector && |
| 6265 | Ty->isVectorType() && |
| 6266 | getContext().getTypeSize(Ty) <= 128); |
| 6267 | } |
| 6268 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6269 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6270 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6271 | switch (BT->getKind()) { |
| 6272 | case BuiltinType::Float: |
| 6273 | case BuiltinType::Double: |
| 6274 | return true; |
| 6275 | default: |
| 6276 | return false; |
| 6277 | } |
| 6278 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6279 | return false; |
| 6280 | } |
| 6281 | |
| 6282 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6283 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6284 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6285 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6286 | |
| 6287 | // If this is a C++ record, check the bases first. |
| 6288 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6289 | for (const auto &I : CXXRD->bases()) { |
| 6290 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6291 | |
| 6292 | // Empty bases don't affect things either way. |
| 6293 | if (isEmptyRecord(getContext(), Base, true)) |
| 6294 | continue; |
| 6295 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6296 | if (!Found.isNull()) |
| 6297 | return Ty; |
| 6298 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6299 | } |
| 6300 | |
| 6301 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6302 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6303 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6304 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6305 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6306 | if (getContext().getLangOpts().CPlusPlus && |
| 6307 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 6308 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6309 | |
| 6310 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6311 | // Nested structures still do though. |
| 6312 | if (!Found.isNull()) |
| 6313 | return Ty; |
| 6314 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6315 | } |
| 6316 | |
| 6317 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6318 | // 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] | 6319 | if (!Found.isNull()) |
| 6320 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6321 | } |
| 6322 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6323 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6324 | } |
| 6325 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6326 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6327 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6328 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6329 | // struct { |
| 6330 | // i64 __gpr; |
| 6331 | // i64 __fpr; |
| 6332 | // i8 *__overflow_arg_area; |
| 6333 | // i8 *__reg_save_area; |
| 6334 | // }; |
| 6335 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6336 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6337 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6338 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6339 | Ty = getContext().getCanonicalType(Ty); |
| 6340 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6341 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6342 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6343 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6344 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6345 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6346 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6347 | CharUnits UnpaddedSize; |
| 6348 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6349 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6350 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6351 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6352 | } else { |
| 6353 | if (AI.getCoerceToType()) |
| 6354 | ArgTy = AI.getCoerceToType(); |
| 6355 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6356 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6357 | UnpaddedSize = TyInfo.first; |
| 6358 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6359 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6360 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6361 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6362 | PaddedSize = CharUnits::fromQuantity(16); |
| 6363 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6364 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6365 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6366 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6367 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6368 | llvm::Value *PaddedSizeV = |
| 6369 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6370 | |
| 6371 | if (IsVector) { |
| 6372 | // Work out the address of a vector argument on the stack. |
| 6373 | // Vector arguments are always passed in the high bits of a |
| 6374 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6375 | Address OverflowArgAreaPtr = |
| 6376 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6377 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6378 | Address OverflowArgArea = |
| 6379 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6380 | TyInfo.second); |
| 6381 | Address MemAddr = |
| 6382 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6383 | |
| 6384 | // Update overflow_arg_area_ptr pointer |
| 6385 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6386 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6387 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6388 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6389 | |
| 6390 | return MemAddr; |
| 6391 | } |
| 6392 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6393 | assert(PaddedSize.getQuantity() == 8); |
| 6394 | |
| 6395 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6396 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6397 | if (InFPRs) { |
| 6398 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6399 | RegCountField = 1; // __fpr |
| 6400 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6401 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6402 | } else { |
| 6403 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6404 | RegCountField = 0; // __gpr |
| 6405 | RegSaveIndex = 2; // save offset for r2 |
| 6406 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6407 | } |
| 6408 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6409 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 6410 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 6411 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6412 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6413 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6414 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6415 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6416 | |
| 6417 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6418 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6419 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6420 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6421 | |
| 6422 | // Emit code to load the value if it was passed in registers. |
| 6423 | CGF.EmitBlock(InRegBlock); |
| 6424 | |
| 6425 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6426 | llvm::Value *ScaledRegCount = |
| 6427 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6428 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6429 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6430 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6431 | llvm::Value *RegOffset = |
| 6432 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6433 | Address RegSaveAreaPtr = |
| 6434 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 6435 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6436 | llvm::Value *RegSaveArea = |
| 6437 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6438 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6439 | "raw_reg_addr"), |
| 6440 | PaddedSize); |
| 6441 | Address RegAddr = |
| 6442 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6443 | |
| 6444 | // Update the register count |
| 6445 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6446 | llvm::Value *NewRegCount = |
| 6447 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6448 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6449 | CGF.EmitBranch(ContBlock); |
| 6450 | |
| 6451 | // Emit code to load the value if it was passed in memory. |
| 6452 | CGF.EmitBlock(InMemBlock); |
| 6453 | |
| 6454 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6455 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 6456 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 6457 | Address OverflowArgArea = |
| 6458 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6459 | PaddedSize); |
| 6460 | Address RawMemAddr = |
| 6461 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6462 | Address MemAddr = |
| 6463 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6464 | |
| 6465 | // Update overflow_arg_area_ptr pointer |
| 6466 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6467 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6468 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6469 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6470 | CGF.EmitBranch(ContBlock); |
| 6471 | |
| 6472 | // Return the appropriate result. |
| 6473 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6474 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6475 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6476 | |
| 6477 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6478 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6479 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6480 | |
| 6481 | return ResAddr; |
| 6482 | } |
| 6483 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6484 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6485 | if (RetTy->isVoidType()) |
| 6486 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6487 | if (isVectorArgumentType(RetTy)) |
| 6488 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6489 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6490 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6491 | return (isPromotableIntegerType(RetTy) ? |
| 6492 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6493 | } |
| 6494 | |
| 6495 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6496 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6497 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6498 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6499 | |
| 6500 | // Integers and enums are extended to full register width. |
| 6501 | if (isPromotableIntegerType(Ty)) |
| 6502 | return ABIArgInfo::getExtend(); |
| 6503 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6504 | // Handle vector types and vector-like structure types. Note that |
| 6505 | // as opposed to float-like structure types, we do not allow any |
| 6506 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6507 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6508 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6509 | if (isVectorArgumentType(SingleElementTy) && |
| 6510 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6511 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6512 | |
| 6513 | // 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] | 6514 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6515 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6516 | |
| 6517 | // Handle small structures. |
| 6518 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6519 | // Structures with flexible arrays have variable length, so really |
| 6520 | // fail the size test above. |
| 6521 | const RecordDecl *RD = RT->getDecl(); |
| 6522 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6523 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6524 | |
| 6525 | // The structure is passed as an unextended integer, a float, or a double. |
| 6526 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6527 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6528 | assert(Size == 32 || Size == 64); |
| 6529 | if (Size == 32) |
| 6530 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6531 | else |
| 6532 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6533 | } else |
| 6534 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6535 | return ABIArgInfo::getDirect(PassTy); |
| 6536 | } |
| 6537 | |
| 6538 | // Non-structure compounds are passed indirectly. |
| 6539 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6540 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6541 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6542 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6543 | } |
| 6544 | |
| 6545 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6546 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6547 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6548 | |
| 6549 | namespace { |
| 6550 | |
| 6551 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6552 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6553 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6554 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6555 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6556 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6557 | }; |
| 6558 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6559 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6560 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6561 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6562 | llvm::GlobalValue *GV, |
| 6563 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6564 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6565 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 6566 | // Handle 'interrupt' attribute: |
| 6567 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6568 | |
| 6569 | // Step 1: Set ISR calling convention. |
| 6570 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 6571 | |
| 6572 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6573 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6574 | |
| 6575 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 6576 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 6577 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 6578 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6579 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6580 | } |
| 6581 | } |
| 6582 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6583 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6584 | // MIPS ABI Implementation. This works for both little-endian and |
| 6585 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6586 | //===----------------------------------------------------------------------===// |
| 6587 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6588 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6589 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6590 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6591 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6592 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6593 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6594 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6595 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6596 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6597 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6598 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6599 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6600 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6601 | |
| 6602 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6603 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6604 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6605 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6606 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6607 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6608 | }; |
| 6609 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6610 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6611 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6612 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6613 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6614 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6615 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6616 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6617 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6618 | return 29; |
| 6619 | } |
| 6620 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6621 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6622 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6623 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6624 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6625 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6626 | if (FD->hasAttr<Mips16Attr>()) { |
| 6627 | Fn->addFnAttr("mips16"); |
| 6628 | } |
| 6629 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6630 | Fn->addFnAttr("nomips16"); |
| 6631 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6632 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6633 | if (FD->hasAttr<MicroMipsAttr>()) |
| 6634 | Fn->addFnAttr("micromips"); |
| 6635 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 6636 | Fn->addFnAttr("nomicromips"); |
| 6637 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6638 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6639 | if (!Attr) |
| 6640 | return; |
| 6641 | |
| 6642 | const char *Kind; |
| 6643 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6644 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6645 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6646 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6647 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6648 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6649 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6650 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6651 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6652 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6653 | } |
| 6654 | |
| 6655 | Fn->addFnAttr("interrupt", Kind); |
| 6656 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6657 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6658 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6659 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6660 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6661 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6662 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6663 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6664 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6665 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6666 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6667 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6668 | void MipsABIInfo::CoerceToIntArgs( |
| 6669 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6670 | llvm::IntegerType *IntTy = |
| 6671 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6672 | |
| 6673 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6674 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6675 | ArgList.push_back(IntTy); |
| 6676 | |
| 6677 | // If necessary, add one more integer type to ArgList. |
| 6678 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6679 | |
| 6680 | if (R) |
| 6681 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6682 | } |
| 6683 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6684 | // In N32/64, an aligned double precision floating point field is passed in |
| 6685 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6686 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6687 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6688 | |
| 6689 | if (IsO32) { |
| 6690 | CoerceToIntArgs(TySize, ArgList); |
| 6691 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6692 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6693 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6694 | if (Ty->isComplexType()) |
| 6695 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6696 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6697 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6698 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6699 | // Unions/vectors are passed in integer registers. |
| 6700 | if (!RT || !RT->isStructureOrClassType()) { |
| 6701 | CoerceToIntArgs(TySize, ArgList); |
| 6702 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6703 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6704 | |
| 6705 | const RecordDecl *RD = RT->getDecl(); |
| 6706 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6707 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6708 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6709 | uint64_t LastOffset = 0; |
| 6710 | unsigned idx = 0; |
| 6711 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6712 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6713 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6714 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6715 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6716 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6717 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6718 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6719 | |
| 6720 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6721 | continue; |
| 6722 | |
| 6723 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6724 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6725 | continue; |
| 6726 | |
| 6727 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6728 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6729 | ArgList.push_back(I64); |
| 6730 | |
| 6731 | // Add double type. |
| 6732 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6733 | LastOffset = Offset + 64; |
| 6734 | } |
| 6735 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6736 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6737 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6738 | |
| 6739 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6740 | } |
| 6741 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6742 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6743 | uint64_t Offset) const { |
| 6744 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6745 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6746 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6747 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6748 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6749 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6750 | ABIArgInfo |
| 6751 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6752 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6753 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6754 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6755 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6756 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6757 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6758 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6759 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6760 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6761 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6762 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6763 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6764 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6765 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6766 | return ABIArgInfo::getIgnore(); |
| 6767 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6768 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6769 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6770 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6771 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6772 | |
Petar Jovanovic | 6f4cdb8 | 2017-05-10 14:28:18 +0000 | [diff] [blame] | 6773 | // Use indirect if the aggregate cannot fit into registers for |
| 6774 | // passing arguments according to the ABI |
| 6775 | unsigned Threshold = IsO32 ? 16 : 64; |
| 6776 | |
| 6777 | if(getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(Threshold)) |
| 6778 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align), true, |
| 6779 | getContext().getTypeAlign(Ty) / 8 > Align); |
| 6780 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6781 | // If we have reached here, aggregates are passed directly by coercing to |
| 6782 | // another structure type. Padding is inserted if the offset of the |
| 6783 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6784 | ABIArgInfo ArgInfo = |
| 6785 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6786 | getPaddingType(OrigOffset, CurrOffset)); |
| 6787 | ArgInfo.setInReg(true); |
| 6788 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | // Treat an enum type as its underlying type. |
| 6792 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6793 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6794 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6795 | // All integral types are promoted to the GPR width. |
| 6796 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6797 | return ABIArgInfo::getExtend(); |
| 6798 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6799 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6800 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6801 | } |
| 6802 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6803 | llvm::Type* |
| 6804 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6805 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6806 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6807 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6808 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6809 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6810 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6811 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6812 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6813 | // N32/64 returns struct/classes in floating point registers if the |
| 6814 | // following conditions are met: |
| 6815 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6816 | // 2. The struct/class has one or two fields all of which are floating |
| 6817 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6818 | // 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] | 6819 | // |
| 6820 | // Any other composite results are returned in integer registers. |
| 6821 | // |
| 6822 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6823 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6824 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6825 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6826 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6827 | if (!BT || !BT->isFloatingPoint()) |
| 6828 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6829 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6830 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6831 | } |
| 6832 | |
| 6833 | if (b == e) |
| 6834 | return llvm::StructType::get(getVMContext(), RTList, |
| 6835 | RD->hasAttr<PackedAttr>()); |
| 6836 | |
| 6837 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6838 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6839 | } |
| 6840 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6841 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6842 | return llvm::StructType::get(getVMContext(), RTList); |
| 6843 | } |
| 6844 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6845 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6846 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6847 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6848 | if (RetTy->isVoidType()) |
| 6849 | return ABIArgInfo::getIgnore(); |
| 6850 | |
| 6851 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6852 | // However, N32/N64 ignores zero sized return values. |
| 6853 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6854 | return ABIArgInfo::getIgnore(); |
| 6855 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6856 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6857 | if (Size <= 128) { |
| 6858 | if (RetTy->isAnyComplexType()) |
| 6859 | return ABIArgInfo::getDirect(); |
| 6860 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6861 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6862 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6863 | if (!IsO32 || |
| 6864 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6865 | ABIArgInfo ArgInfo = |
| 6866 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6867 | ArgInfo.setInReg(true); |
| 6868 | return ArgInfo; |
| 6869 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6870 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6871 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6872 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6873 | } |
| 6874 | |
| 6875 | // Treat an enum type as its underlying type. |
| 6876 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6877 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6878 | |
| 6879 | return (RetTy->isPromotableIntegerType() ? |
| 6880 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6881 | } |
| 6882 | |
| 6883 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6884 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6885 | if (!getCXXABI().classifyReturnType(FI)) |
| 6886 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6887 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6888 | // 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] | 6889 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6890 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6891 | for (auto &I : FI.arguments()) |
| 6892 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6895 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6896 | QualType OrigTy) const { |
| 6897 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6898 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6899 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6900 | // 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] | 6901 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6902 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6903 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6904 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6905 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6906 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6907 | DidPromote = true; |
| 6908 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6909 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6910 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6911 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6912 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6913 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6914 | // The alignment of things in the argument area is never larger than |
| 6915 | // StackAlignInBytes. |
| 6916 | TyInfo.second = |
| 6917 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6918 | |
| 6919 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6920 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6921 | |
| 6922 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6923 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6924 | |
| 6925 | |
| 6926 | // If there was a promotion, "unpromote" into a temporary. |
| 6927 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6928 | if (DidPromote) { |
| 6929 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6930 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6931 | |
| 6932 | // Truncate down to the right width. |
| 6933 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6934 | : CGF.IntPtrTy); |
| 6935 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6936 | if (OrigTy->isPointerType()) |
| 6937 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6938 | |
| 6939 | CGF.Builder.CreateStore(V, Temp); |
| 6940 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6941 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6942 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6943 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6946 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6947 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6948 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6949 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 6950 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 6951 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6952 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6953 | return false; |
| 6954 | } |
| 6955 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6956 | bool |
| 6957 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6958 | llvm::Value *Address) const { |
| 6959 | // This information comes from gcc's implementation, which seems to |
| 6960 | // as canonical as it gets. |
| 6961 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6962 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 6963 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6964 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6965 | |
| 6966 | // 0-31 are the general purpose registers, $0 - $31. |
| 6967 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 6968 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 6969 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6970 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6971 | |
| 6972 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 6973 | // They are one bit wide and ignored here. |
| 6974 | |
| 6975 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 6976 | // (coprocessor 1 is the FP unit) |
| 6977 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 6978 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 6979 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6980 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6981 | return false; |
| 6982 | } |
| 6983 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6984 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 6985 | // AVR ABI Implementation. |
| 6986 | //===----------------------------------------------------------------------===// |
| 6987 | |
| 6988 | namespace { |
| 6989 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6990 | public: |
| 6991 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6992 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 6993 | |
| 6994 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 6995 | CodeGen::CodeGenModule &CGM) const override { |
| 6996 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 6997 | if (!FD) return; |
| 6998 | auto *Fn = cast<llvm::Function>(GV); |
| 6999 | |
| 7000 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7001 | Fn->addFnAttr("interrupt"); |
| 7002 | |
| 7003 | if (FD->getAttr<AVRSignalAttr>()) |
| 7004 | Fn->addFnAttr("signal"); |
| 7005 | } |
| 7006 | }; |
| 7007 | } |
| 7008 | |
| 7009 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7010 | // 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] | 7011 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7012 | // handling. |
| 7013 | //===----------------------------------------------------------------------===// |
| 7014 | |
| 7015 | namespace { |
| 7016 | |
| 7017 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7018 | public: |
| 7019 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7020 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7021 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7022 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7023 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7024 | }; |
| 7025 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7026 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7027 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7028 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7029 | if (!FD) return; |
| 7030 | |
| 7031 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7032 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7033 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7034 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7035 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7036 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7037 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7038 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7039 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7040 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7041 | llvm::NamedMDNode *OpenCLMetadata = |
| 7042 | M.getModule().getOrInsertNamedMetadata( |
| 7043 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7044 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7045 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7046 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7047 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7048 | Operands.push_back( |
| 7049 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7050 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7051 | Operands.push_back( |
| 7052 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7053 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7054 | Operands.push_back( |
| 7055 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7056 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7057 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7058 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7059 | // (false) for implementing the work_group_size_hint attr later. |
| 7060 | // 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] | 7061 | Operands.push_back( |
| 7062 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7063 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7064 | } |
| 7065 | } |
| 7066 | } |
| 7067 | } |
| 7068 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7069 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7070 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7071 | //===----------------------------------------------------------------------===// |
| 7072 | // Hexagon ABI Implementation |
| 7073 | //===----------------------------------------------------------------------===// |
| 7074 | |
| 7075 | namespace { |
| 7076 | |
| 7077 | class HexagonABIInfo : public ABIInfo { |
| 7078 | |
| 7079 | |
| 7080 | public: |
| 7081 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7082 | |
| 7083 | private: |
| 7084 | |
| 7085 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7086 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7087 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7088 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7089 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7090 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7091 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7092 | }; |
| 7093 | |
| 7094 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7095 | public: |
| 7096 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7097 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7098 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7099 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7100 | return 29; |
| 7101 | } |
| 7102 | }; |
| 7103 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7104 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7105 | |
| 7106 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7107 | if (!getCXXABI().classifyReturnType(FI)) |
| 7108 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7109 | for (auto &I : FI.arguments()) |
| 7110 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
| 7113 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7114 | if (!isAggregateTypeForABI(Ty)) { |
| 7115 | // Treat an enum type as its underlying type. |
| 7116 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7117 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7118 | |
| 7119 | return (Ty->isPromotableIntegerType() ? |
| 7120 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 7121 | } |
| 7122 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7123 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7124 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7125 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7126 | // Ignore empty records. |
| 7127 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7128 | return ABIArgInfo::getIgnore(); |
| 7129 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7130 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7131 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7132 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7133 | // Pass in the smallest viable integer type. |
| 7134 | else if (Size > 32) |
| 7135 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7136 | else if (Size > 16) |
| 7137 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7138 | else if (Size > 8) |
| 7139 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7140 | else |
| 7141 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7142 | } |
| 7143 | |
| 7144 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7145 | if (RetTy->isVoidType()) |
| 7146 | return ABIArgInfo::getIgnore(); |
| 7147 | |
| 7148 | // Large vector types should be returned via memory. |
| 7149 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7150 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7151 | |
| 7152 | if (!isAggregateTypeForABI(RetTy)) { |
| 7153 | // Treat an enum type as its underlying type. |
| 7154 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7155 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7156 | |
| 7157 | return (RetTy->isPromotableIntegerType() ? |
| 7158 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 7159 | } |
| 7160 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7161 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7162 | return ABIArgInfo::getIgnore(); |
| 7163 | |
| 7164 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7165 | // are returned indirectly. |
| 7166 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7167 | if (Size <= 64) { |
| 7168 | // Return in the smallest viable integer type. |
| 7169 | if (Size <= 8) |
| 7170 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7171 | if (Size <= 16) |
| 7172 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7173 | if (Size <= 32) |
| 7174 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7175 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7176 | } |
| 7177 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7178 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7179 | } |
| 7180 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7181 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7182 | QualType Ty) const { |
| 7183 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7184 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7185 | getContext().getTypeInfoInChars(Ty), |
| 7186 | CharUnits::fromQuantity(4), |
| 7187 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7188 | } |
| 7189 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7190 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7191 | // Lanai ABI Implementation |
| 7192 | //===----------------------------------------------------------------------===// |
| 7193 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7194 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7195 | class LanaiABIInfo : public DefaultABIInfo { |
| 7196 | public: |
| 7197 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7198 | |
| 7199 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7200 | |
| 7201 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7202 | CCState State(FI.getCallingConvention()); |
| 7203 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7204 | // regparm attribute set. |
| 7205 | if (FI.getHasRegParm()) { |
| 7206 | State.FreeRegs = FI.getRegParm(); |
| 7207 | } else { |
| 7208 | State.FreeRegs = 4; |
| 7209 | } |
| 7210 | |
| 7211 | if (!getCXXABI().classifyReturnType(FI)) |
| 7212 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7213 | for (auto &I : FI.arguments()) |
| 7214 | I.info = classifyArgumentType(I.type, State); |
| 7215 | } |
| 7216 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7217 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7218 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7219 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7220 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7221 | |
| 7222 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7223 | unsigned Size = getContext().getTypeSize(Ty); |
| 7224 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7225 | |
| 7226 | if (SizeInRegs == 0) |
| 7227 | return false; |
| 7228 | |
| 7229 | if (SizeInRegs > State.FreeRegs) { |
| 7230 | State.FreeRegs = 0; |
| 7231 | return false; |
| 7232 | } |
| 7233 | |
| 7234 | State.FreeRegs -= SizeInRegs; |
| 7235 | |
| 7236 | return true; |
| 7237 | } |
| 7238 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7239 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7240 | CCState &State) const { |
| 7241 | if (!ByVal) { |
| 7242 | if (State.FreeRegs) { |
| 7243 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7244 | return getNaturalAlignIndirectInReg(Ty); |
| 7245 | } |
| 7246 | return getNaturalAlignIndirect(Ty, false); |
| 7247 | } |
| 7248 | |
| 7249 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7250 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7251 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7252 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7253 | /*Realign=*/TypeAlign > |
| 7254 | MinABIStackAlignInBytes); |
| 7255 | } |
| 7256 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7257 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7258 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7259 | // Check with the C++ ABI first. |
| 7260 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7261 | if (RT) { |
| 7262 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7263 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7264 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7265 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7266 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7267 | } |
| 7268 | } |
| 7269 | |
| 7270 | if (isAggregateTypeForABI(Ty)) { |
| 7271 | // Structures with flexible arrays are always indirect. |
| 7272 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7273 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7274 | |
| 7275 | // Ignore empty structs/unions. |
| 7276 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7277 | return ABIArgInfo::getIgnore(); |
| 7278 | |
| 7279 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7280 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7281 | if (SizeInRegs <= State.FreeRegs) { |
| 7282 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7283 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7284 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7285 | State.FreeRegs -= SizeInRegs; |
| 7286 | return ABIArgInfo::getDirectInReg(Result); |
| 7287 | } else { |
| 7288 | State.FreeRegs = 0; |
| 7289 | } |
| 7290 | return getIndirectResult(Ty, true, State); |
| 7291 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7292 | |
| 7293 | // Treat an enum type as its underlying type. |
| 7294 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7295 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7296 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7297 | bool InReg = shouldUseInReg(Ty, State); |
| 7298 | if (Ty->isPromotableIntegerType()) { |
| 7299 | if (InReg) |
| 7300 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7301 | return ABIArgInfo::getExtend(); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7302 | } |
| 7303 | if (InReg) |
| 7304 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7305 | return ABIArgInfo::getDirect(); |
| 7306 | } |
| 7307 | |
| 7308 | namespace { |
| 7309 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7310 | public: |
| 7311 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7312 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7313 | }; |
| 7314 | } |
| 7315 | |
| 7316 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7317 | // AMDGPU ABI Implementation |
| 7318 | //===----------------------------------------------------------------------===// |
| 7319 | |
| 7320 | namespace { |
| 7321 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7322 | class AMDGPUABIInfo final : public DefaultABIInfo { |
| 7323 | public: |
| 7324 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7325 | |
| 7326 | private: |
| 7327 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 7328 | |
| 7329 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7330 | }; |
| 7331 | |
| 7332 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7333 | if (!getCXXABI().classifyReturnType(FI)) |
| 7334 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7335 | |
| 7336 | unsigned CC = FI.getCallingConvention(); |
| 7337 | for (auto &Arg : FI.arguments()) |
| 7338 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) |
| 7339 | Arg.info = classifyArgumentType(Arg.type); |
| 7340 | else |
| 7341 | Arg.info = DefaultABIInfo::classifyArgumentType(Arg.type); |
| 7342 | } |
| 7343 | |
| 7344 | /// \brief Classify argument of given type \p Ty. |
| 7345 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty) const { |
| 7346 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7347 | if (!StrTy) { |
| 7348 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7349 | } |
| 7350 | |
| 7351 | // Coerce single element structs to its element. |
| 7352 | if (StrTy->getNumElements() == 1) { |
| 7353 | return ABIArgInfo::getDirect(); |
| 7354 | } |
| 7355 | |
| 7356 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7357 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7358 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 7359 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 7360 | } |
| 7361 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7362 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7363 | public: |
| 7364 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7365 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7366 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7367 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7368 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7369 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7370 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7371 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7372 | |
| 7373 | unsigned getASTAllocaAddressSpace() const override { |
| 7374 | return LangAS::FirstTargetAddressSpace + |
| 7375 | getABIInfo().getDataLayout().getAllocaAddrSpace(); |
| 7376 | } |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7377 | unsigned getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7378 | const VarDecl *D) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7379 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7380 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7381 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7382 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7383 | const Decl *D, |
| 7384 | llvm::GlobalValue *GV, |
| 7385 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7386 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7387 | if (!FD) |
| 7388 | return; |
| 7389 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7390 | llvm::Function *F = cast<llvm::Function>(GV); |
| 7391 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7392 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 7393 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
| 7394 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 7395 | if (ReqdWGS || FlatWGS) { |
| 7396 | unsigned Min = FlatWGS ? FlatWGS->getMin() : 0; |
| 7397 | unsigned Max = FlatWGS ? FlatWGS->getMax() : 0; |
| 7398 | if (ReqdWGS && Min == 0 && Max == 0) |
| 7399 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7400 | |
| 7401 | if (Min != 0) { |
| 7402 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 7403 | |
| 7404 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 7405 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 7406 | } else |
| 7407 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7408 | } |
| 7409 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7410 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 7411 | unsigned Min = Attr->getMin(); |
| 7412 | unsigned Max = Attr->getMax(); |
| 7413 | |
| 7414 | if (Min != 0) { |
| 7415 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 7416 | |
| 7417 | std::string AttrVal = llvm::utostr(Min); |
| 7418 | if (Max != 0) |
| 7419 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 7420 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 7421 | } else |
| 7422 | assert(Max == 0 && "Max must be zero"); |
| 7423 | } |
| 7424 | |
| 7425 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7426 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7427 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7428 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7429 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 7430 | } |
| 7431 | |
| 7432 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7433 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 7434 | |
| 7435 | if (NumVGPR != 0) |
| 7436 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7437 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7438 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7439 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7440 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7441 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7442 | } |
| 7443 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7444 | // Currently LLVM assumes null pointers always have value 0, |
| 7445 | // which results in incorrectly transformed IR. Therefore, instead of |
| 7446 | // emitting null pointers in private and local address spaces, a null |
| 7447 | // pointer in generic address space is emitted which is casted to a |
| 7448 | // pointer in local or private address space. |
| 7449 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 7450 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 7451 | QualType QT) const { |
| 7452 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 7453 | return llvm::ConstantPointerNull::get(PT); |
| 7454 | |
| 7455 | auto &Ctx = CGM.getContext(); |
| 7456 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 7457 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 7458 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 7459 | llvm::ConstantPointerNull::get(NPT), PT); |
| 7460 | } |
| 7461 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7462 | unsigned |
| 7463 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7464 | const VarDecl *D) const { |
| 7465 | assert(!CGM.getLangOpts().OpenCL && |
| 7466 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 7467 | "Address space agnostic languages only"); |
| 7468 | unsigned DefaultGlobalAS = |
| 7469 | LangAS::FirstTargetAddressSpace + |
| 7470 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global); |
| 7471 | if (!D) |
| 7472 | return DefaultGlobalAS; |
| 7473 | |
| 7474 | unsigned AddrSpace = D->getType().getAddressSpace(); |
| 7475 | assert(AddrSpace == LangAS::Default || |
| 7476 | AddrSpace >= LangAS::FirstTargetAddressSpace); |
| 7477 | if (AddrSpace != LangAS::Default) |
| 7478 | return AddrSpace; |
| 7479 | |
| 7480 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 7481 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 7482 | return ConstAS.getValue(); |
| 7483 | } |
| 7484 | return DefaultGlobalAS; |
| 7485 | } |
| 7486 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7487 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 7488 | // SPARC v8 ABI Implementation. |
| 7489 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7490 | // |
| 7491 | // Ensures that complex values are passed in registers. |
| 7492 | // |
| 7493 | namespace { |
| 7494 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 7495 | public: |
| 7496 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7497 | |
| 7498 | private: |
| 7499 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7500 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7501 | }; |
| 7502 | } // end anonymous namespace |
| 7503 | |
| 7504 | |
| 7505 | ABIArgInfo |
| 7506 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 7507 | if (Ty->isAnyComplexType()) { |
| 7508 | return ABIArgInfo::getDirect(); |
| 7509 | } |
| 7510 | else { |
| 7511 | return DefaultABIInfo::classifyReturnType(Ty); |
| 7512 | } |
| 7513 | } |
| 7514 | |
| 7515 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7516 | |
| 7517 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7518 | for (auto &Arg : FI.arguments()) |
| 7519 | Arg.info = classifyArgumentType(Arg.type); |
| 7520 | } |
| 7521 | |
| 7522 | namespace { |
| 7523 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7524 | public: |
| 7525 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7526 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 7527 | }; |
| 7528 | } // end anonymous namespace |
| 7529 | |
| 7530 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7531 | // SPARC v9 ABI Implementation. |
| 7532 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7533 | // |
| 7534 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 7535 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 7536 | // the array, structs larger than 16 bytes are passed indirectly. |
| 7537 | // |
| 7538 | // One case requires special care: |
| 7539 | // |
| 7540 | // struct mixed { |
| 7541 | // int i; |
| 7542 | // float f; |
| 7543 | // }; |
| 7544 | // |
| 7545 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 7546 | // parameter array, but the int is passed in an integer register, and the float |
| 7547 | // is passed in a floating point register. This is represented as two arguments |
| 7548 | // with the LLVM IR inreg attribute: |
| 7549 | // |
| 7550 | // declare void f(i32 inreg %i, float inreg %f) |
| 7551 | // |
| 7552 | // The code generator will only allocate 4 bytes from the parameter array for |
| 7553 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 7554 | // bytes. |
| 7555 | // |
| 7556 | namespace { |
| 7557 | class SparcV9ABIInfo : public ABIInfo { |
| 7558 | public: |
| 7559 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7560 | |
| 7561 | private: |
| 7562 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7563 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7564 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7565 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7566 | |
| 7567 | // Coercion type builder for structs passed in registers. The coercion type |
| 7568 | // serves two purposes: |
| 7569 | // |
| 7570 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 7571 | // in registers. |
| 7572 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 7573 | // code generator knows to pass them in floating point registers. |
| 7574 | // |
| 7575 | // We also compute the InReg flag which indicates that the struct contains |
| 7576 | // aligned 32-bit floats. |
| 7577 | // |
| 7578 | struct CoerceBuilder { |
| 7579 | llvm::LLVMContext &Context; |
| 7580 | const llvm::DataLayout &DL; |
| 7581 | SmallVector<llvm::Type*, 8> Elems; |
| 7582 | uint64_t Size; |
| 7583 | bool InReg; |
| 7584 | |
| 7585 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 7586 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 7587 | |
| 7588 | // Pad Elems with integers until Size is ToSize. |
| 7589 | void pad(uint64_t ToSize) { |
| 7590 | assert(ToSize >= Size && "Cannot remove elements"); |
| 7591 | if (ToSize == Size) |
| 7592 | return; |
| 7593 | |
| 7594 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7595 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7596 | if (Aligned > Size && Aligned <= ToSize) { |
| 7597 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 7598 | Size = Aligned; |
| 7599 | } |
| 7600 | |
| 7601 | // Add whole 64-bit words. |
| 7602 | while (Size + 64 <= ToSize) { |
| 7603 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 7604 | Size += 64; |
| 7605 | } |
| 7606 | |
| 7607 | // Final in-word padding. |
| 7608 | if (Size < ToSize) { |
| 7609 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 7610 | Size = ToSize; |
| 7611 | } |
| 7612 | } |
| 7613 | |
| 7614 | // Add a floating point element at Offset. |
| 7615 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 7616 | // Unaligned floats are treated as integers. |
| 7617 | if (Offset % Bits) |
| 7618 | return; |
| 7619 | // The InReg flag is only required if there are any floats < 64 bits. |
| 7620 | if (Bits < 64) |
| 7621 | InReg = true; |
| 7622 | pad(Offset); |
| 7623 | Elems.push_back(Ty); |
| 7624 | Size = Offset + Bits; |
| 7625 | } |
| 7626 | |
| 7627 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 7628 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 7629 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 7630 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 7631 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 7632 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 7633 | switch (ElemTy->getTypeID()) { |
| 7634 | case llvm::Type::StructTyID: |
| 7635 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 7636 | break; |
| 7637 | case llvm::Type::FloatTyID: |
| 7638 | addFloat(ElemOffset, ElemTy, 32); |
| 7639 | break; |
| 7640 | case llvm::Type::DoubleTyID: |
| 7641 | addFloat(ElemOffset, ElemTy, 64); |
| 7642 | break; |
| 7643 | case llvm::Type::FP128TyID: |
| 7644 | addFloat(ElemOffset, ElemTy, 128); |
| 7645 | break; |
| 7646 | case llvm::Type::PointerTyID: |
| 7647 | if (ElemOffset % 64 == 0) { |
| 7648 | pad(ElemOffset); |
| 7649 | Elems.push_back(ElemTy); |
| 7650 | Size += 64; |
| 7651 | } |
| 7652 | break; |
| 7653 | default: |
| 7654 | break; |
| 7655 | } |
| 7656 | } |
| 7657 | } |
| 7658 | |
| 7659 | // Check if Ty is a usable substitute for the coercion type. |
| 7660 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 7661 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7662 | } |
| 7663 | |
| 7664 | // Get the coercion type as a literal struct type. |
| 7665 | llvm::Type *getType() const { |
| 7666 | if (Elems.size() == 1) |
| 7667 | return Elems.front(); |
| 7668 | else |
| 7669 | return llvm::StructType::get(Context, Elems); |
| 7670 | } |
| 7671 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7672 | }; |
| 7673 | } // end anonymous namespace |
| 7674 | |
| 7675 | ABIArgInfo |
| 7676 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 7677 | if (Ty->isVoidType()) |
| 7678 | return ABIArgInfo::getIgnore(); |
| 7679 | |
| 7680 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7681 | |
| 7682 | // Anything too big to fit in registers is passed with an explicit indirect |
| 7683 | // pointer / sret pointer. |
| 7684 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7685 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7686 | |
| 7687 | // Treat an enum type as its underlying type. |
| 7688 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7689 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7690 | |
| 7691 | // Integer types smaller than a register are extended. |
| 7692 | if (Size < 64 && Ty->isIntegerType()) |
| 7693 | return ABIArgInfo::getExtend(); |
| 7694 | |
| 7695 | // Other non-aggregates go in registers. |
| 7696 | if (!isAggregateTypeForABI(Ty)) |
| 7697 | return ABIArgInfo::getDirect(); |
| 7698 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7699 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 7700 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 7701 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7702 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7703 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7704 | // 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] | 7705 | // Build a coercion type from the LLVM struct type. |
| 7706 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7707 | if (!StrTy) |
| 7708 | return ABIArgInfo::getDirect(); |
| 7709 | |
| 7710 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 7711 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7712 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7713 | |
| 7714 | // Try to use the original type for coercion. |
| 7715 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 7716 | |
| 7717 | if (CB.InReg) |
| 7718 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 7719 | else |
| 7720 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7721 | } |
| 7722 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7723 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7724 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7725 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 7726 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7727 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7728 | AI.setCoerceToType(ArgTy); |
| 7729 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7730 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7731 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7732 | CGBuilderTy &Builder = CGF.Builder; |
| 7733 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 7734 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 7735 | |
| 7736 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 7737 | |
| 7738 | Address ArgAddr = Address::invalid(); |
| 7739 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7740 | switch (AI.getKind()) { |
| 7741 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 7742 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 7743 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7744 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 7745 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7746 | case ABIArgInfo::Extend: { |
| 7747 | Stride = SlotSize; |
| 7748 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 7749 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7750 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7751 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7752 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7753 | case ABIArgInfo::Direct: { |
| 7754 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7755 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7756 | ArgAddr = Addr; |
| 7757 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7758 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7759 | |
| 7760 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7761 | Stride = SlotSize; |
| 7762 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 7763 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 7764 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7765 | break; |
| 7766 | |
| 7767 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7768 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7769 | } |
| 7770 | |
| 7771 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7772 | llvm::Value *NextPtr = |
| 7773 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 7774 | Builder.CreateStore(NextPtr, VAListAddr); |
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 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7777 | } |
| 7778 | |
| 7779 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7780 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7781 | for (auto &I : FI.arguments()) |
| 7782 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7783 | } |
| 7784 | |
| 7785 | namespace { |
| 7786 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7787 | public: |
| 7788 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7789 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7790 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7791 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7792 | return 14; |
| 7793 | } |
| 7794 | |
| 7795 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7796 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7797 | }; |
| 7798 | } // end anonymous namespace |
| 7799 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7800 | bool |
| 7801 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7802 | llvm::Value *Address) const { |
| 7803 | // This is calculated from the LLVM and GCC tables and verified |
| 7804 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 7805 | |
| 7806 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 7807 | |
| 7808 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 7809 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 7810 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 7811 | |
| 7812 | // 0-31: the 8-byte general-purpose registers |
| 7813 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 7814 | |
| 7815 | // 32-63: f0-31, the 4-byte floating-point registers |
| 7816 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 7817 | |
| 7818 | // Y = 64 |
| 7819 | // PSR = 65 |
| 7820 | // WIM = 66 |
| 7821 | // TBR = 67 |
| 7822 | // PC = 68 |
| 7823 | // NPC = 69 |
| 7824 | // FSR = 70 |
| 7825 | // CSR = 71 |
| 7826 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7827 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7828 | // 72-87: d0-15, the 8-byte floating-point registers |
| 7829 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 7830 | |
| 7831 | return false; |
| 7832 | } |
| 7833 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7834 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7835 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7836 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7837 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7838 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7839 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7840 | |
| 7841 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 7842 | /// it by reference between functions that append to it. |
| 7843 | typedef llvm::SmallString<128> SmallStringEnc; |
| 7844 | |
| 7845 | /// TypeStringCache caches the meta encodings of Types. |
| 7846 | /// |
| 7847 | /// The reason for caching TypeStrings is two fold: |
| 7848 | /// 1. To cache a type's encoding for later uses; |
| 7849 | /// 2. As a means to break recursive member type inclusion. |
| 7850 | /// |
| 7851 | /// A cache Entry can have a Status of: |
| 7852 | /// NonRecursive: The type encoding is not recursive; |
| 7853 | /// Recursive: The type encoding is recursive; |
| 7854 | /// Incomplete: An incomplete TypeString; |
| 7855 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 7856 | /// Recursive type encoding. |
| 7857 | /// |
| 7858 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 7859 | /// as possible. Whilst it may contain types which are recursive, the type |
| 7860 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 7861 | /// the type is encountered. |
| 7862 | /// |
| 7863 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 7864 | /// possible. The type itself is recursive and it may contain other types which |
| 7865 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 7866 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 7867 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 7868 | /// |
| 7869 | /// An Incomplete entry is always a RecordType and only encodes its |
| 7870 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 7871 | /// are placed into the cache during type expansion as a means to identify and |
| 7872 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 7873 | /// the entry becomes IncompleteUsed. |
| 7874 | /// |
| 7875 | /// During the expansion of a RecordType's members: |
| 7876 | /// |
| 7877 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 7878 | /// cached encoding is used; |
| 7879 | /// |
| 7880 | /// If the cache contains a Recursive encoding for the member type, the |
| 7881 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 7882 | /// |
| 7883 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 7884 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 7885 | /// |
| 7886 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 7887 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 7888 | /// it is swapped back in; |
| 7889 | /// |
| 7890 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 7891 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 7892 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 7893 | /// |
| 7894 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 7895 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 7896 | /// Else the member is part of a recursive type and thus the recursion has |
| 7897 | /// been exited too soon for the encoding to be correct for the member. |
| 7898 | /// |
| 7899 | class TypeStringCache { |
| 7900 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 7901 | struct Entry { |
| 7902 | std::string Str; // The encoded TypeString for the type. |
| 7903 | enum Status State; // Information about the encoding in 'Str'. |
| 7904 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 7905 | // during the expansion of RecordType's members. |
| 7906 | }; |
| 7907 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 7908 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 7909 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 7910 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 7911 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7912 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 7913 | bool removeIncomplete(const IdentifierInfo *ID); |
| 7914 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7915 | bool IsRecursive); |
| 7916 | StringRef lookupStr(const IdentifierInfo *ID); |
| 7917 | }; |
| 7918 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7919 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7920 | /// FieldEncoding is a helper for this ordering process. |
| 7921 | class FieldEncoding { |
| 7922 | bool HasName; |
| 7923 | std::string Enc; |
| 7924 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 7925 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 7926 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7927 | bool operator<(const FieldEncoding &rhs) const { |
| 7928 | if (HasName != rhs.HasName) return HasName; |
| 7929 | return Enc < rhs.Enc; |
| 7930 | } |
| 7931 | }; |
| 7932 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7933 | class XCoreABIInfo : public DefaultABIInfo { |
| 7934 | public: |
| 7935 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7936 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7937 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7938 | }; |
| 7939 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7940 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7941 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7942 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7943 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7944 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 7945 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7946 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7947 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7948 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7949 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7950 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 7951 | // TODO: this implementation is likely now redundant with the default |
| 7952 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7953 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7954 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7955 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7956 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7957 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7958 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 7959 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7960 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7961 | // Handle the argument. |
| 7962 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7963 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7964 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7965 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7966 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7967 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7968 | |
| 7969 | Address Val = Address::invalid(); |
| 7970 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7971 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7972 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 7973 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 7974 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7975 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 7976 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7977 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 7978 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7979 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7980 | case ABIArgInfo::Extend: |
| 7981 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7982 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 7983 | ArgSize = CharUnits::fromQuantity( |
| 7984 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7985 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7986 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7987 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7988 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 7989 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 7990 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7991 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7992 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7993 | |
| 7994 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7995 | if (!ArgSize.isZero()) { |
| 7996 | llvm::Value *APN = |
| 7997 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 7998 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7999 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8000 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8001 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8002 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8003 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8004 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8005 | /// into the cache as a means to identify and break recursion. |
| 8006 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8007 | /// be reinserted by removeIncomplete(). |
| 8008 | /// All other types of encoding should have been used rather than arriving here. |
| 8009 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8010 | std::string StubEnc) { |
| 8011 | if (!ID) |
| 8012 | return; |
| 8013 | Entry &E = Map[ID]; |
| 8014 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8015 | "Incorrectly use of addIncomplete"); |
| 8016 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8017 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8018 | E.Str.swap(StubEnc); |
| 8019 | E.State = Incomplete; |
| 8020 | ++IncompleteCount; |
| 8021 | } |
| 8022 | |
| 8023 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8024 | /// must be removed from the cache. |
| 8025 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8026 | /// Returns true if the RecordType was defined recursively. |
| 8027 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8028 | if (!ID) |
| 8029 | return false; |
| 8030 | auto I = Map.find(ID); |
| 8031 | assert(I != Map.end() && "Entry not present"); |
| 8032 | Entry &E = I->second; |
| 8033 | assert( (E.State == Incomplete || |
| 8034 | E.State == IncompleteUsed) && |
| 8035 | "Entry must be an incomplete type"); |
| 8036 | bool IsRecursive = false; |
| 8037 | if (E.State == IncompleteUsed) { |
| 8038 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8039 | IsRecursive = true; |
| 8040 | --IncompleteUsedCount; |
| 8041 | } |
| 8042 | if (E.Swapped.empty()) |
| 8043 | Map.erase(I); |
| 8044 | else { |
| 8045 | // Swap the Recursive back. |
| 8046 | E.Swapped.swap(E.Str); |
| 8047 | E.Swapped.clear(); |
| 8048 | E.State = Recursive; |
| 8049 | } |
| 8050 | --IncompleteCount; |
| 8051 | return IsRecursive; |
| 8052 | } |
| 8053 | |
| 8054 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8055 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8056 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8057 | bool IsRecursive) { |
| 8058 | if (!ID || IncompleteUsedCount) |
| 8059 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8060 | Entry &E = Map[ID]; |
| 8061 | if (IsRecursive && !E.Str.empty()) { |
| 8062 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8063 | "This is not the same Recursive entry"); |
| 8064 | // The parent container was not recursive after all, so we could have used |
| 8065 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8066 | // we started viz: IncompleteCount!=0. |
| 8067 | return; |
| 8068 | } |
| 8069 | assert(E.Str.empty() && "Entry already present"); |
| 8070 | E.Str = Str.str(); |
| 8071 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8072 | } |
| 8073 | |
| 8074 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8075 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8076 | /// encoding is Recursive, return an empty StringRef. |
| 8077 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8078 | if (!ID) |
| 8079 | return StringRef(); // We have no key. |
| 8080 | auto I = Map.find(ID); |
| 8081 | if (I == Map.end()) |
| 8082 | return StringRef(); // We have no encoding. |
| 8083 | Entry &E = I->second; |
| 8084 | if (E.State == Recursive && IncompleteCount) |
| 8085 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8086 | |
| 8087 | if (E.State == Incomplete) { |
| 8088 | // The incomplete type is being used to break out of recursion. |
| 8089 | E.State = IncompleteUsed; |
| 8090 | ++IncompleteUsedCount; |
| 8091 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8092 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8093 | } |
| 8094 | |
| 8095 | /// The XCore ABI includes a type information section that communicates symbol |
| 8096 | /// type information to the linker. The linker uses this information to verify |
| 8097 | /// safety/correctness of things such as array bound and pointers et al. |
| 8098 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8099 | /// This type information (TypeString) is emitted into meta data for all global |
| 8100 | /// symbols: definitions, declarations, functions & variables. |
| 8101 | /// |
| 8102 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8103 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8104 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8105 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8106 | /// |
| 8107 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8108 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8109 | |
| 8110 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8111 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8112 | CodeGen::CodeGenModule &CGM) const { |
| 8113 | SmallStringEnc Enc; |
| 8114 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8115 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8116 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8117 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8118 | llvm::NamedMDNode *MD = |
| 8119 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8120 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8121 | } |
| 8122 | } |
| 8123 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8124 | //===----------------------------------------------------------------------===// |
| 8125 | // SPIR ABI Implementation |
| 8126 | //===----------------------------------------------------------------------===// |
| 8127 | |
| 8128 | namespace { |
| 8129 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8130 | public: |
| 8131 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8132 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8133 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8134 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8135 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8136 | } // End anonymous namespace. |
| 8137 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8138 | namespace clang { |
| 8139 | namespace CodeGen { |
| 8140 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8141 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8142 | SPIRABI.computeInfo(FI); |
| 8143 | } |
| 8144 | } |
| 8145 | } |
| 8146 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8147 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8148 | return llvm::CallingConv::SPIR_KERNEL; |
| 8149 | } |
| 8150 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8151 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8152 | const CodeGen::CodeGenModule &CGM, |
| 8153 | TypeStringCache &TSC); |
| 8154 | |
| 8155 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8156 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8157 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8158 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8159 | const RecordDecl *RD, |
| 8160 | const CodeGen::CodeGenModule &CGM, |
| 8161 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8162 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8163 | SmallStringEnc Enc; |
| 8164 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8165 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8166 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8167 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8168 | Enc += "b("; |
| 8169 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8170 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8171 | Enc += ':'; |
| 8172 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8173 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8174 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8175 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8176 | Enc += ')'; |
| 8177 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 8178 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8179 | } |
| 8180 | return true; |
| 8181 | } |
| 8182 | |
| 8183 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 8184 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 8185 | /// Union types have their fields ordered according to the ABI. |
| 8186 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 8187 | const CodeGen::CodeGenModule &CGM, |
| 8188 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 8189 | // Append the cached TypeString if we have one. |
| 8190 | StringRef TypeString = TSC.lookupStr(ID); |
| 8191 | if (!TypeString.empty()) { |
| 8192 | Enc += TypeString; |
| 8193 | return true; |
| 8194 | } |
| 8195 | |
| 8196 | // Start to emit an incomplete TypeString. |
| 8197 | size_t Start = Enc.size(); |
| 8198 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 8199 | Enc += '('; |
| 8200 | if (ID) |
| 8201 | Enc += ID->getName(); |
| 8202 | Enc += "){"; |
| 8203 | |
| 8204 | // We collect all encoded fields and order as necessary. |
| 8205 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8206 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 8207 | if (RD && !RD->field_empty()) { |
| 8208 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 8209 | // so that recursive calls to this RecordType will use it whilst building a |
| 8210 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8211 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8212 | std::string StubEnc(Enc.substr(Start).str()); |
| 8213 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 8214 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 8215 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 8216 | (void) TSC.removeIncomplete(ID); |
| 8217 | return false; |
| 8218 | } |
| 8219 | IsRecursive = TSC.removeIncomplete(ID); |
| 8220 | // The ABI requires unions to be sorted but not structures. |
| 8221 | // See FieldEncoding::operator< for sort algorithm. |
| 8222 | if (RT->isUnionType()) |
| 8223 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8224 | // We can now complete the TypeString. |
| 8225 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8226 | for (unsigned I = 0; I != E; ++I) { |
| 8227 | if (I) |
| 8228 | Enc += ','; |
| 8229 | Enc += FE[I].str(); |
| 8230 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8231 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8232 | Enc += '}'; |
| 8233 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 8234 | return true; |
| 8235 | } |
| 8236 | |
| 8237 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 8238 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 8239 | TypeStringCache &TSC, |
| 8240 | const IdentifierInfo *ID) { |
| 8241 | // Append the cached TypeString if we have one. |
| 8242 | StringRef TypeString = TSC.lookupStr(ID); |
| 8243 | if (!TypeString.empty()) { |
| 8244 | Enc += TypeString; |
| 8245 | return true; |
| 8246 | } |
| 8247 | |
| 8248 | size_t Start = Enc.size(); |
| 8249 | Enc += "e("; |
| 8250 | if (ID) |
| 8251 | Enc += ID->getName(); |
| 8252 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8253 | |
| 8254 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8255 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8256 | SmallVector<FieldEncoding, 16> FE; |
| 8257 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 8258 | ++I) { |
| 8259 | SmallStringEnc EnumEnc; |
| 8260 | EnumEnc += "m("; |
| 8261 | EnumEnc += I->getName(); |
| 8262 | EnumEnc += "){"; |
| 8263 | I->getInitVal().toString(EnumEnc); |
| 8264 | EnumEnc += '}'; |
| 8265 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 8266 | } |
| 8267 | std::sort(FE.begin(), FE.end()); |
| 8268 | unsigned E = FE.size(); |
| 8269 | for (unsigned I = 0; I != E; ++I) { |
| 8270 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8271 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8272 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8273 | } |
| 8274 | } |
| 8275 | Enc += '}'; |
| 8276 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 8277 | return true; |
| 8278 | } |
| 8279 | |
| 8280 | /// Appends type's qualifier to Enc. |
| 8281 | /// This is done prior to appending the type's encoding. |
| 8282 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 8283 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 8284 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8285 | int Lookup = 0; |
| 8286 | if (QT.isConstQualified()) |
| 8287 | Lookup += 1<<0; |
| 8288 | if (QT.isRestrictQualified()) |
| 8289 | Lookup += 1<<1; |
| 8290 | if (QT.isVolatileQualified()) |
| 8291 | Lookup += 1<<2; |
| 8292 | Enc += Table[Lookup]; |
| 8293 | } |
| 8294 | |
| 8295 | /// Appends built-in types to Enc. |
| 8296 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 8297 | const char *EncType; |
| 8298 | switch (BT->getKind()) { |
| 8299 | case BuiltinType::Void: |
| 8300 | EncType = "0"; |
| 8301 | break; |
| 8302 | case BuiltinType::Bool: |
| 8303 | EncType = "b"; |
| 8304 | break; |
| 8305 | case BuiltinType::Char_U: |
| 8306 | EncType = "uc"; |
| 8307 | break; |
| 8308 | case BuiltinType::UChar: |
| 8309 | EncType = "uc"; |
| 8310 | break; |
| 8311 | case BuiltinType::SChar: |
| 8312 | EncType = "sc"; |
| 8313 | break; |
| 8314 | case BuiltinType::UShort: |
| 8315 | EncType = "us"; |
| 8316 | break; |
| 8317 | case BuiltinType::Short: |
| 8318 | EncType = "ss"; |
| 8319 | break; |
| 8320 | case BuiltinType::UInt: |
| 8321 | EncType = "ui"; |
| 8322 | break; |
| 8323 | case BuiltinType::Int: |
| 8324 | EncType = "si"; |
| 8325 | break; |
| 8326 | case BuiltinType::ULong: |
| 8327 | EncType = "ul"; |
| 8328 | break; |
| 8329 | case BuiltinType::Long: |
| 8330 | EncType = "sl"; |
| 8331 | break; |
| 8332 | case BuiltinType::ULongLong: |
| 8333 | EncType = "ull"; |
| 8334 | break; |
| 8335 | case BuiltinType::LongLong: |
| 8336 | EncType = "sll"; |
| 8337 | break; |
| 8338 | case BuiltinType::Float: |
| 8339 | EncType = "ft"; |
| 8340 | break; |
| 8341 | case BuiltinType::Double: |
| 8342 | EncType = "d"; |
| 8343 | break; |
| 8344 | case BuiltinType::LongDouble: |
| 8345 | EncType = "ld"; |
| 8346 | break; |
| 8347 | default: |
| 8348 | return false; |
| 8349 | } |
| 8350 | Enc += EncType; |
| 8351 | return true; |
| 8352 | } |
| 8353 | |
| 8354 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 8355 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 8356 | const CodeGen::CodeGenModule &CGM, |
| 8357 | TypeStringCache &TSC) { |
| 8358 | Enc += "p("; |
| 8359 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 8360 | return false; |
| 8361 | Enc += ')'; |
| 8362 | return true; |
| 8363 | } |
| 8364 | |
| 8365 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8366 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 8367 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8368 | const CodeGen::CodeGenModule &CGM, |
| 8369 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 8370 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 8371 | return false; |
| 8372 | Enc += "a("; |
| 8373 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 8374 | CAT->getSize().toStringUnsigned(Enc); |
| 8375 | else |
| 8376 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 8377 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8378 | // The Qualifiers should be attached to the type rather than the array. |
| 8379 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8380 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 8381 | return false; |
| 8382 | Enc += ')'; |
| 8383 | return true; |
| 8384 | } |
| 8385 | |
| 8386 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 8387 | /// and the arguments. |
| 8388 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 8389 | const CodeGen::CodeGenModule &CGM, |
| 8390 | TypeStringCache &TSC) { |
| 8391 | Enc += "f{"; |
| 8392 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 8393 | return false; |
| 8394 | Enc += "}("; |
| 8395 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 8396 | // N.B. we are only interested in the adjusted param types. |
| 8397 | auto I = FPT->param_type_begin(); |
| 8398 | auto E = FPT->param_type_end(); |
| 8399 | if (I != E) { |
| 8400 | do { |
| 8401 | if (!appendType(Enc, *I, CGM, TSC)) |
| 8402 | return false; |
| 8403 | ++I; |
| 8404 | if (I != E) |
| 8405 | Enc += ','; |
| 8406 | } while (I != E); |
| 8407 | if (FPT->isVariadic()) |
| 8408 | Enc += ",va"; |
| 8409 | } else { |
| 8410 | if (FPT->isVariadic()) |
| 8411 | Enc += "va"; |
| 8412 | else |
| 8413 | Enc += '0'; |
| 8414 | } |
| 8415 | } |
| 8416 | Enc += ')'; |
| 8417 | return true; |
| 8418 | } |
| 8419 | |
| 8420 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 8421 | /// type encodings. |
| 8422 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8423 | const CodeGen::CodeGenModule &CGM, |
| 8424 | TypeStringCache &TSC) { |
| 8425 | |
| 8426 | QualType QT = QType.getCanonicalType(); |
| 8427 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8428 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 8429 | // The Qualifiers should be attached to the type rather than the array. |
| 8430 | // Thus we don't call appendQualifier() here. |
| 8431 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 8432 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8433 | appendQualifier(Enc, QT); |
| 8434 | |
| 8435 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 8436 | return appendBuiltinType(Enc, BT); |
| 8437 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8438 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 8439 | return appendPointerType(Enc, PT, CGM, TSC); |
| 8440 | |
| 8441 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 8442 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 8443 | |
| 8444 | if (const RecordType *RT = QT->getAsStructureType()) |
| 8445 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8446 | |
| 8447 | if (const RecordType *RT = QT->getAsUnionType()) |
| 8448 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8449 | |
| 8450 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 8451 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 8452 | |
| 8453 | return false; |
| 8454 | } |
| 8455 | |
| 8456 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8457 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 8458 | if (!D) |
| 8459 | return false; |
| 8460 | |
| 8461 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8462 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 8463 | return false; |
| 8464 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 8465 | } |
| 8466 | |
| 8467 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8468 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 8469 | return false; |
| 8470 | QualType QT = VD->getType().getCanonicalType(); |
| 8471 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 8472 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 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, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8476 | } |
| 8477 | return appendType(Enc, QT, CGM, TSC); |
| 8478 | } |
| 8479 | return false; |
| 8480 | } |
| 8481 | |
| 8482 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8483 | //===----------------------------------------------------------------------===// |
| 8484 | // Driver code |
| 8485 | //===----------------------------------------------------------------------===// |
| 8486 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8487 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 8488 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8489 | } |
| 8490 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 8491 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8492 | if (TheTargetCodeGenInfo) |
| 8493 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8494 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8495 | // Helper to set the unique_ptr while still keeping the return value. |
| 8496 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 8497 | this->TheTargetCodeGenInfo.reset(P); |
| 8498 | return *P; |
| 8499 | }; |
| 8500 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 8501 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 8502 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8503 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8504 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8505 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 8506 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8507 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 8508 | case llvm::Triple::mips: |
| 8509 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 8510 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8511 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 8512 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8513 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 8514 | case llvm::Triple::mips64: |
| 8515 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8516 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8517 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 8518 | case llvm::Triple::avr: |
| 8519 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 8520 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 8521 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 8522 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8523 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 8524 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8525 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 8526 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 8527 | return SetCGInfo( |
| 8528 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8529 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8530 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8531 | } |
| 8532 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8533 | case llvm::Triple::wasm32: |
| 8534 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8535 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8536 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8537 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 8538 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8539 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8540 | case llvm::Triple::thumbeb: { |
| 8541 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 8542 | return SetCGInfo( |
| 8543 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 8544 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8545 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8546 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 8547 | StringRef ABIStr = getTarget().getABI(); |
| 8548 | if (ABIStr == "apcs-gnu") |
| 8549 | Kind = ARMABIInfo::APCS; |
| 8550 | else if (ABIStr == "aapcs16") |
| 8551 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 8552 | else if (CodeGenOpts.FloatABI == "hard" || |
| 8553 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8554 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 8555 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8556 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8557 | Kind = ARMABIInfo::AAPCS_VFP; |
| 8558 | |
| 8559 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 8560 | } |
| 8561 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8562 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8563 | return SetCGInfo( |
| 8564 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 8565 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8566 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8567 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8568 | if (getTarget().getABI() == "elfv2") |
| 8569 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8570 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8571 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8572 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8573 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 8574 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8575 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8576 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8577 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 8578 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8579 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8580 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8581 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8582 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8583 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8584 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8585 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 8586 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8587 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8588 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 8589 | case llvm::Triple::nvptx: |
| 8590 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8591 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 8592 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8593 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8594 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8595 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8596 | case llvm::Triple::systemz: { |
| 8597 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8598 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8599 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 8600 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8601 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 8602 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8603 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8604 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8605 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8606 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 8607 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8608 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 8609 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 8610 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8611 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8612 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 8613 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8614 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8615 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8616 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 8617 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8618 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 8619 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8620 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8621 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8622 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8623 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8624 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8625 | X86AVXABILevel AVXLevel = |
| 8626 | (ABI == "avx512" |
| 8627 | ? X86AVXABILevel::AVX512 |
| 8628 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8629 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8630 | switch (Triple.getOS()) { |
| 8631 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8632 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 8633 | case llvm::Triple::PS4: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8634 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8635 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8636 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8637 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8638 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 8639 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8640 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 8641 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8642 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8643 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8644 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 8645 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8646 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 8647 | case llvm::Triple::sparc: |
| 8648 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8649 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8650 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8651 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8652 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8653 | case llvm::Triple::spir: |
| 8654 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8655 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8656 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8657 | } |