Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1 | //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // These classes wrap the information about a call or function |
| 11 | // definition used to handle ABI compliancy. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetInfo.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 16 | #include "ABIInfo.h" |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 18 | #include "CGValue.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 21 | #include "clang/CodeGen/CGFunctionInfo.h" |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 22 | #include "clang/CodeGen/SwiftCallingConv.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CodeGenOptions.h" |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DataLayout.h" |
| 27 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 29 | #include <algorithm> // std::sort |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 30 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 34 | // Helper for coercing an aggregate argument or return value into an integer |
| 35 | // array of the same size (including padding) and alignment. This alternate |
| 36 | // coercion happens only for the RenderScript ABI and can be removed after |
| 37 | // runtimes that rely on it are no longer supported. |
| 38 | // |
| 39 | // RenderScript assumes that the size of the argument / return value in the IR |
| 40 | // is the same as the size of the corresponding qualified type. This helper |
| 41 | // coerces the aggregate type into an array of the same size (including |
| 42 | // padding). This coercion is used in lieu of expansion of struct members or |
| 43 | // other canonical coercions that return a coerced-type of larger size. |
| 44 | // |
| 45 | // Ty - The argument / return value type |
| 46 | // Context - The associated ASTContext |
| 47 | // LLVMContext - The associated LLVMContext |
| 48 | static ABIArgInfo coerceToIntArray(QualType Ty, |
| 49 | ASTContext &Context, |
| 50 | llvm::LLVMContext &LLVMContext) { |
| 51 | // Alignment and Size are measured in bits. |
| 52 | const uint64_t Size = Context.getTypeSize(Ty); |
| 53 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 54 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment); |
| 55 | const uint64_t NumElements = (Size + Alignment - 1) / Alignment; |
| 56 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 57 | } |
| 58 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 59 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 60 | llvm::Value *Array, |
| 61 | llvm::Value *Value, |
| 62 | unsigned FirstIndex, |
| 63 | unsigned LastIndex) { |
| 64 | // Alternatively, we could emit this as a loop in the source. |
| 65 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 66 | llvm::Value *Cell = |
| 67 | Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 68 | Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 72 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 73 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 74 | T->isMemberFunctionPointerType(); |
| 75 | } |
| 76 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 77 | ABIArgInfo |
| 78 | ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, |
| 79 | llvm::Type *Padding) const { |
| 80 | return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), |
| 81 | ByRef, Realign, Padding); |
| 82 | } |
| 83 | |
| 84 | ABIArgInfo |
| 85 | ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { |
| 86 | return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), |
| 87 | /*ByRef*/ false, Realign); |
| 88 | } |
| 89 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 90 | Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 91 | QualType Ty) const { |
| 92 | return Address::invalid(); |
| 93 | } |
| 94 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 95 | ABIInfo::~ABIInfo() {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 96 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 97 | /// Does the given lowering require more than the given number of |
| 98 | /// registers when expanded? |
| 99 | /// |
| 100 | /// This is intended to be the basis of a reasonable basic implementation |
| 101 | /// of should{Pass,Return}IndirectlyForSwift. |
| 102 | /// |
| 103 | /// For most targets, a limit of four total registers is reasonable; this |
| 104 | /// limits the amount of code required in order to move around the value |
| 105 | /// in case it wasn't produced immediately prior to the call by the caller |
| 106 | /// (or wasn't produced in exactly the right registers) or isn't used |
| 107 | /// immediately within the callee. But some targets may need to further |
| 108 | /// limit the register count due to an inability to support that many |
| 109 | /// return registers. |
| 110 | static bool occupiesMoreThan(CodeGenTypes &cgt, |
| 111 | ArrayRef<llvm::Type*> scalarTypes, |
| 112 | unsigned maxAllRegisters) { |
| 113 | unsigned intCount = 0, fpCount = 0; |
| 114 | for (llvm::Type *type : scalarTypes) { |
| 115 | if (type->isPointerTy()) { |
| 116 | intCount++; |
| 117 | } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) { |
| 118 | auto ptrWidth = cgt.getTarget().getPointerWidth(0); |
| 119 | intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth; |
| 120 | } else { |
| 121 | assert(type->isVectorTy() || type->isFloatingPointTy()); |
| 122 | fpCount++; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return (intCount + fpCount > maxAllRegisters); |
| 127 | } |
| 128 | |
| 129 | bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 130 | llvm::Type *eltTy, |
| 131 | unsigned numElts) const { |
| 132 | // The default implementation of this assumes that the target guarantees |
| 133 | // 128-bit SIMD support but nothing more. |
| 134 | return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16); |
| 135 | } |
| 136 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 137 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 138 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 139 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 140 | if (!RD) |
| 141 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 142 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 146 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 147 | const RecordType *RT = T->getAs<RecordType>(); |
| 148 | if (!RT) |
| 149 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 150 | return getRecordArgABI(RT, CXXABI); |
| 151 | } |
| 152 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 153 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 154 | /// should ensure that all elements of the union have the same "machine type". |
| 155 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 156 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 157 | const RecordDecl *UD = UT->getDecl(); |
| 158 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 159 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 160 | return UD->field_begin()->getType(); |
| 161 | } |
| 162 | } |
| 163 | return Ty; |
| 164 | } |
| 165 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 166 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 167 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 170 | ASTContext &ABIInfo::getContext() const { |
| 171 | return CGT.getContext(); |
| 172 | } |
| 173 | |
| 174 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 175 | return CGT.getLLVMContext(); |
| 176 | } |
| 177 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 178 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 179 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 180 | } |
| 181 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 182 | const TargetInfo &ABIInfo::getTarget() const { |
| 183 | return CGT.getTarget(); |
| 184 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 185 | |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 186 | bool ABIInfo:: isAndroid() const { return getTarget().getTriple().isAndroid(); } |
| 187 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 188 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 193 | uint64_t Members) const { |
| 194 | return false; |
| 195 | } |
| 196 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 197 | bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 198 | return false; |
| 199 | } |
| 200 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 201 | LLVM_DUMP_METHOD void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 202 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 203 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 204 | switch (TheKind) { |
| 205 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 206 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 207 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 208 | Ty->print(OS); |
| 209 | else |
| 210 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 211 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 212 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 213 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 214 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 215 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 216 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 217 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 218 | case InAlloca: |
| 219 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 220 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 221 | case Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 222 | OS << "Indirect Align=" << getIndirectAlign().getQuantity() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 223 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 224 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 225 | break; |
| 226 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 227 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 228 | break; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 229 | case CoerceAndExpand: |
| 230 | OS << "CoerceAndExpand Type="; |
| 231 | getCoerceAndExpandType()->print(OS); |
| 232 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 233 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 234 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 237 | // Dynamically round a pointer up to a multiple of the given alignment. |
| 238 | static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, |
| 239 | llvm::Value *Ptr, |
| 240 | CharUnits Align) { |
| 241 | llvm::Value *PtrAsInt = Ptr; |
| 242 | // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; |
| 243 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 244 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 245 | llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); |
| 246 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 247 | llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); |
| 248 | PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, |
| 249 | Ptr->getType(), |
| 250 | Ptr->getName() + ".aligned"); |
| 251 | return PtrAsInt; |
| 252 | } |
| 253 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 254 | /// Emit va_arg for a platform using the common void* representation, |
| 255 | /// where arguments are simply emitted in an array of slots on the stack. |
| 256 | /// |
| 257 | /// This version implements the core direct-value passing rules. |
| 258 | /// |
| 259 | /// \param SlotSize - The size and alignment of a stack slot. |
| 260 | /// Each argument will be allocated to a multiple of this number of |
| 261 | /// slots, and all the slots will be aligned to this value. |
| 262 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 263 | /// an argument type with an alignment greater than the slot size |
| 264 | /// will be emitted on a higher-alignment address, potentially |
| 265 | /// leaving one or more empty slots behind as padding. If this |
| 266 | /// is false, the returned address might be less-aligned than |
| 267 | /// DirectAlign. |
| 268 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 269 | Address VAListAddr, |
| 270 | llvm::Type *DirectTy, |
| 271 | CharUnits DirectSize, |
| 272 | CharUnits DirectAlign, |
| 273 | CharUnits SlotSize, |
| 274 | bool AllowHigherAlign) { |
| 275 | // Cast the element type to i8* if necessary. Some platforms define |
| 276 | // va_list as a struct containing an i8* instead of just an i8*. |
| 277 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 278 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 279 | |
| 280 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 281 | |
| 282 | // If the CC aligns values higher than the slot size, do so if needed. |
| 283 | Address Addr = Address::invalid(); |
| 284 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 285 | Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), |
| 286 | DirectAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 287 | } else { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 288 | Addr = Address(Ptr, SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // Advance the pointer past the argument, then store that back. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 292 | CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 293 | llvm::Value *NextPtr = |
| 294 | CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, |
| 295 | "argp.next"); |
| 296 | CGF.Builder.CreateStore(NextPtr, VAListAddr); |
| 297 | |
| 298 | // If the argument is smaller than a slot, and this is a big-endian |
| 299 | // target, the argument will be right-adjusted in its slot. |
Strahinja Petrovic | 515a1eb | 2016-06-24 12:12:41 +0000 | [diff] [blame] | 300 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() && |
| 301 | !DirectTy->isStructTy()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 302 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 303 | } |
| 304 | |
| 305 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 306 | return Addr; |
| 307 | } |
| 308 | |
| 309 | /// Emit va_arg for a platform using the common void* representation, |
| 310 | /// where arguments are simply emitted in an array of slots on the stack. |
| 311 | /// |
| 312 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 313 | /// \param ValueInfo - The size and alignment of this type, generally |
| 314 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 315 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 316 | /// Each argument will be allocated to a multiple of this number of |
| 317 | /// slots, and all the slots will be aligned to this value. |
| 318 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 319 | /// an argument type with an alignment greater than the slot size |
| 320 | /// will be emitted on a higher-alignment address, potentially |
| 321 | /// leaving one or more empty slots behind as padding. |
| 322 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 323 | QualType ValueTy, bool IsIndirect, |
| 324 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 325 | CharUnits SlotSizeAndAlign, |
| 326 | bool AllowHigherAlign) { |
| 327 | // The size and alignment of the value that was passed directly. |
| 328 | CharUnits DirectSize, DirectAlign; |
| 329 | if (IsIndirect) { |
| 330 | DirectSize = CGF.getPointerSize(); |
| 331 | DirectAlign = CGF.getPointerAlign(); |
| 332 | } else { |
| 333 | DirectSize = ValueInfo.first; |
| 334 | DirectAlign = ValueInfo.second; |
| 335 | } |
| 336 | |
| 337 | // Cast the address we've calculated to the right type. |
| 338 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 339 | if (IsIndirect) |
| 340 | DirectTy = DirectTy->getPointerTo(0); |
| 341 | |
| 342 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 343 | DirectSize, DirectAlign, |
| 344 | SlotSizeAndAlign, |
| 345 | AllowHigherAlign); |
| 346 | |
| 347 | if (IsIndirect) { |
| 348 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 349 | } |
| 350 | |
| 351 | return Addr; |
| 352 | |
| 353 | } |
| 354 | |
| 355 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 356 | Address Addr1, llvm::BasicBlock *Block1, |
| 357 | Address Addr2, llvm::BasicBlock *Block2, |
| 358 | const llvm::Twine &Name = "") { |
| 359 | assert(Addr1.getType() == Addr2.getType()); |
| 360 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 361 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 362 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 363 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 364 | return Address(PHI, Align); |
| 365 | } |
| 366 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 367 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 368 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 369 | // If someone can figure out a general rule for this, that would be great. |
| 370 | // It's probably just doomed to be platform-dependent, though. |
| 371 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 372 | // Verified for: |
| 373 | // x86-64 FreeBSD, Linux, Darwin |
| 374 | // x86-32 FreeBSD, Linux, Darwin |
| 375 | // PowerPC Linux, Darwin |
| 376 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 377 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 378 | return 32; |
| 379 | } |
| 380 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 381 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 382 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 383 | // The following conventions are known to require this to be false: |
| 384 | // x86_stdcall |
| 385 | // MIPS |
| 386 | // For everything else, we just prefer false unless we opt out. |
| 387 | return false; |
| 388 | } |
| 389 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 390 | void |
| 391 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 392 | llvm::SmallString<24> &Opt) const { |
| 393 | // This assumes the user is passing a library name like "rt" instead of a |
| 394 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 395 | // dynamic. |
| 396 | Opt = "-l"; |
| 397 | Opt += Lib; |
| 398 | } |
| 399 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 400 | unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 401 | return llvm::CallingConv::C; |
| 402 | } |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 403 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 404 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 405 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 406 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 407 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 408 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 409 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 410 | if (FD->isUnnamedBitfield()) |
| 411 | return true; |
| 412 | |
| 413 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 414 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 415 | // Constant arrays of empty records count as empty, strip them off. |
| 416 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 417 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 418 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 419 | if (AT->getSize() == 0) |
| 420 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 421 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 422 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 423 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 424 | const RecordType *RT = FT->getAs<RecordType>(); |
| 425 | if (!RT) |
| 426 | return false; |
| 427 | |
| 428 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 429 | // |
| 430 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 431 | // current ABI. |
| 432 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 433 | return false; |
| 434 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 435 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 438 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 439 | /// fields. Note that a structure with a flexible array member is not |
| 440 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 441 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 442 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 443 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 444 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 445 | const RecordDecl *RD = RT->getDecl(); |
| 446 | if (RD->hasFlexibleArrayMember()) |
| 447 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 448 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 449 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 450 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 451 | for (const auto &I : CXXRD->bases()) |
| 452 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 453 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 454 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 455 | for (const auto *I : RD->fields()) |
| 456 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 457 | return false; |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | /// isSingleElementStruct - Determine if a structure is a "single |
| 462 | /// element struct", i.e. it has exactly one non-empty field or |
| 463 | /// exactly one field which is itself a single element |
| 464 | /// struct. Structures with flexible array members are never |
| 465 | /// considered single element structs. |
| 466 | /// |
| 467 | /// \return The field declaration for the single non-empty field, if |
| 468 | /// it exists. |
| 469 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 470 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 471 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 472 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 473 | |
| 474 | const RecordDecl *RD = RT->getDecl(); |
| 475 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 476 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 477 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 478 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 479 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 480 | // If this is a C++ record, check the bases first. |
| 481 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 482 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 483 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 484 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 485 | continue; |
| 486 | |
| 487 | // If we already found an element then this isn't a single-element struct. |
| 488 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 489 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 490 | |
| 491 | // If this is non-empty and not a single element struct, the composite |
| 492 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 493 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 494 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 495 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 500 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 501 | QualType FT = FD->getType(); |
| 502 | |
| 503 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 504 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 505 | continue; |
| 506 | |
| 507 | // If we already found an element then this isn't a single-element |
| 508 | // struct. |
| 509 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 510 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 511 | |
| 512 | // Treat single element arrays as the element. |
| 513 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 514 | if (AT->getSize().getZExtValue() != 1) |
| 515 | break; |
| 516 | FT = AT->getElementType(); |
| 517 | } |
| 518 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 519 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 520 | Found = FT.getTypePtr(); |
| 521 | } else { |
| 522 | Found = isSingleElementStruct(FT, Context); |
| 523 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 524 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 528 | // We don't consider a struct a single-element struct if it has |
| 529 | // padding beyond the element type. |
| 530 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 531 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 532 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 533 | return Found; |
| 534 | } |
| 535 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 536 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 537 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 538 | const ABIArgInfo &AI) { |
| 539 | // This default implementation defers to the llvm backend's va_arg |
| 540 | // instruction. It can handle only passing arguments directly |
| 541 | // (typically only handled in the backend for primitive types), or |
| 542 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 543 | // flag has ABI impact in the callee, this implementation cannot |
| 544 | // work.) |
| 545 | |
| 546 | // Only a few cases are covered here at the moment -- those needed |
| 547 | // by the default abi. |
| 548 | llvm::Value *Val; |
| 549 | |
| 550 | if (AI.isIndirect()) { |
| 551 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 552 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 553 | assert( |
| 554 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 555 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 556 | |
| 557 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 558 | CharUnits TyAlignForABI = TyInfo.second; |
| 559 | |
| 560 | llvm::Type *BaseTy = |
| 561 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 562 | llvm::Value *Addr = |
| 563 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 564 | return Address(Addr, TyAlignForABI); |
| 565 | } else { |
| 566 | assert((AI.isDirect() || AI.isExtend()) && |
| 567 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 568 | |
| 569 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 570 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 571 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 572 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 573 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 574 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 575 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 576 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 577 | |
| 578 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 579 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 580 | CGF.Builder.CreateStore(Val, Temp); |
| 581 | return Temp; |
| 582 | } |
| 583 | } |
| 584 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 585 | /// DefaultABIInfo - The default implementation for ABI specific |
| 586 | /// details. This implementation provides information which results in |
| 587 | /// self-consistent and sensible LLVM IR generation, but does not |
| 588 | /// conform to any particular ABI. |
| 589 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 590 | public: |
| 591 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 593 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 594 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 595 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 596 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 597 | if (!getCXXABI().classifyReturnType(FI)) |
| 598 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 599 | for (auto &I : FI.arguments()) |
| 600 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 601 | } |
| 602 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 603 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 604 | QualType Ty) const override { |
| 605 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 606 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 607 | }; |
| 608 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 609 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 610 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 611 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 612 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 613 | }; |
| 614 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 615 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 616 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 617 | |
| 618 | if (isAggregateTypeForABI(Ty)) { |
| 619 | // Records with non-trivial destructors/copy-constructors should not be |
| 620 | // passed by value. |
| 621 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 622 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 623 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 624 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 625 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 627 | // Treat an enum type as its underlying type. |
| 628 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 629 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 631 | return (Ty->isPromotableIntegerType() ? |
| 632 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 635 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 636 | if (RetTy->isVoidType()) |
| 637 | return ABIArgInfo::getIgnore(); |
| 638 | |
| 639 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 640 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 641 | |
| 642 | // Treat an enum type as its underlying type. |
| 643 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 644 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 645 | |
| 646 | return (RetTy->isPromotableIntegerType() ? |
| 647 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 648 | } |
| 649 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 650 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 651 | // WebAssembly ABI Implementation |
| 652 | // |
| 653 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 654 | //===----------------------------------------------------------------------===// |
| 655 | |
| 656 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 657 | public: |
| 658 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 659 | : DefaultABIInfo(CGT) {} |
| 660 | |
| 661 | private: |
| 662 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 663 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 664 | |
| 665 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 666 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 667 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 668 | void computeInfo(CGFunctionInfo &FI) const override { |
| 669 | if (!getCXXABI().classifyReturnType(FI)) |
| 670 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 671 | for (auto &Arg : FI.arguments()) |
| 672 | Arg.info = classifyArgumentType(Arg.type); |
| 673 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 674 | |
| 675 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 676 | QualType Ty) const override; |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 677 | }; |
| 678 | |
| 679 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 680 | public: |
| 681 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 682 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 683 | }; |
| 684 | |
| 685 | /// \brief Classify argument of given type \p Ty. |
| 686 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 687 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 688 | |
| 689 | if (isAggregateTypeForABI(Ty)) { |
| 690 | // Records with non-trivial destructors/copy-constructors should not be |
| 691 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 692 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 693 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 694 | // Ignore empty structs/unions. |
| 695 | if (isEmptyRecord(getContext(), Ty, true)) |
| 696 | return ABIArgInfo::getIgnore(); |
| 697 | // Lower single-element structs to just pass a regular value. TODO: We |
| 698 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 699 | // though watch out for things like bitfields. |
| 700 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 701 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | // Otherwise just do the default thing. |
| 705 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 706 | } |
| 707 | |
| 708 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 709 | if (isAggregateTypeForABI(RetTy)) { |
| 710 | // Records with non-trivial destructors/copy-constructors should not be |
| 711 | // returned by value. |
| 712 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 713 | // Ignore empty structs/unions. |
| 714 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 715 | return ABIArgInfo::getIgnore(); |
| 716 | // Lower single-element structs to just return a regular value. TODO: We |
| 717 | // could do reasonable-size multiple-element structs too, using |
| 718 | // ABIArgInfo::getDirect(). |
| 719 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 720 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | // Otherwise just do the default thing. |
| 725 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 726 | } |
| 727 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 728 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 729 | QualType Ty) const { |
| 730 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 731 | getContext().getTypeInfoInChars(Ty), |
| 732 | CharUnits::fromQuantity(4), |
| 733 | /*AllowHigherAlign=*/ true); |
| 734 | } |
| 735 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 736 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 737 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 738 | // |
| 739 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 740 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 741 | //===----------------------------------------------------------------------===// |
| 742 | |
| 743 | class PNaClABIInfo : public ABIInfo { |
| 744 | public: |
| 745 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 746 | |
| 747 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 748 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 749 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 750 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 751 | Address EmitVAArg(CodeGenFunction &CGF, |
| 752 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 753 | }; |
| 754 | |
| 755 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 756 | public: |
| 757 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 758 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 759 | }; |
| 760 | |
| 761 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 762 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 763 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 764 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 765 | for (auto &I : FI.arguments()) |
| 766 | I.info = classifyArgumentType(I.type); |
| 767 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 768 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 769 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 770 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 771 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 772 | // function classification. Structs get passed directly for varargs |
| 773 | // functions, through a rewriting transform in |
| 774 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 775 | // this target to actually support a va_arg instructions with an |
| 776 | // aggregate type, unlike other targets. |
| 777 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 780 | /// \brief Classify argument of given type \p Ty. |
| 781 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 782 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 783 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 784 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 785 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 786 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 787 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 788 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 789 | } else if (Ty->isFloatingType()) { |
| 790 | // Floating-point types don't go inreg. |
| 791 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 792 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 793 | |
| 794 | return (Ty->isPromotableIntegerType() ? |
| 795 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 799 | if (RetTy->isVoidType()) |
| 800 | return ABIArgInfo::getIgnore(); |
| 801 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 802 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 803 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 804 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 805 | |
| 806 | // Treat an enum type as its underlying type. |
| 807 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 808 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 809 | |
| 810 | return (RetTy->isPromotableIntegerType() ? |
| 811 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 812 | } |
| 813 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 814 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 815 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 816 | // 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] | 817 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 818 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 819 | IRType->getScalarSizeInBits() != 64; |
| 820 | } |
| 821 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 822 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 823 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 824 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 825 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 826 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 827 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 828 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 831 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 835 | return Ty; |
| 836 | } |
| 837 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 838 | /// Returns true if this type can be passed in SSE registers with the |
| 839 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 840 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 841 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 842 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 843 | return true; |
| 844 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 845 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 846 | // registers specially. |
| 847 | unsigned VecSize = Context.getTypeSize(VT); |
| 848 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 849 | return true; |
| 850 | } |
| 851 | return false; |
| 852 | } |
| 853 | |
| 854 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 855 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 856 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 857 | return NumMembers <= 4; |
| 858 | } |
| 859 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 860 | //===----------------------------------------------------------------------===// |
| 861 | // X86-32 ABI Implementation |
| 862 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 863 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 864 | /// \brief Similar to llvm::CCState, but for Clang. |
| 865 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 866 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 867 | |
| 868 | unsigned CC; |
| 869 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 870 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 871 | }; |
| 872 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 873 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 874 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 875 | enum Class { |
| 876 | Integer, |
| 877 | Float |
| 878 | }; |
| 879 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 880 | static const unsigned MinABIStackAlignInBytes = 4; |
| 881 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 882 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 883 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 884 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 885 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 886 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 887 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 888 | |
| 889 | static bool isRegisterSize(unsigned Size) { |
| 890 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 891 | } |
| 892 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 893 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 894 | // FIXME: Assumes vectorcall is in use. |
| 895 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 896 | } |
| 897 | |
| 898 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 899 | uint64_t NumMembers) const override { |
| 900 | // FIXME: Assumes vectorcall is in use. |
| 901 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 902 | } |
| 903 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 904 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 905 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 906 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 907 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 908 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 909 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 910 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 911 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 912 | /// \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] | 913 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 914 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 915 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 916 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 917 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 918 | /// \brief Updates the number of available free registers, returns |
| 919 | /// true if any registers were allocated. |
| 920 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 921 | |
| 922 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 923 | bool &NeedsPadding) const; |
| 924 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 925 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 926 | bool canExpandIndirectArgument(QualType Ty) const; |
| 927 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 928 | /// \brief Rewrite the function info so that all memory arguments use |
| 929 | /// inalloca. |
| 930 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 931 | |
| 932 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 933 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 934 | QualType Type) const; |
| 935 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 936 | public: |
| 937 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 938 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 939 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 940 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 941 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 942 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 943 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 944 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 945 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 946 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 947 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 948 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 949 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 950 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 951 | |
| 952 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 953 | ArrayRef<llvm::Type*> scalars, |
| 954 | bool asReturnValue) const override { |
| 955 | // LLVM's x86-32 lowering currently only assigns up to three |
| 956 | // integer registers and three fp registers. Oddly, it'll use up to |
| 957 | // four vector registers for vectors, but those can overlap with the |
| 958 | // scalar registers. |
| 959 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
| 960 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 961 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 962 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 963 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 964 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 965 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 966 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 967 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 968 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 969 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 970 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 971 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 972 | static bool isStructReturnInRegABI( |
| 973 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 974 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 975 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 976 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 977 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 978 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 979 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 980 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 981 | return 4; |
| 982 | } |
| 983 | |
| 984 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 985 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 986 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 987 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 988 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 989 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 990 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 991 | } |
| 992 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 993 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 994 | std::string &Constraints, |
| 995 | std::vector<llvm::Type *> &ResultRegTypes, |
| 996 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 997 | std::vector<LValue> &ResultRegDests, |
| 998 | std::string &AsmString, |
| 999 | unsigned NumOutputs) const override; |
| 1000 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1001 | llvm::Constant * |
| 1002 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1003 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1004 | (0x06 << 8) | // .+0x08 |
| 1005 | ('F' << 16) | |
| 1006 | ('T' << 24); |
| 1007 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1008 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1009 | |
| 1010 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1011 | return "movl\t%ebp, %ebp" |
| 1012 | "\t\t## marker for objc_retainAutoreleaseReturnValue"; |
| 1013 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1014 | }; |
| 1015 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1016 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1017 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1018 | /// Rewrite input constraint references after adding some output constraints. |
| 1019 | /// In the case where there is one output and one input and we add one output, |
| 1020 | /// we need to replace all operand references greater than or equal to 1: |
| 1021 | /// mov $0, $1 |
| 1022 | /// mov eax, $1 |
| 1023 | /// The result will be: |
| 1024 | /// mov $0, $2 |
| 1025 | /// mov eax, $2 |
| 1026 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1027 | unsigned NumNewOuts, |
| 1028 | std::string &AsmString) { |
| 1029 | std::string Buf; |
| 1030 | llvm::raw_string_ostream OS(Buf); |
| 1031 | size_t Pos = 0; |
| 1032 | while (Pos < AsmString.size()) { |
| 1033 | size_t DollarStart = AsmString.find('$', Pos); |
| 1034 | if (DollarStart == std::string::npos) |
| 1035 | DollarStart = AsmString.size(); |
| 1036 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1037 | if (DollarEnd == std::string::npos) |
| 1038 | DollarEnd = AsmString.size(); |
| 1039 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1040 | Pos = DollarEnd; |
| 1041 | size_t NumDollars = DollarEnd - DollarStart; |
| 1042 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1043 | // We have an operand reference. |
| 1044 | size_t DigitStart = Pos; |
| 1045 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1046 | if (DigitEnd == std::string::npos) |
| 1047 | DigitEnd = AsmString.size(); |
| 1048 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1049 | unsigned OperandIndex; |
| 1050 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1051 | if (OperandIndex >= FirstIn) |
| 1052 | OperandIndex += NumNewOuts; |
| 1053 | OS << OperandIndex; |
| 1054 | } else { |
| 1055 | OS << OperandStr; |
| 1056 | } |
| 1057 | Pos = DigitEnd; |
| 1058 | } |
| 1059 | } |
| 1060 | AsmString = std::move(OS.str()); |
| 1061 | } |
| 1062 | |
| 1063 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1064 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1065 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1066 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1067 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1068 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1069 | unsigned NumOutputs) const { |
| 1070 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1071 | |
| 1072 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1073 | // larger. |
| 1074 | if (!Constraints.empty()) |
| 1075 | Constraints += ','; |
| 1076 | if (RetWidth <= 32) { |
| 1077 | Constraints += "={eax}"; |
| 1078 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1079 | } else { |
| 1080 | // Use the 'A' constraint for EAX:EDX. |
| 1081 | Constraints += "=A"; |
| 1082 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1083 | } |
| 1084 | |
| 1085 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1086 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1087 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1088 | |
| 1089 | // Coerce the integer by bitcasting the return slot pointer. |
| 1090 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1091 | CoerceTy->getPointerTo())); |
| 1092 | ResultRegDests.push_back(ReturnSlot); |
| 1093 | |
| 1094 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1095 | } |
| 1096 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1097 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1098 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1099 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1100 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1101 | uint64_t Size = Context.getTypeSize(Ty); |
| 1102 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1103 | // For i386, type must be register sized. |
| 1104 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1105 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1106 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (Ty->isVectorType()) { |
| 1109 | // 64- and 128- bit vectors inside structures are not returned in |
| 1110 | // registers. |
| 1111 | if (Size == 64 || Size == 128) |
| 1112 | return false; |
| 1113 | |
| 1114 | return true; |
| 1115 | } |
| 1116 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1117 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1118 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1119 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1120 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1121 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1122 | return true; |
| 1123 | |
| 1124 | // Arrays are treated like records. |
| 1125 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1126 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1127 | |
| 1128 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1129 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1130 | if (!RT) return false; |
| 1131 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1132 | // FIXME: Traverse bases here too. |
| 1133 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1134 | // Structure types are passed in register if all fields would be |
| 1135 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1136 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1137 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1138 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1139 | continue; |
| 1140 | |
| 1141 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1142 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1143 | return false; |
| 1144 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1145 | return true; |
| 1146 | } |
| 1147 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1148 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1149 | // Treat complex types as the element type. |
| 1150 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1151 | Ty = CTy->getElementType(); |
| 1152 | |
| 1153 | // Check for a type which we know has a simple scalar argument-passing |
| 1154 | // convention without any padding. (We're specifically looking for 32 |
| 1155 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1156 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1157 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1158 | return false; |
| 1159 | |
| 1160 | uint64_t Size = Context.getTypeSize(Ty); |
| 1161 | return Size == 32 || Size == 64; |
| 1162 | } |
| 1163 | |
| 1164 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1165 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1166 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1167 | /// optimizations. |
| 1168 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1169 | // We can only expand structure types. |
| 1170 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1171 | if (!RT) |
| 1172 | return false; |
| 1173 | const RecordDecl *RD = RT->getDecl(); |
| 1174 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1175 | if (!IsWin32StructABI ) { |
| 1176 | // On non-Windows, we have to conservatively match our old bitcode |
| 1177 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1178 | if (!CXXRD->isCLike()) |
| 1179 | return false; |
| 1180 | } else { |
| 1181 | // Don't do this for dynamic classes. |
| 1182 | if (CXXRD->isDynamicClass()) |
| 1183 | return false; |
| 1184 | // Don't do this if there are any non-empty bases. |
| 1185 | for (const CXXBaseSpecifier &Base : CXXRD->bases()) { |
| 1186 | if (!isEmptyRecord(getContext(), Base.getType(), /*AllowArrays=*/true)) |
| 1187 | return false; |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | uint64_t Size = 0; |
| 1193 | |
| 1194 | for (const auto *FD : RD->fields()) { |
| 1195 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1196 | // argument is smaller than 32-bits, expanding the struct will create |
| 1197 | // alignment padding. |
| 1198 | if (!is32Or64BitBasicType(FD->getType(), getContext())) |
| 1199 | return false; |
| 1200 | |
| 1201 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1202 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1203 | // counts as "basic" is more complicated than what we were doing previously. |
| 1204 | if (FD->isBitField()) |
| 1205 | return false; |
| 1206 | |
| 1207 | Size += getContext().getTypeSize(FD->getType()); |
| 1208 | } |
| 1209 | |
| 1210 | // We can do this if there was no alignment padding. |
| 1211 | return Size == getContext().getTypeSize(Ty); |
| 1212 | } |
| 1213 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1214 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1215 | // If the return value is indirect, then the hidden argument is consuming one |
| 1216 | // integer register. |
| 1217 | if (State.FreeRegs) { |
| 1218 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1219 | if (!IsMCUABI) |
| 1220 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1221 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1222 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1225 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1226 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1227 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1228 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1229 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1230 | const Type *Base = nullptr; |
| 1231 | uint64_t NumElts = 0; |
| 1232 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1233 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1234 | // The LLVM struct type for such an aggregate should lower properly. |
| 1235 | return ABIArgInfo::getDirect(); |
| 1236 | } |
| 1237 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1238 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1239 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1240 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1241 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1242 | |
| 1243 | // 128-bit vectors are a special case; they are returned in |
| 1244 | // registers and we need to make sure to pick a type the LLVM |
| 1245 | // backend will like. |
| 1246 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1247 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1248 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1249 | |
| 1250 | // Always return in register if it fits in a general purpose |
| 1251 | // register, or if it is 64 bits and has a single element. |
| 1252 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1253 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1254 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1255 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1256 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1257 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1261 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1262 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1263 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1264 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1265 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1266 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1267 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1268 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1269 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1270 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1271 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1272 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1273 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1274 | // Ignore empty structs/unions. |
| 1275 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1276 | return ABIArgInfo::getIgnore(); |
| 1277 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1278 | // Small structures which are register sized are generally returned |
| 1279 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1280 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1281 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1282 | |
| 1283 | // As a special-case, if the struct is a "single-element" struct, and |
| 1284 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1285 | // floating-point register. (MSVC does not apply this special case.) |
| 1286 | // We apply a similar transformation for pointer types to improve the |
| 1287 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1288 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1289 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1290 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1291 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1292 | |
| 1293 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1294 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1295 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1298 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1299 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1300 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1301 | // Treat an enum type as its underlying type. |
| 1302 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1303 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1304 | |
| 1305 | return (RetTy->isPromotableIntegerType() ? |
| 1306 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1309 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1310 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1311 | } |
| 1312 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1313 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1314 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1315 | if (!RT) |
| 1316 | return 0; |
| 1317 | const RecordDecl *RD = RT->getDecl(); |
| 1318 | |
| 1319 | // If this is a C++ record, check the bases first. |
| 1320 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1321 | for (const auto &I : CXXRD->bases()) |
| 1322 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1323 | return false; |
| 1324 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1325 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1326 | QualType FT = i->getType(); |
| 1327 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1328 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1329 | return true; |
| 1330 | |
| 1331 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1332 | return true; |
| 1333 | } |
| 1334 | |
| 1335 | return false; |
| 1336 | } |
| 1337 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1338 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1339 | unsigned Align) const { |
| 1340 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1341 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1342 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1343 | return 0; // Use default alignment. |
| 1344 | |
| 1345 | // On non-Darwin, the stack type alignment is always 4. |
| 1346 | if (!IsDarwinVectorABI) { |
| 1347 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1348 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1349 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1350 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1351 | // 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] | 1352 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1353 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1354 | return 16; |
| 1355 | |
| 1356 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1359 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1360 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1361 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1362 | if (State.FreeRegs) { |
| 1363 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1364 | if (!IsMCUABI) |
| 1365 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1366 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1367 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1368 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1369 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1370 | // Compute the byval alignment. |
| 1371 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1372 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1373 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1374 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1375 | |
| 1376 | // If the stack alignment is less than the type alignment, realign the |
| 1377 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1378 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1379 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1380 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1383 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1384 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1385 | if (!T) |
| 1386 | T = Ty.getTypePtr(); |
| 1387 | |
| 1388 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1389 | BuiltinType::Kind K = BT->getKind(); |
| 1390 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1391 | return Float; |
| 1392 | } |
| 1393 | return Integer; |
| 1394 | } |
| 1395 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1396 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1397 | if (!IsSoftFloatABI) { |
| 1398 | Class C = classify(Ty); |
| 1399 | if (C == Float) |
| 1400 | return false; |
| 1401 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1402 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1403 | unsigned Size = getContext().getTypeSize(Ty); |
| 1404 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1405 | |
| 1406 | if (SizeInRegs == 0) |
| 1407 | return false; |
| 1408 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1409 | if (!IsMCUABI) { |
| 1410 | if (SizeInRegs > State.FreeRegs) { |
| 1411 | State.FreeRegs = 0; |
| 1412 | return false; |
| 1413 | } |
| 1414 | } else { |
| 1415 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1416 | // earlier parameters that are passed on the stack. Also, |
| 1417 | // it does not allow passing >8-byte structs in-register, |
| 1418 | // even if there are 3 free registers available. |
| 1419 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1420 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1421 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1422 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1423 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1424 | return true; |
| 1425 | } |
| 1426 | |
| 1427 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
| 1428 | bool &InReg, |
| 1429 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1430 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1431 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1432 | // (HFAs) have already been dealt with at this point. |
| 1433 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1434 | return false; |
| 1435 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1436 | NeedsPadding = false; |
| 1437 | InReg = !IsMCUABI; |
| 1438 | |
| 1439 | if (!updateFreeRegs(Ty, State)) |
| 1440 | return false; |
| 1441 | |
| 1442 | if (IsMCUABI) |
| 1443 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1444 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1445 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1446 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1447 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1448 | NeedsPadding = true; |
| 1449 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1450 | return false; |
| 1451 | } |
| 1452 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1453 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1456 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1457 | if (!updateFreeRegs(Ty, State)) |
| 1458 | return false; |
| 1459 | |
| 1460 | if (IsMCUABI) |
| 1461 | return false; |
| 1462 | |
| 1463 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1464 | State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1465 | if (getContext().getTypeSize(Ty) > 32) |
| 1466 | return false; |
| 1467 | |
| 1468 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
| 1469 | Ty->isReferenceType()); |
| 1470 | } |
| 1471 | |
| 1472 | return true; |
| 1473 | } |
| 1474 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1475 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1476 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1477 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1478 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1479 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1480 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1481 | // Check with the C++ ABI first. |
| 1482 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1483 | if (RT) { |
| 1484 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1485 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1486 | return getIndirectResult(Ty, false, State); |
| 1487 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1488 | // The field index doesn't matter, we'll fix it up later. |
| 1489 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1494 | // to other targets. |
| 1495 | const Type *Base = nullptr; |
| 1496 | uint64_t NumElts = 0; |
| 1497 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1498 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1499 | if (State.FreeSSERegs >= NumElts) { |
| 1500 | State.FreeSSERegs -= NumElts; |
| 1501 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1502 | return ABIArgInfo::getDirect(); |
| 1503 | return ABIArgInfo::getExpand(); |
| 1504 | } |
| 1505 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1506 | } |
| 1507 | |
| 1508 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1509 | // Structures with flexible arrays are always indirect. |
| 1510 | // FIXME: This should not be byval! |
| 1511 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1512 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1513 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1514 | // Ignore empty structs/unions on non-Windows. |
| 1515 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1516 | return ABIArgInfo::getIgnore(); |
| 1517 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1518 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1519 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1520 | bool NeedsPadding = false; |
| 1521 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1522 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1523 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1524 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1525 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1526 | if (InReg) |
| 1527 | return ABIArgInfo::getDirectInReg(Result); |
| 1528 | else |
| 1529 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1530 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1531 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1532 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1533 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1534 | // of those arguments will match the struct. This is important because the |
| 1535 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1536 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1537 | // Don't do this for the MCU if there are still free integer registers |
| 1538 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1539 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1540 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1541 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1542 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1543 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1544 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1545 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1546 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1549 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1550 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1551 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1552 | if (IsDarwinVectorABI) { |
| 1553 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1554 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1555 | (Size == 64 && VT->getNumElements() == 1)) |
| 1556 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1557 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1558 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1559 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1560 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1561 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1562 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1563 | return ABIArgInfo::getDirect(); |
| 1564 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1565 | |
| 1566 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1567 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1568 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1569 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1570 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1571 | |
| 1572 | if (Ty->isPromotableIntegerType()) { |
| 1573 | if (InReg) |
| 1574 | return ABIArgInfo::getExtendInReg(); |
| 1575 | return ABIArgInfo::getExtend(); |
| 1576 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1577 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1578 | if (InReg) |
| 1579 | return ABIArgInfo::getDirectInReg(); |
| 1580 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1583 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1584 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1585 | if (IsMCUABI) |
| 1586 | State.FreeRegs = 3; |
| 1587 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1588 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1589 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1590 | State.FreeRegs = 2; |
| 1591 | State.FreeSSERegs = 6; |
| 1592 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1593 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1594 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1595 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1596 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1597 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1598 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1599 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1600 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1601 | // return value was sret and put it in a register ourselves if appropriate. |
| 1602 | if (State.FreeRegs) { |
| 1603 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1604 | if (!IsMCUABI) |
| 1605 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1606 | } |
| 1607 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1608 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1609 | // The chain argument effectively gives us another free register. |
| 1610 | if (FI.isChainCall()) |
| 1611 | ++State.FreeRegs; |
| 1612 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1613 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1614 | for (auto &I : FI.arguments()) { |
| 1615 | I.info = classifyArgumentType(I.type, State); |
| 1616 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1620 | // all the memory arguments to use inalloca. |
| 1621 | if (UsedInAlloca) |
| 1622 | rewriteWithInAlloca(FI); |
| 1623 | } |
| 1624 | |
| 1625 | void |
| 1626 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1627 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1628 | QualType Type) const { |
| 1629 | // Arguments are always 4-byte-aligned. |
| 1630 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1631 | |
| 1632 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1633 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1634 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1635 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1636 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1637 | // Insert padding bytes to respect alignment. |
| 1638 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1639 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1640 | if (StackOffset != FieldEnd) { |
| 1641 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1642 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1643 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1644 | FrameFields.push_back(Ty); |
| 1645 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1648 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1649 | // Leave ignored and inreg arguments alone. |
| 1650 | switch (Info.getKind()) { |
| 1651 | case ABIArgInfo::InAlloca: |
| 1652 | return true; |
| 1653 | case ABIArgInfo::Indirect: |
| 1654 | assert(Info.getIndirectByVal()); |
| 1655 | return true; |
| 1656 | case ABIArgInfo::Ignore: |
| 1657 | return false; |
| 1658 | case ABIArgInfo::Direct: |
| 1659 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1660 | if (Info.getInReg()) |
| 1661 | return false; |
| 1662 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1663 | case ABIArgInfo::Expand: |
| 1664 | case ABIArgInfo::CoerceAndExpand: |
| 1665 | // These are aggregate types which are never passed in registers when |
| 1666 | // inalloca is involved. |
| 1667 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1668 | } |
| 1669 | llvm_unreachable("invalid enum"); |
| 1670 | } |
| 1671 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1672 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1673 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1674 | |
| 1675 | // Build a packed struct type for all of the arguments in memory. |
| 1676 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1677 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1678 | // The stack alignment is always 4. |
| 1679 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1680 | |
| 1681 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1682 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1683 | |
| 1684 | // Put 'this' into the struct before 'sret', if necessary. |
| 1685 | bool IsThisCall = |
| 1686 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1687 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1688 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1689 | isArgInAlloca(I->info)) { |
| 1690 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1691 | ++I; |
| 1692 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1693 | |
| 1694 | // 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] | 1695 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1696 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1697 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1698 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1699 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1703 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1704 | ++I; |
| 1705 | |
| 1706 | // Put arguments passed in memory into the struct. |
| 1707 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1708 | if (isArgInAlloca(I->info)) |
| 1709 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1713 | /*isPacked=*/true), |
| 1714 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1717 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1718 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1719 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1720 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1721 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1722 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1723 | // |
| 1724 | // Just messing with TypeInfo like this works because we never pass |
| 1725 | // anything indirectly. |
| 1726 | TypeInfo.second = CharUnits::fromQuantity( |
| 1727 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1728 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1729 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1730 | TypeInfo, CharUnits::fromQuantity(4), |
| 1731 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1734 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1735 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1736 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1737 | |
| 1738 | switch (Opts.getStructReturnConvention()) { |
| 1739 | case CodeGenOptions::SRCK_Default: |
| 1740 | break; |
| 1741 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1742 | return false; |
| 1743 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1744 | return true; |
| 1745 | } |
| 1746 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1747 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1748 | return true; |
| 1749 | |
| 1750 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1751 | case llvm::Triple::DragonFly: |
| 1752 | case llvm::Triple::FreeBSD: |
| 1753 | case llvm::Triple::OpenBSD: |
| 1754 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1755 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1756 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1757 | default: |
| 1758 | return false; |
| 1759 | } |
| 1760 | } |
| 1761 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1762 | void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1763 | llvm::GlobalValue *GV, |
| 1764 | CodeGen::CodeGenModule &CGM) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1765 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1766 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1767 | // Get the LLVM function. |
| 1768 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1769 | |
| 1770 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1771 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1772 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1773 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1774 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1775 | llvm::AttributeSet::FunctionIndex, |
| 1776 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1777 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1778 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1779 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1780 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1781 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1782 | } |
| 1783 | } |
| 1784 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1785 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1786 | CodeGen::CodeGenFunction &CGF, |
| 1787 | llvm::Value *Address) const { |
| 1788 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1789 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1790 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1791 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1792 | // 0-7 are the eight integer registers; the order is different |
| 1793 | // on Darwin (for EH), but the range is the same. |
| 1794 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1795 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1796 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1797 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1798 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1799 | // These have size 16, which is sizeof(long double) on |
| 1800 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1801 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1802 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1803 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1804 | } else { |
| 1805 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1806 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1807 | Builder.CreateAlignedStore( |
| 1808 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1809 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1810 | |
| 1811 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1812 | // These have size 12, which is sizeof(long double) on |
| 1813 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1814 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1815 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1816 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1817 | |
| 1818 | return false; |
| 1819 | } |
| 1820 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1821 | //===----------------------------------------------------------------------===// |
| 1822 | // X86-64 ABI Implementation |
| 1823 | //===----------------------------------------------------------------------===// |
| 1824 | |
| 1825 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1826 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1827 | /// The AVX ABI level for X86 targets. |
| 1828 | enum class X86AVXABILevel { |
| 1829 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1830 | AVX, |
| 1831 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1832 | }; |
| 1833 | |
| 1834 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1835 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1836 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1837 | case X86AVXABILevel::AVX512: |
| 1838 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1839 | case X86AVXABILevel::AVX: |
| 1840 | return 256; |
| 1841 | case X86AVXABILevel::None: |
| 1842 | return 128; |
| 1843 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1844 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1847 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1848 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1849 | enum Class { |
| 1850 | Integer = 0, |
| 1851 | SSE, |
| 1852 | SSEUp, |
| 1853 | X87, |
| 1854 | X87Up, |
| 1855 | ComplexX87, |
| 1856 | NoClass, |
| 1857 | Memory |
| 1858 | }; |
| 1859 | |
| 1860 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1861 | /// |
| 1862 | /// Merge an accumulating classification \arg Accum with a field |
| 1863 | /// classification \arg Field. |
| 1864 | /// |
| 1865 | /// \param Accum - The accumulating classification. This should |
| 1866 | /// always be either NoClass or the result of a previous merge |
| 1867 | /// call. In addition, this should never be Memory (the caller |
| 1868 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1869 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1870 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1871 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1872 | /// |
| 1873 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1874 | /// final MEMORY or SSE classes when necessary. |
| 1875 | /// |
| 1876 | /// \param AggregateSize - The size of the current aggregate in |
| 1877 | /// the classification process. |
| 1878 | /// |
| 1879 | /// \param Lo - The classification for the parts of the type |
| 1880 | /// residing in the low word of the containing object. |
| 1881 | /// |
| 1882 | /// \param Hi - The classification for the parts of the type |
| 1883 | /// residing in the higher words of the containing object. |
| 1884 | /// |
| 1885 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1886 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1887 | /// classify - Determine the x86_64 register classes in which the |
| 1888 | /// given type T should be passed. |
| 1889 | /// |
| 1890 | /// \param Lo - The classification for the parts of the type |
| 1891 | /// residing in the low word of the containing object. |
| 1892 | /// |
| 1893 | /// \param Hi - The classification for the parts of the type |
| 1894 | /// residing in the high word of the containing object. |
| 1895 | /// |
| 1896 | /// \param OffsetBase - The bit offset of this type in the |
| 1897 | /// containing object. Some parameters are classified different |
| 1898 | /// depending on whether they straddle an eightbyte boundary. |
| 1899 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1900 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1901 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1902 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1903 | /// If a word is unused its result will be NoClass; if a type should |
| 1904 | /// be passed in Memory then at least the classification of \arg Lo |
| 1905 | /// will be Memory. |
| 1906 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1907 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1908 | /// |
| 1909 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1910 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1911 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1912 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1913 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1914 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1915 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1916 | unsigned IROffset, QualType SourceTy, |
| 1917 | unsigned SourceOffset) const; |
| 1918 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1919 | unsigned IROffset, QualType SourceTy, |
| 1920 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1921 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1922 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1923 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1924 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1925 | |
| 1926 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1927 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1928 | /// |
| 1929 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1930 | /// available. |
| 1931 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1932 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1933 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1934 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1935 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1936 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1937 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1938 | unsigned &neededSSE, |
| 1939 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1940 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1941 | bool IsIllegalVectorType(QualType Ty) const; |
| 1942 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1943 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1944 | /// unfortunately in ways that were not always consistent with |
| 1945 | /// certain previous compilers. In particular, platforms which |
| 1946 | /// required strict binary compatibility with older versions of GCC |
| 1947 | /// may need to exempt themselves. |
| 1948 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1949 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 1952 | /// GCC classifies <1 x long long> as SSE but compatibility with older clang |
| 1953 | // compilers require us to classify it as INTEGER. |
| 1954 | bool classifyIntegerMMXAsSSE() const { |
| 1955 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 1956 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 1957 | return false; |
| 1958 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 1959 | return false; |
| 1960 | return true; |
| 1961 | } |
| 1962 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1963 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1964 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1965 | // 64-bit hardware. |
| 1966 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1967 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1968 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1969 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1970 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1971 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1972 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1973 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1974 | bool isPassedUsingAVXType(QualType type) const { |
| 1975 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1976 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1977 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1978 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1979 | if (info.isDirect()) { |
| 1980 | llvm::Type *ty = info.getCoerceToType(); |
| 1981 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1982 | return (vectorTy->getBitWidth() > 128); |
| 1983 | } |
| 1984 | return false; |
| 1985 | } |
| 1986 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1987 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1988 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1989 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1990 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 1991 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1992 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1993 | |
| 1994 | bool has64BitPointers() const { |
| 1995 | return Has64BitPointers; |
| 1996 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1997 | |
| 1998 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 1999 | ArrayRef<llvm::Type*> scalars, |
| 2000 | bool asReturnValue) const override { |
| 2001 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2002 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2003 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2004 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2005 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2006 | class WinX86_64ABIInfo : public ABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2007 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2008 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
| 2009 | : ABIInfo(CGT), |
| 2010 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2011 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2012 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2013 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2014 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2015 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2016 | |
| 2017 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2018 | // FIXME: Assumes vectorcall is in use. |
| 2019 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2020 | } |
| 2021 | |
| 2022 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2023 | uint64_t NumMembers) const override { |
| 2024 | // FIXME: Assumes vectorcall is in use. |
| 2025 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2026 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2027 | |
| 2028 | private: |
| 2029 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 2030 | bool IsReturnType) const; |
| 2031 | |
| 2032 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2033 | }; |
| 2034 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2035 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2036 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2037 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2038 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2039 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2040 | const X86_64ABIInfo &getABIInfo() const { |
| 2041 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2042 | } |
| 2043 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2044 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2045 | return 7; |
| 2046 | } |
| 2047 | |
| 2048 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2049 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2050 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2051 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2052 | // 0-15 are the 16 integer registers. |
| 2053 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2054 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2055 | return false; |
| 2056 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2057 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2058 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2059 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2060 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2061 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2062 | } |
| 2063 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2064 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2065 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2066 | // The default CC on x86-64 sets %al to the number of SSA |
| 2067 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2068 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2069 | // that when AVX types are involved: the ABI explicitly states it is |
| 2070 | // undefined, and it doesn't work in practice because of how the ABI |
| 2071 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2072 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2073 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2074 | for (CallArgList::const_iterator |
| 2075 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2076 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2077 | HasAVXType = true; |
| 2078 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2079 | } |
| 2080 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2081 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2082 | if (!HasAVXType) |
| 2083 | return true; |
| 2084 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2085 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2086 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2089 | llvm::Constant * |
| 2090 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2091 | unsigned Sig; |
| 2092 | if (getABIInfo().has64BitPointers()) |
| 2093 | Sig = (0xeb << 0) | // jmp rel8 |
| 2094 | (0x0a << 8) | // .+0x0c |
| 2095 | ('F' << 16) | |
| 2096 | ('T' << 24); |
| 2097 | else |
| 2098 | Sig = (0xeb << 0) | // jmp rel8 |
| 2099 | (0x06 << 8) | // .+0x08 |
| 2100 | ('F' << 16) | |
| 2101 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2102 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2103 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2104 | |
| 2105 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2106 | CodeGen::CodeGenModule &CGM) const override { |
| 2107 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2108 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2109 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2110 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2111 | } |
| 2112 | } |
| 2113 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2114 | }; |
| 2115 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2116 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2117 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2118 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2119 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2120 | |
| 2121 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 2122 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2123 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 2124 | // If the argument contains a space, enclose it in quotes. |
| 2125 | if (Lib.find(" ") != StringRef::npos) |
| 2126 | Opt += "\"" + Lib.str() + "\""; |
| 2127 | else |
| 2128 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2129 | } |
| 2130 | }; |
| 2131 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2132 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2133 | // If the argument does not end in .lib, automatically add the suffix. |
| 2134 | // If the argument contains a space, enclose it in quotes. |
| 2135 | // This matches the behavior of MSVC. |
| 2136 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2137 | std::string ArgStr = Quote ? "\"" : ""; |
| 2138 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 2139 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2140 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2141 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2142 | return ArgStr; |
| 2143 | } |
| 2144 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2145 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2146 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2147 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2148 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2149 | unsigned NumRegisterParameters) |
| 2150 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2151 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2152 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2153 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2154 | CodeGen::CodeGenModule &CGM) const override; |
| 2155 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2156 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2157 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2158 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2159 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2160 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2161 | |
| 2162 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2163 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2164 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2165 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2166 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2167 | }; |
| 2168 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2169 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 2170 | llvm::GlobalValue *GV, |
| 2171 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 2172 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2173 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 2174 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2175 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2176 | Fn->addFnAttr("stack-probe-size", |
| 2177 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2178 | } |
| 2179 | } |
| 2180 | } |
| 2181 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2182 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2183 | llvm::GlobalValue *GV, |
| 2184 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2185 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2186 | |
| 2187 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2188 | } |
| 2189 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2190 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2191 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2192 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2193 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2194 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2195 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2196 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2197 | CodeGen::CodeGenModule &CGM) const override; |
| 2198 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2199 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2200 | return 7; |
| 2201 | } |
| 2202 | |
| 2203 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2204 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2205 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2206 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2207 | // 0-15 are the 16 integer registers. |
| 2208 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2209 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2210 | return false; |
| 2211 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2212 | |
| 2213 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2214 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2215 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2216 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2217 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2218 | |
| 2219 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2220 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2221 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2222 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2223 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2224 | }; |
| 2225 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2226 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2227 | llvm::GlobalValue *GV, |
| 2228 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2229 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2230 | |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2231 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2232 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2233 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2234 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2235 | } |
| 2236 | } |
| 2237 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2238 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2239 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2240 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2241 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2242 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2243 | Class &Hi) const { |
| 2244 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2245 | // |
| 2246 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2247 | // memory. |
| 2248 | // |
| 2249 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2250 | // memory. |
| 2251 | // |
| 2252 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2253 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2254 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2255 | // ABI working for processors that don't support the __m256 type. |
| 2256 | // |
| 2257 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2258 | // |
| 2259 | // Some of these are enforced by the merging logic. Others can arise |
| 2260 | // only with unions; for example: |
| 2261 | // union { _Complex double; unsigned; } |
| 2262 | // |
| 2263 | // Note that clauses (b) and (c) were added in 0.98. |
| 2264 | // |
| 2265 | if (Hi == Memory) |
| 2266 | Lo = Memory; |
| 2267 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2268 | Lo = Memory; |
| 2269 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2270 | Lo = Memory; |
| 2271 | if (Hi == SSEUp && Lo != SSE) |
| 2272 | Hi = SSE; |
| 2273 | } |
| 2274 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2275 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2276 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2277 | // classified recursively so that always two fields are |
| 2278 | // considered. The resulting class is calculated according to |
| 2279 | // the classes of the fields in the eightbyte: |
| 2280 | // |
| 2281 | // (a) If both classes are equal, this is the resulting class. |
| 2282 | // |
| 2283 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2284 | // the other class. |
| 2285 | // |
| 2286 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2287 | // class. |
| 2288 | // |
| 2289 | // (d) If one of the classes is INTEGER, the result is the |
| 2290 | // INTEGER. |
| 2291 | // |
| 2292 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2293 | // MEMORY is used as class. |
| 2294 | // |
| 2295 | // (f) Otherwise class SSE is used. |
| 2296 | |
| 2297 | // Accum should never be memory (we should have returned) or |
| 2298 | // ComplexX87 (because this cannot be passed in a structure). |
| 2299 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2300 | "Invalid accumulated classification during merge."); |
| 2301 | if (Accum == Field || Field == NoClass) |
| 2302 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2303 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2304 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2305 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2306 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2307 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2308 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2309 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2310 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2311 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2312 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2315 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2316 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2317 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2318 | // Class pairs with appropriate constructor methods for the various |
| 2319 | // situations. |
| 2320 | |
| 2321 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2322 | // shouldn't be passed in registers for example, so there is no chance they |
| 2323 | // can straddle an eightbyte. Verify & simplify. |
| 2324 | |
| 2325 | Lo = Hi = NoClass; |
| 2326 | |
| 2327 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2328 | Current = Memory; |
| 2329 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2330 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2331 | BuiltinType::Kind k = BT->getKind(); |
| 2332 | |
| 2333 | if (k == BuiltinType::Void) { |
| 2334 | Current = NoClass; |
| 2335 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2336 | Lo = Integer; |
| 2337 | Hi = Integer; |
| 2338 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2339 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2340 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2341 | Current = SSE; |
| 2342 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2343 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2344 | if (LDF == &llvm::APFloat::IEEEquad) { |
| 2345 | Lo = SSE; |
| 2346 | Hi = SSEUp; |
| 2347 | } else if (LDF == &llvm::APFloat::x87DoubleExtended) { |
| 2348 | Lo = X87; |
| 2349 | Hi = X87Up; |
| 2350 | } else if (LDF == &llvm::APFloat::IEEEdouble) { |
| 2351 | Current = SSE; |
| 2352 | } else |
| 2353 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2354 | } |
| 2355 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2356 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2357 | return; |
| 2358 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2359 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2360 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2361 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2362 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2363 | return; |
| 2364 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2365 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2366 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2367 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2368 | return; |
| 2369 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2370 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2371 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2372 | if (Ty->isMemberFunctionPointerType()) { |
| 2373 | if (Has64BitPointers) { |
| 2374 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2375 | // Lo and Hi now. |
| 2376 | Lo = Hi = Integer; |
| 2377 | } else { |
| 2378 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2379 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2380 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2381 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2382 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2383 | Lo = Hi = Integer; |
| 2384 | } else { |
| 2385 | Current = Integer; |
| 2386 | } |
| 2387 | } |
| 2388 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2389 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2390 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2391 | return; |
| 2392 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2393 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2394 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2395 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2396 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2397 | // gcc passes the following as integer: |
| 2398 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2399 | // 2 bytes - <2 x char>, <1 x short> |
| 2400 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2401 | Current = Integer; |
| 2402 | |
| 2403 | // If this type crosses an eightbyte boundary, it should be |
| 2404 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2405 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2406 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2407 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2408 | Hi = Lo; |
| 2409 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2410 | QualType ElementType = VT->getElementType(); |
| 2411 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2412 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2413 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2414 | return; |
| 2415 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2416 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2417 | // pass them as integer. For platforms where clang is the de facto |
| 2418 | // platform compiler, we must continue to use integer. |
| 2419 | if (!classifyIntegerMMXAsSSE() && |
| 2420 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2421 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2422 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2423 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2424 | Current = Integer; |
| 2425 | else |
| 2426 | Current = SSE; |
| 2427 | |
| 2428 | // If this type crosses an eightbyte boundary, it should be |
| 2429 | // split. |
| 2430 | if (OffsetBase && OffsetBase != 64) |
| 2431 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2432 | } else if (Size == 128 || |
| 2433 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2434 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2435 | // least significant one belongs to class SSE and all the others to class |
| 2436 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2437 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2438 | // This design isn't correct for 256-bits, but since there're no cases |
| 2439 | // where the upper parts would need to be inspected, avoid adding |
| 2440 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2441 | // |
| 2442 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2443 | // registers if they are "named", i.e. not part of the "..." of a |
| 2444 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2445 | // |
| 2446 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2447 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2448 | Lo = SSE; |
| 2449 | Hi = SSEUp; |
| 2450 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2451 | return; |
| 2452 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2453 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2454 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2455 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2456 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2457 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2458 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2459 | if (Size <= 64) |
| 2460 | Current = Integer; |
| 2461 | else if (Size <= 128) |
| 2462 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2463 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2464 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2465 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2466 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2467 | } else if (ET == getContext().LongDoubleTy) { |
| 2468 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2469 | if (LDF == &llvm::APFloat::IEEEquad) |
| 2470 | Current = Memory; |
| 2471 | else if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 2472 | Current = ComplexX87; |
| 2473 | else if (LDF == &llvm::APFloat::IEEEdouble) |
| 2474 | Lo = Hi = SSE; |
| 2475 | else |
| 2476 | llvm_unreachable("unexpected long double representation!"); |
| 2477 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2478 | |
| 2479 | // If this complex type crosses an eightbyte boundary then it |
| 2480 | // should be split. |
| 2481 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2482 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2483 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2484 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2485 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2486 | return; |
| 2487 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2488 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2489 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2490 | // Arrays are treated like structures. |
| 2491 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2492 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2493 | |
| 2494 | // 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] | 2495 | // than eight eightbytes, ..., it has class MEMORY. |
| 2496 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2497 | return; |
| 2498 | |
| 2499 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2500 | // fields, it has class MEMORY. |
| 2501 | // |
| 2502 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2503 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2504 | return; |
| 2505 | |
| 2506 | // Otherwise implement simplified merge. We could be smarter about |
| 2507 | // this, but it isn't worth it and would be harder to verify. |
| 2508 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2509 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2510 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2511 | |
| 2512 | // The only case a 256-bit wide vector could be used is when the array |
| 2513 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2514 | // 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] | 2515 | // |
| 2516 | if (Size > 128 && |
| 2517 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2518 | return; |
| 2519 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2520 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2521 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2522 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2523 | Lo = merge(Lo, FieldLo); |
| 2524 | Hi = merge(Hi, FieldHi); |
| 2525 | if (Lo == Memory || Hi == Memory) |
| 2526 | break; |
| 2527 | } |
| 2528 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2529 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2530 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
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 (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2535 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2536 | |
| 2537 | // 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] | 2538 | // than eight eightbytes, ..., it has class MEMORY. |
| 2539 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2540 | return; |
| 2541 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2542 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2543 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2544 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2545 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2546 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2547 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2548 | const RecordDecl *RD = RT->getDecl(); |
| 2549 | |
| 2550 | // Assume variable sized types are passed in memory. |
| 2551 | if (RD->hasFlexibleArrayMember()) |
| 2552 | return; |
| 2553 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2554 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2555 | |
| 2556 | // Reset Lo class, this will be recomputed. |
| 2557 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2558 | |
| 2559 | // If this is a C++ record, classify the bases first. |
| 2560 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2561 | for (const auto &I : CXXRD->bases()) { |
| 2562 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2563 | "Unexpected base class!"); |
| 2564 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2565 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2566 | |
| 2567 | // Classify this field. |
| 2568 | // |
| 2569 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2570 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2571 | // initialized to class NO_CLASS. |
| 2572 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2573 | uint64_t Offset = |
| 2574 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2575 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2576 | Lo = merge(Lo, FieldLo); |
| 2577 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2578 | if (Lo == Memory || Hi == Memory) { |
| 2579 | postMerge(Size, Lo, Hi); |
| 2580 | return; |
| 2581 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
| 2585 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2586 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2587 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2588 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2589 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2590 | bool BitField = i->isBitField(); |
| 2591 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2592 | // Ignore padding bit-fields. |
| 2593 | if (BitField && i->isUnnamedBitfield()) |
| 2594 | continue; |
| 2595 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2596 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2597 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2598 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2599 | // The only case a 256-bit wide vector could be used is when the struct |
| 2600 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2601 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2602 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2603 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2604 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2605 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2606 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2607 | return; |
| 2608 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2609 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2610 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2611 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2612 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2613 | return; |
| 2614 | } |
| 2615 | |
| 2616 | // Classify this field. |
| 2617 | // |
| 2618 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2619 | // exceeds a single eightbyte, each is classified |
| 2620 | // separately. Each eightbyte gets initialized to class |
| 2621 | // NO_CLASS. |
| 2622 | Class FieldLo, FieldHi; |
| 2623 | |
| 2624 | // Bit-fields require special handling, they do not force the |
| 2625 | // structure to be passed in memory even if unaligned, and |
| 2626 | // therefore they can straddle an eightbyte. |
| 2627 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2628 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2629 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2630 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2631 | |
| 2632 | uint64_t EB_Lo = Offset / 64; |
| 2633 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2634 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2635 | if (EB_Lo) { |
| 2636 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2637 | FieldLo = NoClass; |
| 2638 | FieldHi = Integer; |
| 2639 | } else { |
| 2640 | FieldLo = Integer; |
| 2641 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2642 | } |
| 2643 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2644 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2645 | Lo = merge(Lo, FieldLo); |
| 2646 | Hi = merge(Hi, FieldHi); |
| 2647 | if (Lo == Memory || Hi == Memory) |
| 2648 | break; |
| 2649 | } |
| 2650 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2651 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2652 | } |
| 2653 | } |
| 2654 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2655 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2656 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2657 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2658 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2659 | // Treat an enum type as its underlying type. |
| 2660 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2661 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2662 | |
| 2663 | return (Ty->isPromotableIntegerType() ? |
| 2664 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2665 | } |
| 2666 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2667 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2670 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2671 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2672 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2673 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2674 | if (Size <= 64 || Size > LargestVector) |
| 2675 | return true; |
| 2676 | } |
| 2677 | |
| 2678 | return false; |
| 2679 | } |
| 2680 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2681 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2682 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2683 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2684 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2685 | // |
| 2686 | // This assumption is optimistic, as there could be free registers available |
| 2687 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2688 | // the argument in the free register. This does not seem to happen currently, |
| 2689 | // but this code would be much safer if we could mark the argument with |
| 2690 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2691 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2692 | // Treat an enum type as its underlying type. |
| 2693 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2694 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2695 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2696 | return (Ty->isPromotableIntegerType() ? |
| 2697 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2698 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2699 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2700 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2701 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2702 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2703 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2704 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2705 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2706 | |
| 2707 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2708 | // is important for good codegen. |
| 2709 | // |
| 2710 | // We do this by coercing the value into a scalar type which the backend can |
| 2711 | // handle naturally (i.e., without using byval). |
| 2712 | // |
| 2713 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2714 | // free integer registers. Doing this when there are free integer registers |
| 2715 | // would require more care, as we would have to ensure that the coerced value |
| 2716 | // did not claim the unused register. That would require either reording the |
| 2717 | // arguments to the function (so that any subsequent inreg values came first), |
| 2718 | // or only doing this optimization when there were no following arguments that |
| 2719 | // might be inreg. |
| 2720 | // |
| 2721 | // We currently expect it to be rare (particularly in well written code) for |
| 2722 | // arguments to be passed on the stack when there are still free integer |
| 2723 | // registers available (this would typically imply large structs being passed |
| 2724 | // by value), so this seems like a fair tradeoff for now. |
| 2725 | // |
| 2726 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2727 | // attributes. See PR12193. |
| 2728 | if (freeIntRegs == 0) { |
| 2729 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2730 | |
| 2731 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2732 | // type, which will end up on the stack (with alignment 8). |
| 2733 | if (Align == 8 && Size <= 64) |
| 2734 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2735 | Size)); |
| 2736 | } |
| 2737 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2738 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2741 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2742 | /// 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] | 2743 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2744 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2745 | // vectors; strip them off if present. |
| 2746 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2747 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2748 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2749 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2750 | if (isa<llvm::VectorType>(IRType) || |
| 2751 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2752 | return IRType; |
| 2753 | |
| 2754 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2755 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2756 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2757 | |
| 2758 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2759 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2760 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2763 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2764 | /// is known to either be off the end of the specified type or being in |
| 2765 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2766 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2767 | /// classification that put one of the two halves in the INTEGER class. |
| 2768 | /// |
| 2769 | /// It is conservatively correct to return false. |
| 2770 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2771 | unsigned EndBit, ASTContext &Context) { |
| 2772 | // If the bytes being queried are off the end of the type, there is no user |
| 2773 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2774 | // types that don't contain interesting padding. |
| 2775 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2776 | if (TySize <= StartBit) |
| 2777 | return true; |
| 2778 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2779 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2780 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2781 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2782 | |
| 2783 | // Check each element to see if the element overlaps with the queried range. |
| 2784 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2785 | // If the element is after the span we care about, then we're done.. |
| 2786 | unsigned EltOffset = i*EltSize; |
| 2787 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2788 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2789 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2790 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2791 | EndBit-EltOffset, Context)) |
| 2792 | return false; |
| 2793 | } |
| 2794 | // If it overlaps no elements, then it is safe to process as padding. |
| 2795 | return true; |
| 2796 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2797 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2798 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2799 | const RecordDecl *RD = RT->getDecl(); |
| 2800 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2801 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2802 | // If this is a C++ record, check the bases first. |
| 2803 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2804 | for (const auto &I : CXXRD->bases()) { |
| 2805 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2806 | "Unexpected base class!"); |
| 2807 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2808 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2809 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2810 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2811 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2812 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2813 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2814 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2815 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2816 | EndBit-BaseOffset, Context)) |
| 2817 | return false; |
| 2818 | } |
| 2819 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2820 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2821 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2822 | // this could be sped up a lot by being smarter about queried fields, |
| 2823 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2824 | // much. |
| 2825 | unsigned idx = 0; |
| 2826 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2827 | i != e; ++i, ++idx) { |
| 2828 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2829 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2830 | // If we found a field after the region we care about, then we're done. |
| 2831 | if (FieldOffset >= EndBit) break; |
| 2832 | |
| 2833 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2834 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2835 | Context)) |
| 2836 | return false; |
| 2837 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2838 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2839 | // If nothing in this record overlapped the area of interest, then we're |
| 2840 | // clean. |
| 2841 | return true; |
| 2842 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2843 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2844 | return false; |
| 2845 | } |
| 2846 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2847 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2848 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2849 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2850 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2851 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2852 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2853 | // Base case if we find a float. |
| 2854 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2855 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2856 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2857 | // 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] | 2858 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2859 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2860 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2861 | IROffset -= SL->getElementOffset(Elt); |
| 2862 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2863 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2864 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2865 | // 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] | 2866 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2867 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2868 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2869 | IROffset -= IROffset/EltSize*EltSize; |
| 2870 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2871 | } |
| 2872 | |
| 2873 | return false; |
| 2874 | } |
| 2875 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2876 | |
| 2877 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2878 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2879 | llvm::Type *X86_64ABIInfo:: |
| 2880 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2881 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2882 | // 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] | 2883 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2884 | // structs that contain 3 floats. |
| 2885 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2886 | SourceOffset*8+64, getContext())) |
| 2887 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2888 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2889 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2890 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2891 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2892 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2893 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2894 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2895 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2896 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2897 | } |
| 2898 | |
| 2899 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2900 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2901 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2902 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2903 | /// 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] | 2904 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2905 | /// etc). |
| 2906 | /// |
| 2907 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2908 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2909 | /// the 8-byte value references. PrefType may be null. |
| 2910 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2911 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2912 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2913 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2914 | llvm::Type *X86_64ABIInfo:: |
| 2915 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2916 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2917 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2918 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2919 | if (IROffset == 0) { |
| 2920 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2921 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2922 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2923 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2924 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2925 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2926 | // goodness in the source type is just tail padding. This is allowed to |
| 2927 | // kick in for struct {double,int} on the int, but not on |
| 2928 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2929 | // have to do this analysis on the source type because we can't depend on |
| 2930 | // unions being lowered a specific way etc. |
| 2931 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2932 | IRType->isIntegerTy(32) || |
| 2933 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2934 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2935 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2936 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2937 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2938 | SourceOffset*8+64, getContext())) |
| 2939 | return IRType; |
| 2940 | } |
| 2941 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2942 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2943 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2944 | // 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] | 2945 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2946 | if (IROffset < SL->getSizeInBytes()) { |
| 2947 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2948 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2949 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2950 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2951 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2952 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2953 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2954 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2955 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2956 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2957 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2958 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2959 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2960 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2961 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2962 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2963 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2964 | // 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] | 2965 | unsigned TySizeInBytes = |
| 2966 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2967 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2968 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2969 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2970 | // It is always safe to classify this as an integer type up to i64 that |
| 2971 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2972 | return llvm::IntegerType::get(getVMContext(), |
| 2973 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2976 | |
| 2977 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2978 | /// be used as elements of a two register pair to pass or return, return a |
| 2979 | /// first class aggregate to represent them. For example, if the low part of |
| 2980 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2981 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2982 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2983 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2984 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2985 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2986 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2987 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2988 | // the second element at offset 8. Check for this: |
| 2989 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2990 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 2991 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2992 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2993 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2994 | // To handle this, we have to increase the size of the low part so that the |
| 2995 | // second element will start at an 8 byte offset. We can't increase the size |
| 2996 | // of the second element because it might make us access off the end of the |
| 2997 | // struct. |
| 2998 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 2999 | // There are usually two sorts of types the ABI generation code can produce |
| 3000 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3001 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3002 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3003 | // Promote these to a larger type. |
| 3004 | if (Lo->isFloatTy()) |
| 3005 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3006 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3007 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3008 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3009 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3010 | } |
| 3011 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3012 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3013 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3014 | |
| 3015 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3016 | // Verify that the second element is at an 8-byte offset. |
| 3017 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3018 | "Invalid x86-64 argument pair!"); |
| 3019 | return Result; |
| 3020 | } |
| 3021 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3022 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3023 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3024 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3025 | // classification algorithm. |
| 3026 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3027 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3028 | |
| 3029 | // Check some invariants. |
| 3030 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3031 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3032 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3033 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3034 | switch (Lo) { |
| 3035 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3036 | if (Hi == NoClass) |
| 3037 | return ABIArgInfo::getIgnore(); |
| 3038 | // If the low part is just padding, it takes no register, leave ResType |
| 3039 | // null. |
| 3040 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3041 | "Unknown missing lo part"); |
| 3042 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3043 | |
| 3044 | case SSEUp: |
| 3045 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3046 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3047 | |
| 3048 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3049 | // hidden argument. |
| 3050 | case Memory: |
| 3051 | return getIndirectReturnResult(RetTy); |
| 3052 | |
| 3053 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3054 | // available register of the sequence %rax, %rdx is used. |
| 3055 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3056 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3057 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3058 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3059 | // so that the parameter gets the right LLVM IR attributes. |
| 3060 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3061 | // Treat an enum type as its underlying type. |
| 3062 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3063 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3064 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3065 | if (RetTy->isIntegralOrEnumerationType() && |
| 3066 | RetTy->isPromotableIntegerType()) |
| 3067 | return ABIArgInfo::getExtend(); |
| 3068 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3069 | break; |
| 3070 | |
| 3071 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3072 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3073 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3074 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3075 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3076 | |
| 3077 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3078 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3079 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3080 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3081 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3082 | |
| 3083 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3084 | // part of the value is returned in %st0 and the imaginary part in |
| 3085 | // %st1. |
| 3086 | case ComplexX87: |
| 3087 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3088 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3089 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3090 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3091 | break; |
| 3092 | } |
| 3093 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3094 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3095 | switch (Hi) { |
| 3096 | // Memory was handled previously and X87 should |
| 3097 | // never occur as a hi class. |
| 3098 | case Memory: |
| 3099 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3100 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3101 | |
| 3102 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3103 | case NoClass: |
| 3104 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3105 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3106 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3107 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3108 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3109 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3110 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3111 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3112 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3113 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3114 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3115 | break; |
| 3116 | |
| 3117 | // 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] | 3118 | // is passed in the next available eightbyte chunk if the last used |
| 3119 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3120 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3121 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3122 | case SSEUp: |
| 3123 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3124 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3125 | break; |
| 3126 | |
| 3127 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3128 | // returned together with the previous X87 value in %st0. |
| 3129 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3130 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3131 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3132 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3133 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3134 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3135 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3136 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3137 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3138 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3139 | break; |
| 3140 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3141 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3142 | // 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] | 3143 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3144 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3145 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3146 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3147 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3148 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3151 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3152 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3153 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3154 | const |
| 3155 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3156 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3157 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3158 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3159 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3160 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3161 | // Check some invariants. |
| 3162 | // FIXME: Enforce these by construction. |
| 3163 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3164 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3165 | |
| 3166 | neededInt = 0; |
| 3167 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3168 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3169 | switch (Lo) { |
| 3170 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3171 | if (Hi == NoClass) |
| 3172 | return ABIArgInfo::getIgnore(); |
| 3173 | // If the low part is just padding, it takes no register, leave ResType |
| 3174 | // null. |
| 3175 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3176 | "Unknown missing lo part"); |
| 3177 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3178 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3179 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3180 | // on the stack. |
| 3181 | case Memory: |
| 3182 | |
| 3183 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3184 | // COMPLEX_X87, it is passed in memory. |
| 3185 | case X87: |
| 3186 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3187 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3188 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3189 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3190 | |
| 3191 | case SSEUp: |
| 3192 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3193 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3194 | |
| 3195 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3196 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3197 | // and %r9 is used. |
| 3198 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3199 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3200 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3201 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3202 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3203 | |
| 3204 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3205 | // so that the parameter gets the right LLVM IR attributes. |
| 3206 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3207 | // Treat an enum type as its underlying type. |
| 3208 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3209 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3210 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3211 | if (Ty->isIntegralOrEnumerationType() && |
| 3212 | Ty->isPromotableIntegerType()) |
| 3213 | return ABIArgInfo::getExtend(); |
| 3214 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3215 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3216 | break; |
| 3217 | |
| 3218 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3219 | // available SSE register is used, the registers are taken in the |
| 3220 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3221 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3222 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3223 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3224 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3225 | break; |
| 3226 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3227 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3228 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3229 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3230 | switch (Hi) { |
| 3231 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3232 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3233 | // which is passed in memory. |
| 3234 | case Memory: |
| 3235 | case X87: |
| 3236 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3237 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3238 | |
| 3239 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3240 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3241 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3242 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3243 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3244 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3245 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3246 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3247 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3248 | break; |
| 3249 | |
| 3250 | // X87Up generally doesn't occur here (long double is passed in |
| 3251 | // memory), except in situations involving unions. |
| 3252 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3253 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3254 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3255 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3256 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3257 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3258 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3259 | ++neededSSE; |
| 3260 | break; |
| 3261 | |
| 3262 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3263 | // 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] | 3264 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3265 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3266 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3267 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3268 | break; |
| 3269 | } |
| 3270 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3271 | // If a high part was specified, merge it together with the low part. It is |
| 3272 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3273 | // first class struct aggregate with the high and low part: {low, high} |
| 3274 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3275 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3276 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3277 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3280 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3281 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3282 | if (!getCXXABI().classifyReturnType(FI)) |
| 3283 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3284 | |
| 3285 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3286 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3287 | |
| 3288 | // If the return value is indirect, then the hidden argument is consuming one |
| 3289 | // integer register. |
| 3290 | if (FI.getReturnInfo().isIndirect()) |
| 3291 | --freeIntRegs; |
| 3292 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3293 | // The chain argument effectively gives us another free register. |
| 3294 | if (FI.isChainCall()) |
| 3295 | ++freeIntRegs; |
| 3296 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3297 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3298 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3299 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3300 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3301 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3302 | it != ie; ++it, ++ArgNo) { |
| 3303 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3304 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3305 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3306 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3307 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3308 | |
| 3309 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3310 | // eightbyte of an argument, the whole argument is passed on the |
| 3311 | // stack. If registers have already been assigned for some |
| 3312 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3313 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3314 | freeIntRegs -= neededInt; |
| 3315 | freeSSERegs -= neededSSE; |
| 3316 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3317 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3318 | } |
| 3319 | } |
| 3320 | } |
| 3321 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3322 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3323 | Address VAListAddr, QualType Ty) { |
| 3324 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3325 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3326 | llvm::Value *overflow_arg_area = |
| 3327 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3328 | |
| 3329 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3330 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3331 | // It isn't stated explicitly in the standard, but in practice we use |
| 3332 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3333 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3334 | if (Align > CharUnits::fromQuantity(8)) { |
| 3335 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3336 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | // 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] | 3340 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3341 | llvm::Value *Res = |
| 3342 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3343 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3344 | |
| 3345 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3346 | // l->overflow_arg_area + sizeof(type). |
| 3347 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3348 | // an 8 byte boundary. |
| 3349 | |
| 3350 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3351 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3352 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3353 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3354 | "overflow_arg_area.next"); |
| 3355 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3356 | |
| 3357 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3358 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3361 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3362 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3363 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3364 | // struct { |
| 3365 | // i32 gp_offset; |
| 3366 | // i32 fp_offset; |
| 3367 | // i8* overflow_arg_area; |
| 3368 | // i8* reg_save_area; |
| 3369 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3370 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3371 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3372 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3373 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3374 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3375 | |
| 3376 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3377 | // in the registers. If not go to step 7. |
| 3378 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3379 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3380 | |
| 3381 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3382 | // general purpose registers needed to pass type and num_fp to hold |
| 3383 | // the number of floating point registers needed. |
| 3384 | |
| 3385 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3386 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3387 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3388 | // |
| 3389 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3390 | // register save space). |
| 3391 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3392 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3393 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3394 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3395 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3396 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3397 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3398 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3399 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3400 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3401 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3405 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3406 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3407 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3408 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3409 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3410 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3411 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3412 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3413 | } |
| 3414 | |
| 3415 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3416 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3417 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3418 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3419 | |
| 3420 | // Emit code to load the value if it was passed in registers. |
| 3421 | |
| 3422 | CGF.EmitBlock(InRegBlock); |
| 3423 | |
| 3424 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3425 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3426 | // copying to a temporary location in case the parameter is passed |
| 3427 | // in different register classes or requires an alignment greater |
| 3428 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3429 | // |
| 3430 | // FIXME: This really results in shameful code when we end up needing to |
| 3431 | // collect arguments from different places; often what should result in a |
| 3432 | // simple assembling of a structure from scattered addresses has many more |
| 3433 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3434 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3435 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3436 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3437 | "reg_save_area"); |
| 3438 | |
| 3439 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3440 | if (neededInt && neededSSE) { |
| 3441 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3442 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3443 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3444 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3445 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3446 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3447 | llvm::Type *TyLo = ST->getElementType(0); |
| 3448 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3449 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3450 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3451 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3452 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3453 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3454 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3455 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3456 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3457 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3458 | // Copy the first element. |
| 3459 | llvm::Value *V = |
| 3460 | CGF.Builder.CreateDefaultAlignedLoad( |
| 3461 | CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 3462 | CGF.Builder.CreateStore(V, |
| 3463 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3464 | |
| 3465 | // Copy the second element. |
| 3466 | V = CGF.Builder.CreateDefaultAlignedLoad( |
| 3467 | CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 3468 | CharUnits Offset = CharUnits::fromQuantity( |
| 3469 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3470 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3471 | |
| 3472 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3473 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3474 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3475 | CharUnits::fromQuantity(8)); |
| 3476 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3477 | |
| 3478 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3479 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3480 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3481 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3482 | CharUnits TyAlign = SizeAlign.second; |
| 3483 | |
| 3484 | // Copy into a temporary if the type is more aligned than the |
| 3485 | // register save area. |
| 3486 | if (TyAlign.getQuantity() > 8) { |
| 3487 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3488 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3489 | RegAddr = Tmp; |
| 3490 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3491 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3492 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3493 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3494 | CharUnits::fromQuantity(16)); |
| 3495 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3496 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3497 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3498 | // SSE registers are spaced 16 bytes apart in the register save |
| 3499 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3500 | // The ABI isn't explicit about this, but it seems reasonable |
| 3501 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3502 | // naturally 16-byte aligned and the prologue is expected to store |
| 3503 | // all the SSE registers to the RSA. |
| 3504 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3505 | CharUnits::fromQuantity(16)); |
| 3506 | Address RegAddrHi = |
| 3507 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3508 | CharUnits::fromQuantity(16)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3509 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3510 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3511 | llvm::Value *V; |
| 3512 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3513 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3514 | V = CGF.Builder.CreateLoad( |
| 3515 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3516 | CGF.Builder.CreateStore(V, |
| 3517 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3518 | V = CGF.Builder.CreateLoad( |
| 3519 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3520 | CGF.Builder.CreateStore(V, |
| 3521 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3522 | |
| 3523 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3527 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3528 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3529 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3530 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3531 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3532 | gp_offset_p); |
| 3533 | } |
| 3534 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3535 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3536 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3537 | fp_offset_p); |
| 3538 | } |
| 3539 | CGF.EmitBranch(ContBlock); |
| 3540 | |
| 3541 | // Emit code to load the value if it was passed in memory. |
| 3542 | |
| 3543 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3544 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3545 | |
| 3546 | // Return the appropriate result. |
| 3547 | |
| 3548 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3549 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3550 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3551 | return ResAddr; |
| 3552 | } |
| 3553 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3554 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3555 | QualType Ty) const { |
| 3556 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3557 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3558 | CharUnits::fromQuantity(8), |
| 3559 | /*allowHigherAlign*/ false); |
| 3560 | } |
| 3561 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3562 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 3563 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3564 | |
| 3565 | if (Ty->isVoidType()) |
| 3566 | return ABIArgInfo::getIgnore(); |
| 3567 | |
| 3568 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3569 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3570 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3571 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3572 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3573 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3574 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3575 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3576 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3577 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3578 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3579 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3583 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3584 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3585 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3586 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3587 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3588 | // other targets. |
| 3589 | const Type *Base = nullptr; |
| 3590 | uint64_t NumElts = 0; |
| 3591 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3592 | if (FreeSSERegs >= NumElts) { |
| 3593 | FreeSSERegs -= NumElts; |
| 3594 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3595 | return ABIArgInfo::getDirect(); |
| 3596 | return ABIArgInfo::getExpand(); |
| 3597 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3598 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3602 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3603 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3604 | // directly. |
| 3605 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3606 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3607 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3610 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3611 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3612 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3613 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3614 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3615 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3616 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3617 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3620 | // Bool type is always extended to the ABI, other builtin types are not |
| 3621 | // extended. |
| 3622 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3623 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3624 | return ABIArgInfo::getExtend(); |
| 3625 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3626 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3627 | // passes them indirectly through memory. |
| 3628 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3629 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 3630 | if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 3631 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3632 | } |
| 3633 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3634 | return ABIArgInfo::getDirect(); |
| 3635 | } |
| 3636 | |
| 3637 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3638 | bool IsVectorCall = |
| 3639 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3640 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3641 | // We can use up to 4 SSE return registers with vectorcall. |
| 3642 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3643 | if (!getCXXABI().classifyReturnType(FI)) |
| 3644 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3645 | |
| 3646 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3647 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3648 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3649 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3652 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3653 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 3654 | |
| 3655 | bool IsIndirect = false; |
| 3656 | |
| 3657 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3658 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 3659 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 3660 | uint64_t Width = getContext().getTypeSize(Ty); |
| 3661 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 3662 | } |
| 3663 | |
| 3664 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3665 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3666 | CharUnits::fromQuantity(8), |
| 3667 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3668 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3669 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3670 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3671 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3672 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3673 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3674 | bool IsSoftFloatABI; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3675 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3676 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 3677 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3678 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3679 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3680 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3681 | }; |
| 3682 | |
| 3683 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3684 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3685 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 3686 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3687 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3688 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3689 | // This is recovered from gcc output. |
| 3690 | return 1; // r1 is the dedicated stack pointer |
| 3691 | } |
| 3692 | |
| 3693 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3694 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3695 | }; |
| 3696 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3697 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3698 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 3699 | // TODO: this implementation is now likely redundant with |
| 3700 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3701 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 3702 | QualType Ty) const { |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 3703 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3704 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3705 | // TODO: Implement this. For now ignore. |
| 3706 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 3707 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3710 | // struct __va_list_tag { |
| 3711 | // unsigned char gpr; |
| 3712 | // unsigned char fpr; |
| 3713 | // unsigned short reserved; |
| 3714 | // void *overflow_arg_area; |
| 3715 | // void *reg_save_area; |
| 3716 | // }; |
| 3717 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3718 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3719 | bool isInt = |
| 3720 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3721 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3722 | |
| 3723 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 3724 | // with the argument-lowering code. |
| 3725 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3726 | |
| 3727 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3728 | |
| 3729 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 3730 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3731 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3732 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 3733 | } else { |
| 3734 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3735 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3736 | |
| 3737 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 3738 | |
| 3739 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3740 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3741 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 3742 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 3743 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3744 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3745 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 3746 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3747 | |
| 3748 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3749 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3750 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3751 | |
| 3752 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3753 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3754 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 3755 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3756 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3757 | // Case 1: consume registers. |
| 3758 | Address RegAddr = Address::invalid(); |
| 3759 | { |
| 3760 | CGF.EmitBlock(UsingRegs); |
| 3761 | |
| 3762 | Address RegSaveAreaPtr = |
| 3763 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 3764 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 3765 | CharUnits::fromQuantity(8)); |
| 3766 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 3767 | |
| 3768 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3769 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3770 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 3771 | CharUnits::fromQuantity(32)); |
| 3772 | } |
| 3773 | |
| 3774 | // Get the address of the saved value by scaling the number of |
| 3775 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3776 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3777 | llvm::Value *RegOffset = |
| 3778 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 3779 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 3780 | RegAddr.getPointer(), RegOffset), |
| 3781 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 3782 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 3783 | |
| 3784 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3785 | NumRegs = |
| 3786 | Builder.CreateAdd(NumRegs, |
| 3787 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3788 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 3789 | |
| 3790 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3791 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3792 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3793 | // Case 2: consume space in the overflow area. |
| 3794 | Address MemAddr = Address::invalid(); |
| 3795 | { |
| 3796 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3797 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 3798 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 3799 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3800 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 3801 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 3802 | |
| 3803 | CharUnits Size; |
| 3804 | if (!isIndirect) { |
| 3805 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3806 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3807 | } else { |
| 3808 | Size = CGF.getPointerSize(); |
| 3809 | } |
| 3810 | |
| 3811 | Address OverflowAreaAddr = |
| 3812 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3813 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3814 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3815 | // Round up address of argument to alignment |
| 3816 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3817 | if (Align > OverflowAreaAlign) { |
| 3818 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 3819 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 3820 | Align); |
| 3821 | } |
| 3822 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3823 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 3824 | |
| 3825 | // Increase the overflow area. |
| 3826 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 3827 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 3828 | CGF.EmitBranch(Cont); |
| 3829 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3830 | |
| 3831 | CGF.EmitBlock(Cont); |
| 3832 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3833 | // Merge the cases with a phi. |
| 3834 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 3835 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3836 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3837 | // Load the pointer if the argument was passed indirectly. |
| 3838 | if (isIndirect) { |
| 3839 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 3840 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | return Result; |
| 3844 | } |
| 3845 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3846 | bool |
| 3847 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3848 | llvm::Value *Address) const { |
| 3849 | // This is calculated from the LLVM and GCC tables and verified |
| 3850 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3851 | |
| 3852 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3853 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3854 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3855 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3856 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3857 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3858 | |
| 3859 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3860 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3861 | |
| 3862 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3863 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3864 | |
| 3865 | // 64-76 are various 4-byte special-purpose registers: |
| 3866 | // 64: mq |
| 3867 | // 65: lr |
| 3868 | // 66: ctr |
| 3869 | // 67: ap |
| 3870 | // 68-75 cr0-7 |
| 3871 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3872 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3873 | |
| 3874 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3875 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3876 | |
| 3877 | // 109: vrsave |
| 3878 | // 110: vscr |
| 3879 | // 111: spe_acc |
| 3880 | // 112: spefscr |
| 3881 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3882 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3883 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3884 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3887 | // PowerPC-64 |
| 3888 | |
| 3889 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3890 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 3891 | class PPC64_SVR4_ABIInfo : public ABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3892 | public: |
| 3893 | enum ABIKind { |
| 3894 | ELFv1 = 0, |
| 3895 | ELFv2 |
| 3896 | }; |
| 3897 | |
| 3898 | private: |
| 3899 | static const unsigned GPRBits = 64; |
| 3900 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3901 | bool HasQPX; |
| 3902 | |
| 3903 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3904 | // will be passed in a QPX register. |
| 3905 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3906 | if (!HasQPX) |
| 3907 | return false; |
| 3908 | |
| 3909 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3910 | unsigned NumElements = VT->getNumElements(); |
| 3911 | if (NumElements == 1) |
| 3912 | return false; |
| 3913 | |
| 3914 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3915 | if (getContext().getTypeSize(Ty) <= 256) |
| 3916 | return true; |
| 3917 | } else if (VT->getElementType()-> |
| 3918 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3919 | if (getContext().getTypeSize(Ty) <= 128) |
| 3920 | return true; |
| 3921 | } |
| 3922 | } |
| 3923 | |
| 3924 | return false; |
| 3925 | } |
| 3926 | |
| 3927 | bool IsQPXVectorTy(QualType Ty) const { |
| 3928 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3929 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3930 | |
| 3931 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3932 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 3933 | : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3934 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3935 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3936 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3937 | |
| 3938 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3939 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3940 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3941 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3942 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3943 | uint64_t Members) const override; |
| 3944 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3945 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3946 | // Example: For aggregate arguments that fit in a register, we could |
| 3947 | // use getDirectInReg (as is done below for structs containing a single |
| 3948 | // floating-point value) to avoid pushing them to memory on function |
| 3949 | // entry. This would require changing the logic in PPCISelLowering |
| 3950 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3951 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3952 | if (!getCXXABI().classifyReturnType(FI)) |
| 3953 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3954 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3955 | // We rely on the default argument classification for the most part. |
| 3956 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3957 | // 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] | 3958 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3959 | if (T) { |
| 3960 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3961 | if (IsQPXVectorTy(T) || |
| 3962 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3963 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3964 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3965 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3966 | continue; |
| 3967 | } |
| 3968 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3969 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3970 | } |
| 3971 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3972 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3973 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3974 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3975 | }; |
| 3976 | |
| 3977 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3978 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3979 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3980 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3981 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 3982 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3983 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3984 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3985 | // This is recovered from gcc output. |
| 3986 | return 1; // r1 is the dedicated stack pointer |
| 3987 | } |
| 3988 | |
| 3989 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3990 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3991 | }; |
| 3992 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3993 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3994 | public: |
| 3995 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3996 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3997 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3998 | // This is recovered from gcc output. |
| 3999 | return 1; // r1 is the dedicated stack pointer |
| 4000 | } |
| 4001 | |
| 4002 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4003 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4004 | }; |
| 4005 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4006 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4007 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4008 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4009 | // extended to 64 bits. |
| 4010 | bool |
| 4011 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4012 | // Treat an enum type as its underlying type. |
| 4013 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4014 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4015 | |
| 4016 | // Promotable integer types are required to be promoted by the ABI. |
| 4017 | if (Ty->isPromotableIntegerType()) |
| 4018 | return true; |
| 4019 | |
| 4020 | // In addition to the usual promotable integer types, we also need to |
| 4021 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4022 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4023 | switch (BT->getKind()) { |
| 4024 | case BuiltinType::Int: |
| 4025 | case BuiltinType::UInt: |
| 4026 | return true; |
| 4027 | default: |
| 4028 | break; |
| 4029 | } |
| 4030 | |
| 4031 | return false; |
| 4032 | } |
| 4033 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4034 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4035 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4036 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4037 | // Complex types are passed just like their elements. |
| 4038 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4039 | Ty = CTy->getElementType(); |
| 4040 | |
| 4041 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4042 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4043 | if (IsQPXVectorTy(Ty)) { |
| 4044 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4045 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4046 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4047 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4048 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4049 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4050 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4051 | |
| 4052 | // For single-element float/vector structs, we consider the whole type |
| 4053 | // to have the same alignment requirements as its single element. |
| 4054 | const Type *AlignAsType = nullptr; |
| 4055 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4056 | if (EltType) { |
| 4057 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4058 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4059 | getContext().getTypeSize(EltType) == 128) || |
| 4060 | (BT && BT->isFloatingPoint())) |
| 4061 | AlignAsType = EltType; |
| 4062 | } |
| 4063 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4064 | // Likewise for ELFv2 homogeneous aggregates. |
| 4065 | const Type *Base = nullptr; |
| 4066 | uint64_t Members = 0; |
| 4067 | if (!AlignAsType && Kind == ELFv2 && |
| 4068 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4069 | AlignAsType = Base; |
| 4070 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4071 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4072 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4073 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4074 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4075 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4076 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4077 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4078 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4079 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4080 | |
| 4081 | // Otherwise, we only need alignment for any aggregate type that |
| 4082 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4083 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4084 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4085 | return CharUnits::fromQuantity(32); |
| 4086 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4087 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4088 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4089 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4092 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4093 | /// aggregate. Base is set to the base element type, and Members is set |
| 4094 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4095 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4096 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4097 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4098 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4099 | if (NElements == 0) |
| 4100 | return false; |
| 4101 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4102 | return false; |
| 4103 | Members *= NElements; |
| 4104 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4105 | const RecordDecl *RD = RT->getDecl(); |
| 4106 | if (RD->hasFlexibleArrayMember()) |
| 4107 | return false; |
| 4108 | |
| 4109 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4110 | |
| 4111 | // If this is a C++ record, check the bases first. |
| 4112 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4113 | for (const auto &I : CXXRD->bases()) { |
| 4114 | // Ignore empty records. |
| 4115 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4116 | continue; |
| 4117 | |
| 4118 | uint64_t FldMembers; |
| 4119 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4120 | return false; |
| 4121 | |
| 4122 | Members += FldMembers; |
| 4123 | } |
| 4124 | } |
| 4125 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4126 | for (const auto *FD : RD->fields()) { |
| 4127 | // Ignore (non-zero arrays of) empty records. |
| 4128 | QualType FT = FD->getType(); |
| 4129 | while (const ConstantArrayType *AT = |
| 4130 | getContext().getAsConstantArrayType(FT)) { |
| 4131 | if (AT->getSize().getZExtValue() == 0) |
| 4132 | return false; |
| 4133 | FT = AT->getElementType(); |
| 4134 | } |
| 4135 | if (isEmptyRecord(getContext(), FT, true)) |
| 4136 | continue; |
| 4137 | |
| 4138 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4139 | if (getContext().getLangOpts().CPlusPlus && |
| 4140 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4141 | continue; |
| 4142 | |
| 4143 | uint64_t FldMembers; |
| 4144 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4145 | return false; |
| 4146 | |
| 4147 | Members = (RD->isUnion() ? |
| 4148 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4149 | } |
| 4150 | |
| 4151 | if (!Base) |
| 4152 | return false; |
| 4153 | |
| 4154 | // Ensure there is no padding. |
| 4155 | if (getContext().getTypeSize(Base) * Members != |
| 4156 | getContext().getTypeSize(Ty)) |
| 4157 | return false; |
| 4158 | } else { |
| 4159 | Members = 1; |
| 4160 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4161 | Members = 2; |
| 4162 | Ty = CT->getElementType(); |
| 4163 | } |
| 4164 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4165 | // Most ABIs only support float, double, and some vector type widths. |
| 4166 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4167 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4168 | |
| 4169 | // The base type must be the same for all members. Types that |
| 4170 | // agree in both total size and mode (float vs. vector) are |
| 4171 | // treated as being equivalent here. |
| 4172 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4173 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4174 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4175 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4176 | // so make sure to widen it explicitly. |
| 4177 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4178 | QualType EltTy = VT->getElementType(); |
| 4179 | unsigned NumElements = |
| 4180 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4181 | Base = getContext() |
| 4182 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4183 | .getTypePtr(); |
| 4184 | } |
| 4185 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4186 | |
| 4187 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4188 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4189 | return false; |
| 4190 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4191 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4192 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4193 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4194 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4195 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4196 | // double, long double, or 128-bit vectors. |
| 4197 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4198 | if (BT->getKind() == BuiltinType::Float || |
| 4199 | BT->getKind() == BuiltinType::Double || |
| 4200 | BT->getKind() == BuiltinType::LongDouble) |
| 4201 | return true; |
| 4202 | } |
| 4203 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4204 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4205 | return true; |
| 4206 | } |
| 4207 | return false; |
| 4208 | } |
| 4209 | |
| 4210 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4211 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4212 | // Vector types require one register, floating point types require one |
| 4213 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4214 | uint32_t NumRegs = |
| 4215 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4216 | |
| 4217 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4218 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4221 | ABIArgInfo |
| 4222 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4223 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4224 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4225 | if (Ty->isAnyComplexType()) |
| 4226 | return ABIArgInfo::getDirect(); |
| 4227 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4228 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4229 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4230 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4231 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4232 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4233 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4234 | else if (Size < 128) { |
| 4235 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4236 | return ABIArgInfo::getDirect(CoerceTy); |
| 4237 | } |
| 4238 | } |
| 4239 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4240 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4241 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4242 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4243 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4244 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4245 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4246 | |
| 4247 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4248 | const Type *Base = nullptr; |
| 4249 | uint64_t Members = 0; |
| 4250 | if (Kind == ELFv2 && |
| 4251 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4252 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4253 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4254 | return ABIArgInfo::getDirect(CoerceTy); |
| 4255 | } |
| 4256 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4257 | // If an aggregate may end up fully in registers, we do not |
| 4258 | // use the ByVal method, but pass the aggregate as array. |
| 4259 | // This is usually beneficial since we avoid forcing the |
| 4260 | // back-end to store the argument to memory. |
| 4261 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4262 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4263 | llvm::Type *CoerceTy; |
| 4264 | |
| 4265 | // Types up to 8 bytes are passed as integer type (which will be |
| 4266 | // properly aligned in the argument save area doubleword). |
| 4267 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4268 | CoerceTy = |
| 4269 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4270 | // Larger types are passed as arrays, with the base type selected |
| 4271 | // according to the required alignment in the save area. |
| 4272 | else { |
| 4273 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4274 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4275 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4276 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4277 | } |
| 4278 | |
| 4279 | return ABIArgInfo::getDirect(CoerceTy); |
| 4280 | } |
| 4281 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4282 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4283 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4284 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4285 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
| 4288 | return (isPromotableTypeForABI(Ty) ? |
| 4289 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4290 | } |
| 4291 | |
| 4292 | ABIArgInfo |
| 4293 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4294 | if (RetTy->isVoidType()) |
| 4295 | return ABIArgInfo::getIgnore(); |
| 4296 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4297 | if (RetTy->isAnyComplexType()) |
| 4298 | return ABIArgInfo::getDirect(); |
| 4299 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4300 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4301 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4302 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4303 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4304 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4305 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4306 | else if (Size < 128) { |
| 4307 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4308 | return ABIArgInfo::getDirect(CoerceTy); |
| 4309 | } |
| 4310 | } |
| 4311 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4312 | if (isAggregateTypeForABI(RetTy)) { |
| 4313 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4314 | const Type *Base = nullptr; |
| 4315 | uint64_t Members = 0; |
| 4316 | if (Kind == ELFv2 && |
| 4317 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4318 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4319 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4320 | return ABIArgInfo::getDirect(CoerceTy); |
| 4321 | } |
| 4322 | |
| 4323 | // ELFv2 small aggregates are returned in up to two registers. |
| 4324 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4325 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4326 | if (Bits == 0) |
| 4327 | return ABIArgInfo::getIgnore(); |
| 4328 | |
| 4329 | llvm::Type *CoerceTy; |
| 4330 | if (Bits > GPRBits) { |
| 4331 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 4332 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4333 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4334 | CoerceTy = |
| 4335 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4336 | return ABIArgInfo::getDirect(CoerceTy); |
| 4337 | } |
| 4338 | |
| 4339 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4340 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4341 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4342 | |
| 4343 | return (isPromotableTypeForABI(RetTy) ? |
| 4344 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4345 | } |
| 4346 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4347 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4348 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4349 | QualType Ty) const { |
| 4350 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4351 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4352 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4353 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4354 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4355 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4356 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4357 | // in separate doublewords. However, Clang expects us to produce a |
| 4358 | // pointer to a structure with the two parts packed tightly. So generate |
| 4359 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4360 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4361 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4362 | CharUnits EltSize = TypeInfo.first / 2; |
| 4363 | if (EltSize < SlotSize) { |
| 4364 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4365 | SlotSize * 2, SlotSize, |
| 4366 | SlotSize, /*AllowHigher*/ true); |
| 4367 | |
| 4368 | Address RealAddr = Addr; |
| 4369 | Address ImagAddr = RealAddr; |
| 4370 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4371 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4372 | SlotSize - EltSize); |
| 4373 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4374 | 2 * SlotSize - EltSize); |
| 4375 | } else { |
| 4376 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4377 | } |
| 4378 | |
| 4379 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4380 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4381 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4382 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4383 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4384 | |
| 4385 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4386 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4387 | /*init*/ true); |
| 4388 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4389 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4390 | } |
| 4391 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4392 | // Otherwise, just use the general rule. |
| 4393 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4394 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | static bool |
| 4398 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4399 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4400 | // This is calculated from the LLVM and GCC tables and verified |
| 4401 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4402 | |
| 4403 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4404 | |
| 4405 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4406 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4407 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4408 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4409 | |
| 4410 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4411 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4412 | |
| 4413 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4414 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4415 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4416 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4417 | // 64: mq |
| 4418 | // 65: lr |
| 4419 | // 66: ctr |
| 4420 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4421 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4422 | |
| 4423 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4424 | // 68-75 cr0-7 |
| 4425 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4426 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4427 | |
| 4428 | // 77-108: v0-31, the 16-byte vector registers |
| 4429 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4430 | |
| 4431 | // 109: vrsave |
| 4432 | // 110: vscr |
| 4433 | // 111: spe_acc |
| 4434 | // 112: spefscr |
| 4435 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4436 | // 114: tfhar |
| 4437 | // 115: tfiar |
| 4438 | // 116: texasr |
| 4439 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4440 | |
| 4441 | return false; |
| 4442 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4443 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4444 | bool |
| 4445 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4446 | CodeGen::CodeGenFunction &CGF, |
| 4447 | llvm::Value *Address) const { |
| 4448 | |
| 4449 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4450 | } |
| 4451 | |
| 4452 | bool |
| 4453 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4454 | llvm::Value *Address) const { |
| 4455 | |
| 4456 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4457 | } |
| 4458 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4459 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4460 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4461 | //===----------------------------------------------------------------------===// |
| 4462 | |
| 4463 | namespace { |
| 4464 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4465 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4466 | public: |
| 4467 | enum ABIKind { |
| 4468 | AAPCS = 0, |
| 4469 | DarwinPCS |
| 4470 | }; |
| 4471 | |
| 4472 | private: |
| 4473 | ABIKind Kind; |
| 4474 | |
| 4475 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4476 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4477 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4478 | |
| 4479 | private: |
| 4480 | ABIKind getABIKind() const { return Kind; } |
| 4481 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4482 | |
| 4483 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4484 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4485 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4486 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4487 | uint64_t Members) const override; |
| 4488 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4489 | bool isIllegalVectorType(QualType Ty) const; |
| 4490 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4491 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4492 | if (!getCXXABI().classifyReturnType(FI)) |
| 4493 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4494 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4495 | for (auto &it : FI.arguments()) |
| 4496 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4499 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4500 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4501 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4502 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4503 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4504 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4505 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4506 | QualType Ty) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4507 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4508 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 4509 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4510 | |
| 4511 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 4512 | ArrayRef<llvm::Type*> scalars, |
| 4513 | bool asReturnValue) const override { |
| 4514 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4515 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4516 | }; |
| 4517 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4518 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4519 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4520 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4521 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4522 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4523 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4524 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 4525 | } |
| 4526 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4527 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4528 | return 31; |
| 4529 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4530 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4531 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4532 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4533 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4534 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4535 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4536 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4537 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4538 | // Handle illegal vector types here. |
| 4539 | if (isIllegalVectorType(Ty)) { |
| 4540 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4541 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 4542 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4543 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 4544 | return ABIArgInfo::getDirect(ResType); |
| 4545 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4546 | if (Size <= 32) { |
| 4547 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4548 | return ABIArgInfo::getDirect(ResType); |
| 4549 | } |
| 4550 | if (Size == 64) { |
| 4551 | llvm::Type *ResType = |
| 4552 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4553 | return ABIArgInfo::getDirect(ResType); |
| 4554 | } |
| 4555 | if (Size == 128) { |
| 4556 | llvm::Type *ResType = |
| 4557 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4558 | return ABIArgInfo::getDirect(ResType); |
| 4559 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4560 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4561 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4562 | |
| 4563 | if (!isAggregateTypeForABI(Ty)) { |
| 4564 | // Treat an enum type as its underlying type. |
| 4565 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4566 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4567 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4568 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4569 | ? ABIArgInfo::getExtend() |
| 4570 | : ABIArgInfo::getDirect()); |
| 4571 | } |
| 4572 | |
| 4573 | // Structures with either a non-trivial destructor or a non-trivial |
| 4574 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4575 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4576 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4577 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4578 | } |
| 4579 | |
| 4580 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4581 | // elsewhere for GNU compatibility. |
| 4582 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4583 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4584 | return ABIArgInfo::getIgnore(); |
| 4585 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4586 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4587 | } |
| 4588 | |
| 4589 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4590 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4591 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4592 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4593 | return ABIArgInfo::getDirect( |
| 4594 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
| 4597 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4598 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4599 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 4600 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 4601 | // same size and alignment. |
| 4602 | if (getTarget().isRenderScriptTarget()) { |
| 4603 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 4604 | } |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4605 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4606 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4607 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4608 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4609 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4610 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4611 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4612 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4613 | } |
| 4614 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4615 | } |
| 4616 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4617 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4620 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4621 | if (RetTy->isVoidType()) |
| 4622 | return ABIArgInfo::getIgnore(); |
| 4623 | |
| 4624 | // Large vector types should be returned via memory. |
| 4625 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4626 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4627 | |
| 4628 | if (!isAggregateTypeForABI(RetTy)) { |
| 4629 | // Treat an enum type as its underlying type. |
| 4630 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4631 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4632 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4633 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4634 | ? ABIArgInfo::getExtend() |
| 4635 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4638 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4639 | return ABIArgInfo::getIgnore(); |
| 4640 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4641 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4642 | uint64_t Members = 0; |
| 4643 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4644 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4645 | return ABIArgInfo::getDirect(); |
| 4646 | |
| 4647 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4648 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4649 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 4650 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 4651 | // same size and alignment. |
| 4652 | if (getTarget().isRenderScriptTarget()) { |
| 4653 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 4654 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4655 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4656 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4657 | |
| 4658 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4659 | // For aggregates with 16-byte alignment, we use i128. |
| 4660 | if (Alignment < 128 && Size == 128) { |
| 4661 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4662 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4663 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4664 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4665 | } |
| 4666 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4667 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4670 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4671 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4672 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4673 | // Check whether VT is legal. |
| 4674 | unsigned NumElements = VT->getNumElements(); |
| 4675 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 4676 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 4677 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4678 | return true; |
| 4679 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4680 | } |
| 4681 | return false; |
| 4682 | } |
| 4683 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4684 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4685 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4686 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4687 | // but with the difference that any floating-point type is allowed, |
| 4688 | // including __fp16. |
| 4689 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4690 | if (BT->isFloatingPoint()) |
| 4691 | return true; |
| 4692 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4693 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4694 | if (VecSize == 64 || VecSize == 128) |
| 4695 | return true; |
| 4696 | } |
| 4697 | return false; |
| 4698 | } |
| 4699 | |
| 4700 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4701 | uint64_t Members) const { |
| 4702 | return Members <= 4; |
| 4703 | } |
| 4704 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4705 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4706 | QualType Ty, |
| 4707 | CodeGenFunction &CGF) const { |
| 4708 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4709 | bool IsIndirect = AI.isIndirect(); |
| 4710 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4711 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4712 | if (IsIndirect) |
| 4713 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4714 | else if (AI.getCoerceToType()) |
| 4715 | BaseTy = AI.getCoerceToType(); |
| 4716 | |
| 4717 | unsigned NumRegs = 1; |
| 4718 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4719 | BaseTy = ArrTy->getElementType(); |
| 4720 | NumRegs = ArrTy->getNumElements(); |
| 4721 | } |
| 4722 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4723 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4724 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4725 | // Standard, section B.4: |
| 4726 | // |
| 4727 | // struct { |
| 4728 | // void *__stack; |
| 4729 | // void *__gr_top; |
| 4730 | // void *__vr_top; |
| 4731 | // int __gr_offs; |
| 4732 | // int __vr_offs; |
| 4733 | // }; |
| 4734 | |
| 4735 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4736 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4737 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4738 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4739 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4740 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4741 | CharUnits TyAlign = TyInfo.second; |
| 4742 | |
| 4743 | Address reg_offs_p = Address::invalid(); |
| 4744 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4745 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4746 | CharUnits reg_top_offset; |
| 4747 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4748 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4749 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4750 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4751 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 4752 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4753 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4754 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4755 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4756 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4757 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4758 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4759 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4760 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 4761 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4762 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4763 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4764 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4765 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | //======================================= |
| 4769 | // Find out where argument was passed |
| 4770 | //======================================= |
| 4771 | |
| 4772 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4773 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4774 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4775 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4776 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4777 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4778 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4779 | |
| 4780 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4781 | |
| 4782 | // 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] | 4783 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4784 | CGF.EmitBlock(MaybeRegBlock); |
| 4785 | |
| 4786 | // Integer arguments may need to correct register alignment (for example a |
| 4787 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4788 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4789 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 4790 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4791 | |
| 4792 | reg_offs = CGF.Builder.CreateAdd( |
| 4793 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4794 | "align_regoffs"); |
| 4795 | reg_offs = CGF.Builder.CreateAnd( |
| 4796 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4797 | "aligned_regoffs"); |
| 4798 | } |
| 4799 | |
| 4800 | // 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] | 4801 | // The fact that this is done unconditionally reflects the fact that |
| 4802 | // allocating an argument to the stack also uses up all the remaining |
| 4803 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4804 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4805 | NewOffset = CGF.Builder.CreateAdd( |
| 4806 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4807 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4808 | |
| 4809 | // Now we're in a position to decide whether this argument really was in |
| 4810 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4811 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4812 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4813 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4814 | |
| 4815 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4816 | |
| 4817 | //======================================= |
| 4818 | // Argument was in registers |
| 4819 | //======================================= |
| 4820 | |
| 4821 | // Now we emit the code for if the argument was originally passed in |
| 4822 | // registers. First start the appropriate block: |
| 4823 | CGF.EmitBlock(InRegBlock); |
| 4824 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4825 | llvm::Value *reg_top = nullptr; |
| 4826 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 4827 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4828 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4829 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 4830 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 4831 | Address RegAddr = Address::invalid(); |
| 4832 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4833 | |
| 4834 | if (IsIndirect) { |
| 4835 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4836 | // stored registers or on the stack will actually be a struct **. |
| 4837 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4838 | } |
| 4839 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4840 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4841 | uint64_t NumMembers = 0; |
| 4842 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4843 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4844 | // Homogeneous aggregates passed in registers will have their elements split |
| 4845 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4846 | // qN+1, ...). We reload and store into a temporary local variable |
| 4847 | // contiguously. |
| 4848 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4849 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4850 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4851 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4852 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 4853 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4854 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4855 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 4856 | int Offset = 0; |
| 4857 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4858 | BaseTyInfo.first.getQuantity() < 16) |
| 4859 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 4860 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4861 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4862 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 4863 | Address LoadAddr = |
| 4864 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 4865 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 4866 | |
| 4867 | Address StoreAddr = |
| 4868 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4869 | |
| 4870 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4871 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4872 | } |
| 4873 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4874 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4875 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4876 | // Otherwise the object is contiguous in memory. |
| 4877 | |
| 4878 | // It might be right-aligned in its slot. |
| 4879 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 4880 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4881 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4882 | TyInfo.first < SlotSize) { |
| 4883 | CharUnits Offset = SlotSize - TyInfo.first; |
| 4884 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4887 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | CGF.EmitBranch(ContBlock); |
| 4891 | |
| 4892 | //======================================= |
| 4893 | // Argument was on the stack |
| 4894 | //======================================= |
| 4895 | CGF.EmitBlock(OnStackBlock); |
| 4896 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4897 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 4898 | CharUnits::Zero(), "stack_p"); |
| 4899 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4900 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4901 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4902 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4903 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 4904 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4905 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4906 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4907 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4908 | OnStackPtr = CGF.Builder.CreateAdd( |
| 4909 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4910 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4911 | OnStackPtr = CGF.Builder.CreateAnd( |
| 4912 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4913 | "align_stack"); |
| 4914 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4915 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4916 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4917 | Address OnStackAddr(OnStackPtr, |
| 4918 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4919 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4920 | // All stack slots are multiples of 8 bytes. |
| 4921 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 4922 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4923 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4924 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4925 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4926 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4927 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4928 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4929 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4930 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4931 | |
| 4932 | // Write the new value of __stack for the next call to va_arg |
| 4933 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4934 | |
| 4935 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4936 | TyInfo.first < StackSlotSize) { |
| 4937 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 4938 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4939 | } |
| 4940 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4941 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4942 | |
| 4943 | CGF.EmitBranch(ContBlock); |
| 4944 | |
| 4945 | //======================================= |
| 4946 | // Tidy up |
| 4947 | //======================================= |
| 4948 | CGF.EmitBlock(ContBlock); |
| 4949 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4950 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 4951 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4952 | |
| 4953 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4954 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 4955 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4956 | |
| 4957 | return ResAddr; |
| 4958 | } |
| 4959 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4960 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4961 | CodeGenFunction &CGF) const { |
| 4962 | // The backend's lowering doesn't support va_arg for aggregates or |
| 4963 | // illegal vector types. Lower VAArg here for these cases and use |
| 4964 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4965 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4966 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4967 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4968 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4969 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4970 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4971 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4972 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 4973 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 4974 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4977 | // The size of the actual thing passed, which might end up just |
| 4978 | // being a pointer for indirect types. |
| 4979 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4980 | |
| 4981 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 4982 | // aggregates should be passed indirectly. |
| 4983 | bool IsIndirect = false; |
| 4984 | if (TyInfo.first.getQuantity() > 16) { |
| 4985 | const Type *Base = nullptr; |
| 4986 | uint64_t Members = 0; |
| 4987 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4990 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 4991 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4992 | } |
| 4993 | |
| 4994 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4995 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4996 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4997 | |
| 4998 | namespace { |
| 4999 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5000 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5001 | public: |
| 5002 | enum ABIKind { |
| 5003 | APCS = 0, |
| 5004 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5005 | AAPCS_VFP = 2, |
| 5006 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5007 | }; |
| 5008 | |
| 5009 | private: |
| 5010 | ABIKind Kind; |
| 5011 | |
| 5012 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5013 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5014 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5015 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5016 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5017 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5018 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5019 | switch (getTarget().getTriple().getEnvironment()) { |
| 5020 | case llvm::Triple::Android: |
| 5021 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5022 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5023 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5024 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5025 | case llvm::Triple::MuslEABI: |
| 5026 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5027 | return true; |
| 5028 | default: |
| 5029 | return false; |
| 5030 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5033 | bool isEABIHF() const { |
| 5034 | switch (getTarget().getTriple().getEnvironment()) { |
| 5035 | case llvm::Triple::EABIHF: |
| 5036 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5037 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5038 | return true; |
| 5039 | default: |
| 5040 | return false; |
| 5041 | } |
| 5042 | } |
| 5043 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5044 | ABIKind getABIKind() const { return Kind; } |
| 5045 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5046 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 5047 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5048 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5049 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5050 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5051 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5052 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5053 | uint64_t Members) const override; |
| 5054 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5055 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5056 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5057 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5058 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5059 | |
| 5060 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5061 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5062 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5063 | |
| 5064 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 5065 | ArrayRef<llvm::Type*> scalars, |
| 5066 | bool asReturnValue) const override { |
| 5067 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5068 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5069 | }; |
| 5070 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5071 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5072 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5073 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5074 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5075 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5076 | const ARMABIInfo &getABIInfo() const { |
| 5077 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5078 | } |
| 5079 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5080 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5081 | return 13; |
| 5082 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5083 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5084 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5085 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 5086 | } |
| 5087 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5088 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5089 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5090 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5091 | |
| 5092 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5093 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5094 | return false; |
| 5095 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5096 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5097 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5098 | if (getABIInfo().isEABI()) return 88; |
| 5099 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5100 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5101 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5102 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5103 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5104 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5105 | if (!FD) |
| 5106 | return; |
| 5107 | |
| 5108 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5109 | if (!Attr) |
| 5110 | return; |
| 5111 | |
| 5112 | const char *Kind; |
| 5113 | switch (Attr->getInterrupt()) { |
| 5114 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5115 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5116 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5117 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5118 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5119 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5120 | } |
| 5121 | |
| 5122 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5123 | |
| 5124 | Fn->addFnAttr("interrupt", Kind); |
| 5125 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5126 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5127 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5128 | return; |
| 5129 | |
| 5130 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5131 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5132 | // the backend to perform a realignment as part of the function prologue. |
| 5133 | llvm::AttrBuilder B; |
| 5134 | B.addStackAlignmentAttr(8); |
| 5135 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 5136 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 5137 | llvm::AttributeSet::FunctionIndex, |
| 5138 | B)); |
| 5139 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5140 | }; |
| 5141 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5142 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5143 | public: |
| 5144 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5145 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5146 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5147 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5148 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5149 | |
| 5150 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5151 | llvm::SmallString<24> &Opt) const override { |
| 5152 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5153 | } |
| 5154 | |
| 5155 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5156 | llvm::SmallString<32> &Opt) const override { |
| 5157 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5158 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5159 | }; |
| 5160 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5161 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5162 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5163 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5164 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 5165 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5166 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5167 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5168 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5169 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5170 | FI.getReturnInfo() = |
| 5171 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5172 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5173 | for (auto &I : FI.arguments()) |
| 5174 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5175 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5176 | // Always honor user-specified calling convention. |
| 5177 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5178 | return; |
| 5179 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5180 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5181 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5182 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5183 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5184 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5185 | /// Return the default calling convention that LLVM will use. |
| 5186 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5187 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5188 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5189 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5190 | else if (isEABI()) |
| 5191 | return llvm::CallingConv::ARM_AAPCS; |
| 5192 | else |
| 5193 | return llvm::CallingConv::ARM_APCS; |
| 5194 | } |
| 5195 | |
| 5196 | /// Return the calling convention that our ABI would like us to use |
| 5197 | /// as the C calling convention. |
| 5198 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5199 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5200 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5201 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5202 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5203 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5204 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5205 | llvm_unreachable("bad ABI kind"); |
| 5206 | } |
| 5207 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5208 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5209 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5210 | |
| 5211 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5212 | // they'd just match what LLVM will infer from the triple. |
| 5213 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5214 | if (abiCC != getLLVMDefaultCC()) |
| 5215 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5216 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5217 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 5218 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 5219 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
| 5220 | switch (getABIKind()) { |
| 5221 | case APCS: |
| 5222 | case AAPCS16_VFP: |
| 5223 | if (abiCC != getLLVMDefaultCC()) |
| 5224 | BuiltinCC = abiCC; |
| 5225 | break; |
| 5226 | case AAPCS: |
| 5227 | case AAPCS_VFP: |
| 5228 | BuiltinCC = llvm::CallingConv::ARM_AAPCS; |
| 5229 | break; |
| 5230 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5231 | } |
| 5232 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5233 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5234 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5235 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5236 | // A single-precision floating-point type (including promoted |
| 5237 | // half-precision types); A double-precision floating-point type; |
| 5238 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5239 | // with a Base Type of a single- or double-precision floating-point type, |
| 5240 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5241 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5242 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5243 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5244 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5245 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5246 | // Handle illegal vector types here. |
| 5247 | if (isIllegalVectorType(Ty)) { |
| 5248 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5249 | if (Size <= 32) { |
| 5250 | llvm::Type *ResType = |
| 5251 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5252 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5253 | } |
| 5254 | if (Size == 64) { |
| 5255 | llvm::Type *ResType = llvm::VectorType::get( |
| 5256 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5257 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5258 | } |
| 5259 | if (Size == 128) { |
| 5260 | llvm::Type *ResType = llvm::VectorType::get( |
| 5261 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5262 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5263 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5264 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5265 | } |
| 5266 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5267 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 5268 | // unspecified. This is not done for OpenCL as it handles the half type |
| 5269 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5270 | if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5271 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5272 | llvm::Type::getFloatTy(getVMContext()) : |
| 5273 | llvm::Type::getInt32Ty(getVMContext()); |
| 5274 | return ABIArgInfo::getDirect(ResType); |
| 5275 | } |
| 5276 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5277 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5278 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5279 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5280 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5281 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5282 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5283 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5284 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5285 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5286 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5287 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5288 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5289 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5290 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5291 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5292 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5293 | return ABIArgInfo::getIgnore(); |
| 5294 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5295 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5296 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5297 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5298 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5299 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5300 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5301 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5302 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5303 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5304 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5305 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5306 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5307 | // this convention even for a variadic function: the backend will use GPRs |
| 5308 | // if needed. |
| 5309 | const Type *Base = nullptr; |
| 5310 | uint64_t Members = 0; |
| 5311 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5312 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5313 | llvm::Type *Ty = |
| 5314 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5315 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5316 | } |
| 5317 | } |
| 5318 | |
| 5319 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5320 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5321 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5322 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5323 | // and a pointer is passed. |
| 5324 | return ABIArgInfo::getIndirect( |
| 5325 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5326 | } |
| 5327 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5328 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5329 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5330 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5331 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5332 | uint64_t ABIAlign = 4; |
| 5333 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5334 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5335 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5336 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5337 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5338 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5339 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5340 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5341 | /*ByVal=*/true, |
| 5342 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5343 | } |
| 5344 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5345 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5346 | // same size and alignment. |
| 5347 | if (getTarget().isRenderScriptTarget()) { |
| 5348 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5349 | } |
| 5350 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5351 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5352 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5353 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5354 | // FIXME: Try to match the types of the arguments more accurately where |
| 5355 | // we can. |
| 5356 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5357 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5358 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5359 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5360 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5361 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5362 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5363 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5364 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5367 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5368 | llvm::LLVMContext &VMContext) { |
| 5369 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5370 | // is called integer-like if its size is less than or equal to one word, and |
| 5371 | // the offset of each of its addressable sub-fields is zero. |
| 5372 | |
| 5373 | uint64_t Size = Context.getTypeSize(Ty); |
| 5374 | |
| 5375 | // Check that the type fits in a word. |
| 5376 | if (Size > 32) |
| 5377 | return false; |
| 5378 | |
| 5379 | // FIXME: Handle vector types! |
| 5380 | if (Ty->isVectorType()) |
| 5381 | return false; |
| 5382 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5383 | // Float types are never treated as "integer like". |
| 5384 | if (Ty->isRealFloatingType()) |
| 5385 | return false; |
| 5386 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5387 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5388 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5389 | return true; |
| 5390 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5391 | // Small complex integer types are "integer like". |
| 5392 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5393 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5394 | |
| 5395 | // Single element and zero sized arrays should be allowed, by the definition |
| 5396 | // above, but they are not. |
| 5397 | |
| 5398 | // Otherwise, it must be a record type. |
| 5399 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5400 | if (!RT) return false; |
| 5401 | |
| 5402 | // Ignore records with flexible arrays. |
| 5403 | const RecordDecl *RD = RT->getDecl(); |
| 5404 | if (RD->hasFlexibleArrayMember()) |
| 5405 | return false; |
| 5406 | |
| 5407 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5408 | // like". |
| 5409 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5410 | |
| 5411 | bool HadField = false; |
| 5412 | unsigned idx = 0; |
| 5413 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5414 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5415 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5416 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5417 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5418 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5419 | // struct { int : 0; int x } |
| 5420 | // is non-integer like according to gcc. |
| 5421 | if (FD->isBitField()) { |
| 5422 | if (!RD->isUnion()) |
| 5423 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5424 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5425 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5426 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5427 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5428 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5429 | } |
| 5430 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5431 | // Check if this field is at offset 0. |
| 5432 | if (Layout.getFieldOffset(idx) != 0) |
| 5433 | return false; |
| 5434 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5435 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5436 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5437 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5438 | // Only allow at most one field in a structure. This doesn't match the |
| 5439 | // wording above, but follows gcc in situations with a field following an |
| 5440 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5441 | if (!RD->isUnion()) { |
| 5442 | if (HadField) |
| 5443 | return false; |
| 5444 | |
| 5445 | HadField = true; |
| 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | return true; |
| 5450 | } |
| 5451 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5452 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5453 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5454 | bool IsEffectivelyAAPCS_VFP = |
| 5455 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5456 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5457 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5458 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5459 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5460 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5461 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5462 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5463 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5464 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5465 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5466 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5467 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5468 | if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5469 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5470 | llvm::Type::getFloatTy(getVMContext()) : |
| 5471 | llvm::Type::getInt32Ty(getVMContext()); |
| 5472 | return ABIArgInfo::getDirect(ResType); |
| 5473 | } |
| 5474 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5475 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5476 | // Treat an enum type as its underlying type. |
| 5477 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5478 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5479 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5480 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5481 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5482 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5483 | |
| 5484 | // Are we following APCS? |
| 5485 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5486 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5487 | return ABIArgInfo::getIgnore(); |
| 5488 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5489 | // Complex types are all returned as packed integers. |
| 5490 | // |
| 5491 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5492 | // correctly. |
| 5493 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5494 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5495 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5496 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5497 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5498 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5499 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5500 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5501 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5502 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5503 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5504 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5505 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5509 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5510 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5511 | |
| 5512 | // Otherwise this is an AAPCS variant. |
| 5513 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5514 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5515 | return ABIArgInfo::getIgnore(); |
| 5516 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5517 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5518 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5519 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5520 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5521 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5522 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5523 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5524 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5525 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5526 | } |
| 5527 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5528 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5529 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5530 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5531 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5532 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 5533 | // same size and alignment. |
| 5534 | if (getTarget().isRenderScriptTarget()) { |
| 5535 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5536 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5537 | if (getDataLayout().isBigEndian()) |
| 5538 | // 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] | 5539 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5540 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5541 | // Return in the smallest viable integer type. |
| 5542 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5543 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5544 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5545 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5546 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5547 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5548 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5549 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5550 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5551 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5552 | } |
| 5553 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5554 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5557 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5558 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 5559 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 5560 | if (isAndroid()) { |
| 5561 | // Android shipped using Clang 3.1, which supported a slightly different |
| 5562 | // vector ABI. The primary differences were that 3-element vector types |
| 5563 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 5564 | // accepts that legacy behavior for Android only. |
| 5565 | // Check whether VT is legal. |
| 5566 | unsigned NumElements = VT->getNumElements(); |
| 5567 | // NumElements should be power of 2 or equal to 3. |
| 5568 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 5569 | return true; |
| 5570 | } else { |
| 5571 | // Check whether VT is legal. |
| 5572 | unsigned NumElements = VT->getNumElements(); |
| 5573 | uint64_t Size = getContext().getTypeSize(VT); |
| 5574 | // NumElements should be power of 2. |
| 5575 | if (!llvm::isPowerOf2_32(NumElements)) |
| 5576 | return true; |
| 5577 | // Size should be greater than 32 bits. |
| 5578 | return Size <= 32; |
| 5579 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5580 | } |
| 5581 | return false; |
| 5582 | } |
| 5583 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5584 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5585 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 5586 | // double, or 64-bit or 128-bit vectors. |
| 5587 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5588 | if (BT->getKind() == BuiltinType::Float || |
| 5589 | BT->getKind() == BuiltinType::Double || |
| 5590 | BT->getKind() == BuiltinType::LongDouble) |
| 5591 | return true; |
| 5592 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5593 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5594 | if (VecSize == 64 || VecSize == 128) |
| 5595 | return true; |
| 5596 | } |
| 5597 | return false; |
| 5598 | } |
| 5599 | |
| 5600 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5601 | uint64_t Members) const { |
| 5602 | return Members <= 4; |
| 5603 | } |
| 5604 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5605 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5606 | QualType Ty) const { |
| 5607 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5608 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5609 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5610 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5611 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 5612 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5613 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5616 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5617 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5618 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5619 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 5620 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5621 | const Type *Base = nullptr; |
| 5622 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5623 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 5624 | IsIndirect = true; |
| 5625 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5626 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 5627 | // allocated by the caller. |
| 5628 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 5629 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5630 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 5631 | IsIndirect = true; |
| 5632 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5633 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5634 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 5635 | // 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] | 5636 | // Our callers should be prepared to handle an under-aligned address. |
| 5637 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 5638 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5639 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5640 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 5641 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5642 | // ARMv7k allows type alignment up to 16 bytes. |
| 5643 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5644 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5645 | } else { |
| 5646 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5647 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5648 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5649 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5650 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 5651 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5654 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5655 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5656 | //===----------------------------------------------------------------------===// |
| 5657 | |
| 5658 | namespace { |
| 5659 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5660 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5661 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5662 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5663 | |
| 5664 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5665 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5666 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5667 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5668 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5669 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5670 | }; |
| 5671 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5672 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5673 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5674 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5675 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5676 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5677 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5678 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5679 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5680 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5681 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5682 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5683 | }; |
| 5684 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5685 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5686 | if (RetTy->isVoidType()) |
| 5687 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5688 | |
| 5689 | // note: this is different from default ABI |
| 5690 | if (!RetTy->isScalarType()) |
| 5691 | return ABIArgInfo::getDirect(); |
| 5692 | |
| 5693 | // Treat an enum type as its underlying type. |
| 5694 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5695 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5696 | |
| 5697 | return (RetTy->isPromotableIntegerType() ? |
| 5698 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5699 | } |
| 5700 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5701 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5702 | // Treat an enum type as its underlying type. |
| 5703 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5704 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5705 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5706 | // Return aggregates type as indirect by value |
| 5707 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5708 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5709 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5710 | return (Ty->isPromotableIntegerType() ? |
| 5711 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5712 | } |
| 5713 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5714 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5715 | if (!getCXXABI().classifyReturnType(FI)) |
| 5716 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5717 | for (auto &I : FI.arguments()) |
| 5718 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5719 | |
| 5720 | // Always honor user-specified calling convention. |
| 5721 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5722 | return; |
| 5723 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5724 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5725 | } |
| 5726 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5727 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5728 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5729 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5730 | } |
| 5731 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5732 | void NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5733 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5734 | CodeGen::CodeGenModule &M) const{ |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5735 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5736 | if (!FD) return; |
| 5737 | |
| 5738 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5739 | |
| 5740 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5741 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5742 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5743 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5744 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5745 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5746 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5747 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5748 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5749 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5750 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5751 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5752 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5753 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5754 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5755 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5756 | // __global__ functions cannot be called from the device, we do not |
| 5757 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5758 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5759 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5760 | addNVVMMetadata(F, "kernel", 1); |
| 5761 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5762 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5763 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5764 | llvm::APSInt MaxThreads(32); |
| 5765 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5766 | if (MaxThreads > 0) |
| 5767 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5768 | |
| 5769 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5770 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5771 | // we don't have to add a PTX directive. |
| 5772 | if (Attr->getMinBlocks()) { |
| 5773 | llvm::APSInt MinBlocks(32); |
| 5774 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5775 | if (MinBlocks > 0) |
| 5776 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5777 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5778 | } |
| 5779 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5780 | } |
| 5781 | } |
| 5782 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5783 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5784 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5785 | llvm::Module *M = F->getParent(); |
| 5786 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5787 | |
| 5788 | // Get "nvvm.annotations" metadata node |
| 5789 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5790 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5791 | llvm::Metadata *MDVals[] = { |
| 5792 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5793 | llvm::ConstantAsMetadata::get( |
| 5794 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5795 | // Append metadata to nvvm.annotations |
| 5796 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5797 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5798 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5799 | |
| 5800 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5801 | // SystemZ ABI Implementation |
| 5802 | //===----------------------------------------------------------------------===// |
| 5803 | |
| 5804 | namespace { |
| 5805 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 5806 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5807 | bool HasVector; |
| 5808 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5809 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5810 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 5811 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5812 | |
| 5813 | bool isPromotableIntegerType(QualType Ty) const; |
| 5814 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5815 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5816 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5817 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5818 | |
| 5819 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5820 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5821 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5822 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5823 | if (!getCXXABI().classifyReturnType(FI)) |
| 5824 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5825 | for (auto &I : FI.arguments()) |
| 5826 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5827 | } |
| 5828 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5829 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5830 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 5831 | |
| 5832 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 5833 | ArrayRef<llvm::Type*> scalars, |
| 5834 | bool asReturnValue) const override { |
| 5835 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5836 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5837 | }; |
| 5838 | |
| 5839 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5840 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5841 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5842 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5843 | }; |
| 5844 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5845 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5846 | |
| 5847 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5848 | // Treat an enum type as its underlying type. |
| 5849 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5850 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5851 | |
| 5852 | // Promotable integer types are required to be promoted by the ABI. |
| 5853 | if (Ty->isPromotableIntegerType()) |
| 5854 | return true; |
| 5855 | |
| 5856 | // 32-bit values must also be promoted. |
| 5857 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5858 | switch (BT->getKind()) { |
| 5859 | case BuiltinType::Int: |
| 5860 | case BuiltinType::UInt: |
| 5861 | return true; |
| 5862 | default: |
| 5863 | return false; |
| 5864 | } |
| 5865 | return false; |
| 5866 | } |
| 5867 | |
| 5868 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5869 | return (Ty->isAnyComplexType() || |
| 5870 | Ty->isVectorType() || |
| 5871 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5872 | } |
| 5873 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5874 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5875 | return (HasVector && |
| 5876 | Ty->isVectorType() && |
| 5877 | getContext().getTypeSize(Ty) <= 128); |
| 5878 | } |
| 5879 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5880 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5881 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5882 | switch (BT->getKind()) { |
| 5883 | case BuiltinType::Float: |
| 5884 | case BuiltinType::Double: |
| 5885 | return true; |
| 5886 | default: |
| 5887 | return false; |
| 5888 | } |
| 5889 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5890 | return false; |
| 5891 | } |
| 5892 | |
| 5893 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5894 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5895 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5896 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5897 | |
| 5898 | // If this is a C++ record, check the bases first. |
| 5899 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5900 | for (const auto &I : CXXRD->bases()) { |
| 5901 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5902 | |
| 5903 | // Empty bases don't affect things either way. |
| 5904 | if (isEmptyRecord(getContext(), Base, true)) |
| 5905 | continue; |
| 5906 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5907 | if (!Found.isNull()) |
| 5908 | return Ty; |
| 5909 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5910 | } |
| 5911 | |
| 5912 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5913 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5914 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5915 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5916 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5917 | if (getContext().getLangOpts().CPlusPlus && |
| 5918 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5919 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5920 | |
| 5921 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5922 | // Nested structures still do though. |
| 5923 | if (!Found.isNull()) |
| 5924 | return Ty; |
| 5925 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5926 | } |
| 5927 | |
| 5928 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5929 | // 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] | 5930 | if (!Found.isNull()) |
| 5931 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5932 | } |
| 5933 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5934 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5937 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5938 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5939 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5940 | // struct { |
| 5941 | // i64 __gpr; |
| 5942 | // i64 __fpr; |
| 5943 | // i8 *__overflow_arg_area; |
| 5944 | // i8 *__reg_save_area; |
| 5945 | // }; |
| 5946 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5947 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5948 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5949 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5950 | Ty = getContext().getCanonicalType(Ty); |
| 5951 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5952 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5953 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5954 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5955 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5956 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5957 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5958 | CharUnits UnpaddedSize; |
| 5959 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5960 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5961 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 5962 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5963 | } else { |
| 5964 | if (AI.getCoerceToType()) |
| 5965 | ArgTy = AI.getCoerceToType(); |
| 5966 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5967 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5968 | UnpaddedSize = TyInfo.first; |
| 5969 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5970 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5971 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 5972 | if (IsVector && UnpaddedSize > PaddedSize) |
| 5973 | PaddedSize = CharUnits::fromQuantity(16); |
| 5974 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5975 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5976 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5977 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5978 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5979 | llvm::Value *PaddedSizeV = |
| 5980 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5981 | |
| 5982 | if (IsVector) { |
| 5983 | // Work out the address of a vector argument on the stack. |
| 5984 | // Vector arguments are always passed in the high bits of a |
| 5985 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5986 | Address OverflowArgAreaPtr = |
| 5987 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5988 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5989 | Address OverflowArgArea = |
| 5990 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5991 | TyInfo.second); |
| 5992 | Address MemAddr = |
| 5993 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5994 | |
| 5995 | // Update overflow_arg_area_ptr pointer |
| 5996 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5997 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5998 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5999 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6000 | |
| 6001 | return MemAddr; |
| 6002 | } |
| 6003 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6004 | assert(PaddedSize.getQuantity() == 8); |
| 6005 | |
| 6006 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6007 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6008 | if (InFPRs) { |
| 6009 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6010 | RegCountField = 1; // __fpr |
| 6011 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6012 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6013 | } else { |
| 6014 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6015 | RegCountField = 0; // __gpr |
| 6016 | RegSaveIndex = 2; // save offset for r2 |
| 6017 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6018 | } |
| 6019 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6020 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 6021 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 6022 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6023 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6024 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6025 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6026 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6027 | |
| 6028 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6029 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6030 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6031 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6032 | |
| 6033 | // Emit code to load the value if it was passed in registers. |
| 6034 | CGF.EmitBlock(InRegBlock); |
| 6035 | |
| 6036 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6037 | llvm::Value *ScaledRegCount = |
| 6038 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6039 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6040 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6041 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6042 | llvm::Value *RegOffset = |
| 6043 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6044 | Address RegSaveAreaPtr = |
| 6045 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 6046 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6047 | llvm::Value *RegSaveArea = |
| 6048 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6049 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6050 | "raw_reg_addr"), |
| 6051 | PaddedSize); |
| 6052 | Address RegAddr = |
| 6053 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6054 | |
| 6055 | // Update the register count |
| 6056 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6057 | llvm::Value *NewRegCount = |
| 6058 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6059 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6060 | CGF.EmitBranch(ContBlock); |
| 6061 | |
| 6062 | // Emit code to load the value if it was passed in memory. |
| 6063 | CGF.EmitBlock(InMemBlock); |
| 6064 | |
| 6065 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6066 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 6067 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 6068 | Address OverflowArgArea = |
| 6069 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6070 | PaddedSize); |
| 6071 | Address RawMemAddr = |
| 6072 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6073 | Address MemAddr = |
| 6074 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6075 | |
| 6076 | // Update overflow_arg_area_ptr pointer |
| 6077 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6078 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6079 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6080 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6081 | CGF.EmitBranch(ContBlock); |
| 6082 | |
| 6083 | // Return the appropriate result. |
| 6084 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6085 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6086 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6087 | |
| 6088 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6089 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6090 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6091 | |
| 6092 | return ResAddr; |
| 6093 | } |
| 6094 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6095 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6096 | if (RetTy->isVoidType()) |
| 6097 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6098 | if (isVectorArgumentType(RetTy)) |
| 6099 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6100 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6101 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6102 | return (isPromotableIntegerType(RetTy) ? |
| 6103 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6104 | } |
| 6105 | |
| 6106 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6107 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6108 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6109 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6110 | |
| 6111 | // Integers and enums are extended to full register width. |
| 6112 | if (isPromotableIntegerType(Ty)) |
| 6113 | return ABIArgInfo::getExtend(); |
| 6114 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6115 | // Handle vector types and vector-like structure types. Note that |
| 6116 | // as opposed to float-like structure types, we do not allow any |
| 6117 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6118 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6119 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6120 | if (isVectorArgumentType(SingleElementTy) && |
| 6121 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6122 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6123 | |
| 6124 | // 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] | 6125 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6126 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6127 | |
| 6128 | // Handle small structures. |
| 6129 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6130 | // Structures with flexible arrays have variable length, so really |
| 6131 | // fail the size test above. |
| 6132 | const RecordDecl *RD = RT->getDecl(); |
| 6133 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6134 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6135 | |
| 6136 | // The structure is passed as an unextended integer, a float, or a double. |
| 6137 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6138 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6139 | assert(Size == 32 || Size == 64); |
| 6140 | if (Size == 32) |
| 6141 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6142 | else |
| 6143 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6144 | } else |
| 6145 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6146 | return ABIArgInfo::getDirect(PassTy); |
| 6147 | } |
| 6148 | |
| 6149 | // Non-structure compounds are passed indirectly. |
| 6150 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6151 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6152 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6153 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6154 | } |
| 6155 | |
| 6156 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6157 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6158 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6159 | |
| 6160 | namespace { |
| 6161 | |
| 6162 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6163 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6164 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6165 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6166 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6167 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6168 | }; |
| 6169 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6170 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6171 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6172 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6173 | llvm::GlobalValue *GV, |
| 6174 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6175 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6176 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 6177 | // Handle 'interrupt' attribute: |
| 6178 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6179 | |
| 6180 | // Step 1: Set ISR calling convention. |
| 6181 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 6182 | |
| 6183 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6184 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6185 | |
| 6186 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 6187 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 6188 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 6189 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6190 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6191 | } |
| 6192 | } |
| 6193 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6194 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6195 | // MIPS ABI Implementation. This works for both little-endian and |
| 6196 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6197 | //===----------------------------------------------------------------------===// |
| 6198 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6199 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6200 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6201 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6202 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6203 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6204 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6205 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6206 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6207 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6208 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6209 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6210 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6211 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6212 | |
| 6213 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6214 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6215 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6216 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6217 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6218 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6219 | }; |
| 6220 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6221 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6222 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6223 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6224 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6225 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6226 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6227 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6228 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6229 | return 29; |
| 6230 | } |
| 6231 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6232 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6233 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6234 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6235 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6236 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6237 | if (FD->hasAttr<Mips16Attr>()) { |
| 6238 | Fn->addFnAttr("mips16"); |
| 6239 | } |
| 6240 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6241 | Fn->addFnAttr("nomips16"); |
| 6242 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6243 | |
| 6244 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6245 | if (!Attr) |
| 6246 | return; |
| 6247 | |
| 6248 | const char *Kind; |
| 6249 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6250 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6251 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6252 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6253 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6254 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6255 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6256 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6257 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6258 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6259 | } |
| 6260 | |
| 6261 | Fn->addFnAttr("interrupt", Kind); |
| 6262 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6263 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6264 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6265 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6266 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6267 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6268 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6269 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6270 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6271 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6272 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6273 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6274 | void MipsABIInfo::CoerceToIntArgs( |
| 6275 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6276 | llvm::IntegerType *IntTy = |
| 6277 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6278 | |
| 6279 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6280 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6281 | ArgList.push_back(IntTy); |
| 6282 | |
| 6283 | // If necessary, add one more integer type to ArgList. |
| 6284 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6285 | |
| 6286 | if (R) |
| 6287 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6288 | } |
| 6289 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6290 | // In N32/64, an aligned double precision floating point field is passed in |
| 6291 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6292 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6293 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6294 | |
| 6295 | if (IsO32) { |
| 6296 | CoerceToIntArgs(TySize, ArgList); |
| 6297 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6298 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6299 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6300 | if (Ty->isComplexType()) |
| 6301 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6302 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6303 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6304 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6305 | // Unions/vectors are passed in integer registers. |
| 6306 | if (!RT || !RT->isStructureOrClassType()) { |
| 6307 | CoerceToIntArgs(TySize, ArgList); |
| 6308 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6309 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6310 | |
| 6311 | const RecordDecl *RD = RT->getDecl(); |
| 6312 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6313 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6314 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6315 | uint64_t LastOffset = 0; |
| 6316 | unsigned idx = 0; |
| 6317 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6318 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6319 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6320 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6321 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6322 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6323 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6324 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6325 | |
| 6326 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6327 | continue; |
| 6328 | |
| 6329 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6330 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6331 | continue; |
| 6332 | |
| 6333 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6334 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6335 | ArgList.push_back(I64); |
| 6336 | |
| 6337 | // Add double type. |
| 6338 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6339 | LastOffset = Offset + 64; |
| 6340 | } |
| 6341 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6342 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6343 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6344 | |
| 6345 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6346 | } |
| 6347 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6348 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6349 | uint64_t Offset) const { |
| 6350 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6351 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6352 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6353 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6354 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6355 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6356 | ABIArgInfo |
| 6357 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6358 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6359 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6360 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6361 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6362 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6363 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6364 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6365 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6366 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6367 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6368 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6369 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6370 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6371 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6372 | return ABIArgInfo::getIgnore(); |
| 6373 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6374 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6375 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6376 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6377 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6378 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6379 | // If we have reached here, aggregates are passed directly by coercing to |
| 6380 | // another structure type. Padding is inserted if the offset of the |
| 6381 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6382 | ABIArgInfo ArgInfo = |
| 6383 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6384 | getPaddingType(OrigOffset, CurrOffset)); |
| 6385 | ArgInfo.setInReg(true); |
| 6386 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6387 | } |
| 6388 | |
| 6389 | // Treat an enum type as its underlying type. |
| 6390 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6391 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6392 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6393 | // All integral types are promoted to the GPR width. |
| 6394 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6395 | return ABIArgInfo::getExtend(); |
| 6396 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6397 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6398 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6399 | } |
| 6400 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6401 | llvm::Type* |
| 6402 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6403 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6404 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6405 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6406 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6407 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6408 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6409 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6410 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6411 | // N32/64 returns struct/classes in floating point registers if the |
| 6412 | // following conditions are met: |
| 6413 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6414 | // 2. The struct/class has one or two fields all of which are floating |
| 6415 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6416 | // 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] | 6417 | // |
| 6418 | // Any other composite results are returned in integer registers. |
| 6419 | // |
| 6420 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6421 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6422 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6423 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6424 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6425 | if (!BT || !BT->isFloatingPoint()) |
| 6426 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6427 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6428 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6429 | } |
| 6430 | |
| 6431 | if (b == e) |
| 6432 | return llvm::StructType::get(getVMContext(), RTList, |
| 6433 | RD->hasAttr<PackedAttr>()); |
| 6434 | |
| 6435 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6436 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6437 | } |
| 6438 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6439 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6440 | return llvm::StructType::get(getVMContext(), RTList); |
| 6441 | } |
| 6442 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6443 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6444 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6445 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6446 | if (RetTy->isVoidType()) |
| 6447 | return ABIArgInfo::getIgnore(); |
| 6448 | |
| 6449 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6450 | // However, N32/N64 ignores zero sized return values. |
| 6451 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6452 | return ABIArgInfo::getIgnore(); |
| 6453 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6454 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6455 | if (Size <= 128) { |
| 6456 | if (RetTy->isAnyComplexType()) |
| 6457 | return ABIArgInfo::getDirect(); |
| 6458 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6459 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6460 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6461 | if (!IsO32 || |
| 6462 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6463 | ABIArgInfo ArgInfo = |
| 6464 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6465 | ArgInfo.setInReg(true); |
| 6466 | return ArgInfo; |
| 6467 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6468 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6469 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6470 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6471 | } |
| 6472 | |
| 6473 | // Treat an enum type as its underlying type. |
| 6474 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6475 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6476 | |
| 6477 | return (RetTy->isPromotableIntegerType() ? |
| 6478 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6479 | } |
| 6480 | |
| 6481 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6482 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6483 | if (!getCXXABI().classifyReturnType(FI)) |
| 6484 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6485 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6486 | // 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] | 6487 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6488 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6489 | for (auto &I : FI.arguments()) |
| 6490 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6491 | } |
| 6492 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6493 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6494 | QualType OrigTy) const { |
| 6495 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6496 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6497 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6498 | // 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] | 6499 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6500 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6501 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6502 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6503 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6504 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6505 | DidPromote = true; |
| 6506 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6507 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6508 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6509 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6510 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6511 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6512 | // The alignment of things in the argument area is never larger than |
| 6513 | // StackAlignInBytes. |
| 6514 | TyInfo.second = |
| 6515 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6516 | |
| 6517 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6518 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6519 | |
| 6520 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6521 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6522 | |
| 6523 | |
| 6524 | // If there was a promotion, "unpromote" into a temporary. |
| 6525 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6526 | if (DidPromote) { |
| 6527 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6528 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6529 | |
| 6530 | // Truncate down to the right width. |
| 6531 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6532 | : CGF.IntPtrTy); |
| 6533 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6534 | if (OrigTy->isPointerType()) |
| 6535 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6536 | |
| 6537 | CGF.Builder.CreateStore(V, Temp); |
| 6538 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6539 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6540 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6541 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6542 | } |
| 6543 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6544 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6545 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6546 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6547 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 6548 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 6549 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6550 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6551 | return false; |
| 6552 | } |
| 6553 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6554 | bool |
| 6555 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6556 | llvm::Value *Address) const { |
| 6557 | // This information comes from gcc's implementation, which seems to |
| 6558 | // as canonical as it gets. |
| 6559 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6560 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 6561 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6562 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6563 | |
| 6564 | // 0-31 are the general purpose registers, $0 - $31. |
| 6565 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 6566 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 6567 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6568 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6569 | |
| 6570 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 6571 | // They are one bit wide and ignored here. |
| 6572 | |
| 6573 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 6574 | // (coprocessor 1 is the FP unit) |
| 6575 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 6576 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 6577 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6578 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6579 | return false; |
| 6580 | } |
| 6581 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6582 | //===----------------------------------------------------------------------===// |
| 6583 | // 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] | 6584 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6585 | // handling. |
| 6586 | //===----------------------------------------------------------------------===// |
| 6587 | |
| 6588 | namespace { |
| 6589 | |
| 6590 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 6591 | public: |
| 6592 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 6593 | : DefaultTargetCodeGenInfo(CGT) {} |
| 6594 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6595 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6596 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6597 | }; |
| 6598 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6599 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6600 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6601 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6602 | if (!FD) return; |
| 6603 | |
| 6604 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6605 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6606 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6607 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 6608 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6609 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 6610 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 6611 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6612 | // Convert the reqd_work_group_size() attributes to metadata. |
| 6613 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6614 | llvm::NamedMDNode *OpenCLMetadata = |
| 6615 | M.getModule().getOrInsertNamedMetadata( |
| 6616 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6617 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6618 | SmallVector<llvm::Metadata *, 5> Operands; |
| 6619 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6620 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6621 | Operands.push_back( |
| 6622 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6623 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 6624 | Operands.push_back( |
| 6625 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6626 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 6627 | Operands.push_back( |
| 6628 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6629 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6630 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6631 | // Add a boolean constant operand for "required" (true) or "hint" |
| 6632 | // (false) for implementing the work_group_size_hint attr later. |
| 6633 | // 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] | 6634 | Operands.push_back( |
| 6635 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6636 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 6637 | } |
| 6638 | } |
| 6639 | } |
| 6640 | } |
| 6641 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6642 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6643 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6644 | //===----------------------------------------------------------------------===// |
| 6645 | // Hexagon ABI Implementation |
| 6646 | //===----------------------------------------------------------------------===// |
| 6647 | |
| 6648 | namespace { |
| 6649 | |
| 6650 | class HexagonABIInfo : public ABIInfo { |
| 6651 | |
| 6652 | |
| 6653 | public: |
| 6654 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6655 | |
| 6656 | private: |
| 6657 | |
| 6658 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6659 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 6660 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6661 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6662 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6663 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6664 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6665 | }; |
| 6666 | |
| 6667 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6668 | public: |
| 6669 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6670 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 6671 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6672 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6673 | return 29; |
| 6674 | } |
| 6675 | }; |
| 6676 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6677 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6678 | |
| 6679 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6680 | if (!getCXXABI().classifyReturnType(FI)) |
| 6681 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6682 | for (auto &I : FI.arguments()) |
| 6683 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6684 | } |
| 6685 | |
| 6686 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 6687 | if (!isAggregateTypeForABI(Ty)) { |
| 6688 | // Treat an enum type as its underlying type. |
| 6689 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6690 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6691 | |
| 6692 | return (Ty->isPromotableIntegerType() ? |
| 6693 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6694 | } |
| 6695 | |
| 6696 | // Ignore empty records. |
| 6697 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6698 | return ABIArgInfo::getIgnore(); |
| 6699 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6700 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6701 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6702 | |
| 6703 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6704 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6705 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6706 | // Pass in the smallest viable integer type. |
| 6707 | else if (Size > 32) |
| 6708 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6709 | else if (Size > 16) |
| 6710 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6711 | else if (Size > 8) |
| 6712 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6713 | else |
| 6714 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6715 | } |
| 6716 | |
| 6717 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6718 | if (RetTy->isVoidType()) |
| 6719 | return ABIArgInfo::getIgnore(); |
| 6720 | |
| 6721 | // Large vector types should be returned via memory. |
| 6722 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6723 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6724 | |
| 6725 | if (!isAggregateTypeForABI(RetTy)) { |
| 6726 | // Treat an enum type as its underlying type. |
| 6727 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6728 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6729 | |
| 6730 | return (RetTy->isPromotableIntegerType() ? |
| 6731 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6732 | } |
| 6733 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6734 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6735 | return ABIArgInfo::getIgnore(); |
| 6736 | |
| 6737 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6738 | // are returned indirectly. |
| 6739 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6740 | if (Size <= 64) { |
| 6741 | // Return in the smallest viable integer type. |
| 6742 | if (Size <= 8) |
| 6743 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6744 | if (Size <= 16) |
| 6745 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6746 | if (Size <= 32) |
| 6747 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6748 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6749 | } |
| 6750 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6751 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6752 | } |
| 6753 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6754 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6755 | QualType Ty) const { |
| 6756 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 6757 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6758 | getContext().getTypeInfoInChars(Ty), |
| 6759 | CharUnits::fromQuantity(4), |
| 6760 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6761 | } |
| 6762 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6763 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6764 | // Lanai ABI Implementation |
| 6765 | //===----------------------------------------------------------------------===// |
| 6766 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 6767 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6768 | class LanaiABIInfo : public DefaultABIInfo { |
| 6769 | public: |
| 6770 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 6771 | |
| 6772 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 6773 | |
| 6774 | void computeInfo(CGFunctionInfo &FI) const override { |
| 6775 | CCState State(FI.getCallingConvention()); |
| 6776 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 6777 | // regparm attribute set. |
| 6778 | if (FI.getHasRegParm()) { |
| 6779 | State.FreeRegs = FI.getRegParm(); |
| 6780 | } else { |
| 6781 | State.FreeRegs = 4; |
| 6782 | } |
| 6783 | |
| 6784 | if (!getCXXABI().classifyReturnType(FI)) |
| 6785 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 6786 | for (auto &I : FI.arguments()) |
| 6787 | I.info = classifyArgumentType(I.type, State); |
| 6788 | } |
| 6789 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6790 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6791 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 6792 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 6793 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6794 | |
| 6795 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 6796 | unsigned Size = getContext().getTypeSize(Ty); |
| 6797 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 6798 | |
| 6799 | if (SizeInRegs == 0) |
| 6800 | return false; |
| 6801 | |
| 6802 | if (SizeInRegs > State.FreeRegs) { |
| 6803 | State.FreeRegs = 0; |
| 6804 | return false; |
| 6805 | } |
| 6806 | |
| 6807 | State.FreeRegs -= SizeInRegs; |
| 6808 | |
| 6809 | return true; |
| 6810 | } |
| 6811 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6812 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 6813 | CCState &State) const { |
| 6814 | if (!ByVal) { |
| 6815 | if (State.FreeRegs) { |
| 6816 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 6817 | return getNaturalAlignIndirectInReg(Ty); |
| 6818 | } |
| 6819 | return getNaturalAlignIndirect(Ty, false); |
| 6820 | } |
| 6821 | |
| 6822 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 6823 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6824 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 6825 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 6826 | /*Realign=*/TypeAlign > |
| 6827 | MinABIStackAlignInBytes); |
| 6828 | } |
| 6829 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6830 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 6831 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6832 | // Check with the C++ ABI first. |
| 6833 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 6834 | if (RT) { |
| 6835 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 6836 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 6837 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 6838 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 6839 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 6840 | } |
| 6841 | } |
| 6842 | |
| 6843 | if (isAggregateTypeForABI(Ty)) { |
| 6844 | // Structures with flexible arrays are always indirect. |
| 6845 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 6846 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 6847 | |
| 6848 | // Ignore empty structs/unions. |
| 6849 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6850 | return ABIArgInfo::getIgnore(); |
| 6851 | |
| 6852 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 6853 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 6854 | if (SizeInRegs <= State.FreeRegs) { |
| 6855 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 6856 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 6857 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 6858 | State.FreeRegs -= SizeInRegs; |
| 6859 | return ABIArgInfo::getDirectInReg(Result); |
| 6860 | } else { |
| 6861 | State.FreeRegs = 0; |
| 6862 | } |
| 6863 | return getIndirectResult(Ty, true, State); |
| 6864 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6865 | |
| 6866 | // Treat an enum type as its underlying type. |
| 6867 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 6868 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6869 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6870 | bool InReg = shouldUseInReg(Ty, State); |
| 6871 | if (Ty->isPromotableIntegerType()) { |
| 6872 | if (InReg) |
| 6873 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6874 | return ABIArgInfo::getExtend(); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6875 | } |
| 6876 | if (InReg) |
| 6877 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6878 | return ABIArgInfo::getDirect(); |
| 6879 | } |
| 6880 | |
| 6881 | namespace { |
| 6882 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6883 | public: |
| 6884 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 6885 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 6886 | }; |
| 6887 | } |
| 6888 | |
| 6889 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6890 | // AMDGPU ABI Implementation |
| 6891 | //===----------------------------------------------------------------------===// |
| 6892 | |
| 6893 | namespace { |
| 6894 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 6895 | class AMDGPUABIInfo final : public DefaultABIInfo { |
| 6896 | public: |
| 6897 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 6898 | |
| 6899 | private: |
| 6900 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6901 | |
| 6902 | void computeInfo(CGFunctionInfo &FI) const override; |
| 6903 | }; |
| 6904 | |
| 6905 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6906 | if (!getCXXABI().classifyReturnType(FI)) |
| 6907 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 6908 | |
| 6909 | unsigned CC = FI.getCallingConvention(); |
| 6910 | for (auto &Arg : FI.arguments()) |
| 6911 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) |
| 6912 | Arg.info = classifyArgumentType(Arg.type); |
| 6913 | else |
| 6914 | Arg.info = DefaultABIInfo::classifyArgumentType(Arg.type); |
| 6915 | } |
| 6916 | |
| 6917 | /// \brief Classify argument of given type \p Ty. |
| 6918 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty) const { |
| 6919 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6920 | if (!StrTy) { |
| 6921 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 6922 | } |
| 6923 | |
| 6924 | // Coerce single element structs to its element. |
| 6925 | if (StrTy->getNumElements() == 1) { |
| 6926 | return ABIArgInfo::getDirect(); |
| 6927 | } |
| 6928 | |
| 6929 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 6930 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 6931 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 6932 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 6933 | } |
| 6934 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6935 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6936 | public: |
| 6937 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 6938 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6939 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6940 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 6941 | unsigned getOpenCLKernelCallingConv() const override; |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6942 | }; |
| 6943 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6944 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6945 | |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 6946 | static void appendOpenCLVersionMD (CodeGen::CodeGenModule &CGM); |
| 6947 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6948 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame^] | 6949 | const Decl *D, |
| 6950 | llvm::GlobalValue *GV, |
| 6951 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6952 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6953 | if (!FD) |
| 6954 | return; |
| 6955 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame^] | 6956 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6957 | |
| 6958 | if (const auto *Attr = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) { |
| 6959 | unsigned Min = Attr->getMin(); |
| 6960 | unsigned Max = Attr->getMax(); |
| 6961 | |
| 6962 | if (Min != 0) { |
| 6963 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 6964 | |
| 6965 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 6966 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 6967 | } else |
| 6968 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6969 | } |
| 6970 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame^] | 6971 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 6972 | unsigned Min = Attr->getMin(); |
| 6973 | unsigned Max = Attr->getMax(); |
| 6974 | |
| 6975 | if (Min != 0) { |
| 6976 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 6977 | |
| 6978 | std::string AttrVal = llvm::utostr(Min); |
| 6979 | if (Max != 0) |
| 6980 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 6981 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 6982 | } else |
| 6983 | assert(Max == 0 && "Max must be zero"); |
| 6984 | } |
| 6985 | |
| 6986 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6987 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame^] | 6988 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6989 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame^] | 6990 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 6991 | } |
| 6992 | |
| 6993 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6994 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6995 | |
| 6996 | if (NumVGPR != 0) |
| 6997 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6998 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6999 | |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7000 | appendOpenCLVersionMD(M); |
| 7001 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7002 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7003 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7004 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7005 | } |
| 7006 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7007 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 7008 | // SPARC v8 ABI Implementation. |
| 7009 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7010 | // |
| 7011 | // Ensures that complex values are passed in registers. |
| 7012 | // |
| 7013 | namespace { |
| 7014 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 7015 | public: |
| 7016 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7017 | |
| 7018 | private: |
| 7019 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7020 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7021 | }; |
| 7022 | } // end anonymous namespace |
| 7023 | |
| 7024 | |
| 7025 | ABIArgInfo |
| 7026 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 7027 | if (Ty->isAnyComplexType()) { |
| 7028 | return ABIArgInfo::getDirect(); |
| 7029 | } |
| 7030 | else { |
| 7031 | return DefaultABIInfo::classifyReturnType(Ty); |
| 7032 | } |
| 7033 | } |
| 7034 | |
| 7035 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7036 | |
| 7037 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7038 | for (auto &Arg : FI.arguments()) |
| 7039 | Arg.info = classifyArgumentType(Arg.type); |
| 7040 | } |
| 7041 | |
| 7042 | namespace { |
| 7043 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7044 | public: |
| 7045 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7046 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 7047 | }; |
| 7048 | } // end anonymous namespace |
| 7049 | |
| 7050 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7051 | // SPARC v9 ABI Implementation. |
| 7052 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7053 | // |
| 7054 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 7055 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 7056 | // the array, structs larger than 16 bytes are passed indirectly. |
| 7057 | // |
| 7058 | // One case requires special care: |
| 7059 | // |
| 7060 | // struct mixed { |
| 7061 | // int i; |
| 7062 | // float f; |
| 7063 | // }; |
| 7064 | // |
| 7065 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 7066 | // parameter array, but the int is passed in an integer register, and the float |
| 7067 | // is passed in a floating point register. This is represented as two arguments |
| 7068 | // with the LLVM IR inreg attribute: |
| 7069 | // |
| 7070 | // declare void f(i32 inreg %i, float inreg %f) |
| 7071 | // |
| 7072 | // The code generator will only allocate 4 bytes from the parameter array for |
| 7073 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 7074 | // bytes. |
| 7075 | // |
| 7076 | namespace { |
| 7077 | class SparcV9ABIInfo : public ABIInfo { |
| 7078 | public: |
| 7079 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7080 | |
| 7081 | private: |
| 7082 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7083 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7084 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7085 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7086 | |
| 7087 | // Coercion type builder for structs passed in registers. The coercion type |
| 7088 | // serves two purposes: |
| 7089 | // |
| 7090 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 7091 | // in registers. |
| 7092 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 7093 | // code generator knows to pass them in floating point registers. |
| 7094 | // |
| 7095 | // We also compute the InReg flag which indicates that the struct contains |
| 7096 | // aligned 32-bit floats. |
| 7097 | // |
| 7098 | struct CoerceBuilder { |
| 7099 | llvm::LLVMContext &Context; |
| 7100 | const llvm::DataLayout &DL; |
| 7101 | SmallVector<llvm::Type*, 8> Elems; |
| 7102 | uint64_t Size; |
| 7103 | bool InReg; |
| 7104 | |
| 7105 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 7106 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 7107 | |
| 7108 | // Pad Elems with integers until Size is ToSize. |
| 7109 | void pad(uint64_t ToSize) { |
| 7110 | assert(ToSize >= Size && "Cannot remove elements"); |
| 7111 | if (ToSize == Size) |
| 7112 | return; |
| 7113 | |
| 7114 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7115 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7116 | if (Aligned > Size && Aligned <= ToSize) { |
| 7117 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 7118 | Size = Aligned; |
| 7119 | } |
| 7120 | |
| 7121 | // Add whole 64-bit words. |
| 7122 | while (Size + 64 <= ToSize) { |
| 7123 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 7124 | Size += 64; |
| 7125 | } |
| 7126 | |
| 7127 | // Final in-word padding. |
| 7128 | if (Size < ToSize) { |
| 7129 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 7130 | Size = ToSize; |
| 7131 | } |
| 7132 | } |
| 7133 | |
| 7134 | // Add a floating point element at Offset. |
| 7135 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 7136 | // Unaligned floats are treated as integers. |
| 7137 | if (Offset % Bits) |
| 7138 | return; |
| 7139 | // The InReg flag is only required if there are any floats < 64 bits. |
| 7140 | if (Bits < 64) |
| 7141 | InReg = true; |
| 7142 | pad(Offset); |
| 7143 | Elems.push_back(Ty); |
| 7144 | Size = Offset + Bits; |
| 7145 | } |
| 7146 | |
| 7147 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 7148 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 7149 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 7150 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 7151 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 7152 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 7153 | switch (ElemTy->getTypeID()) { |
| 7154 | case llvm::Type::StructTyID: |
| 7155 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 7156 | break; |
| 7157 | case llvm::Type::FloatTyID: |
| 7158 | addFloat(ElemOffset, ElemTy, 32); |
| 7159 | break; |
| 7160 | case llvm::Type::DoubleTyID: |
| 7161 | addFloat(ElemOffset, ElemTy, 64); |
| 7162 | break; |
| 7163 | case llvm::Type::FP128TyID: |
| 7164 | addFloat(ElemOffset, ElemTy, 128); |
| 7165 | break; |
| 7166 | case llvm::Type::PointerTyID: |
| 7167 | if (ElemOffset % 64 == 0) { |
| 7168 | pad(ElemOffset); |
| 7169 | Elems.push_back(ElemTy); |
| 7170 | Size += 64; |
| 7171 | } |
| 7172 | break; |
| 7173 | default: |
| 7174 | break; |
| 7175 | } |
| 7176 | } |
| 7177 | } |
| 7178 | |
| 7179 | // Check if Ty is a usable substitute for the coercion type. |
| 7180 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 7181 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7182 | } |
| 7183 | |
| 7184 | // Get the coercion type as a literal struct type. |
| 7185 | llvm::Type *getType() const { |
| 7186 | if (Elems.size() == 1) |
| 7187 | return Elems.front(); |
| 7188 | else |
| 7189 | return llvm::StructType::get(Context, Elems); |
| 7190 | } |
| 7191 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7192 | }; |
| 7193 | } // end anonymous namespace |
| 7194 | |
| 7195 | ABIArgInfo |
| 7196 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 7197 | if (Ty->isVoidType()) |
| 7198 | return ABIArgInfo::getIgnore(); |
| 7199 | |
| 7200 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7201 | |
| 7202 | // Anything too big to fit in registers is passed with an explicit indirect |
| 7203 | // pointer / sret pointer. |
| 7204 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7205 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7206 | |
| 7207 | // Treat an enum type as its underlying type. |
| 7208 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7209 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7210 | |
| 7211 | // Integer types smaller than a register are extended. |
| 7212 | if (Size < 64 && Ty->isIntegerType()) |
| 7213 | return ABIArgInfo::getExtend(); |
| 7214 | |
| 7215 | // Other non-aggregates go in registers. |
| 7216 | if (!isAggregateTypeForABI(Ty)) |
| 7217 | return ABIArgInfo::getDirect(); |
| 7218 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7219 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 7220 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 7221 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7222 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7223 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7224 | // 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] | 7225 | // Build a coercion type from the LLVM struct type. |
| 7226 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7227 | if (!StrTy) |
| 7228 | return ABIArgInfo::getDirect(); |
| 7229 | |
| 7230 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 7231 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7232 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7233 | |
| 7234 | // Try to use the original type for coercion. |
| 7235 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 7236 | |
| 7237 | if (CB.InReg) |
| 7238 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 7239 | else |
| 7240 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7241 | } |
| 7242 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7243 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7244 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7245 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 7246 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7247 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7248 | AI.setCoerceToType(ArgTy); |
| 7249 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7250 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7251 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7252 | CGBuilderTy &Builder = CGF.Builder; |
| 7253 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 7254 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 7255 | |
| 7256 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 7257 | |
| 7258 | Address ArgAddr = Address::invalid(); |
| 7259 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7260 | switch (AI.getKind()) { |
| 7261 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 7262 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 7263 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7264 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 7265 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7266 | case ABIArgInfo::Extend: { |
| 7267 | Stride = SlotSize; |
| 7268 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 7269 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7270 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7271 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7272 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7273 | case ABIArgInfo::Direct: { |
| 7274 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7275 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7276 | ArgAddr = Addr; |
| 7277 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7278 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7279 | |
| 7280 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7281 | Stride = SlotSize; |
| 7282 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 7283 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 7284 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7285 | break; |
| 7286 | |
| 7287 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7288 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7289 | } |
| 7290 | |
| 7291 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7292 | llvm::Value *NextPtr = |
| 7293 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 7294 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7295 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7296 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7297 | } |
| 7298 | |
| 7299 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7300 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7301 | for (auto &I : FI.arguments()) |
| 7302 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7303 | } |
| 7304 | |
| 7305 | namespace { |
| 7306 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7307 | public: |
| 7308 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7309 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7310 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7311 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7312 | return 14; |
| 7313 | } |
| 7314 | |
| 7315 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7316 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7317 | }; |
| 7318 | } // end anonymous namespace |
| 7319 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7320 | bool |
| 7321 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7322 | llvm::Value *Address) const { |
| 7323 | // This is calculated from the LLVM and GCC tables and verified |
| 7324 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 7325 | |
| 7326 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 7327 | |
| 7328 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 7329 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 7330 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 7331 | |
| 7332 | // 0-31: the 8-byte general-purpose registers |
| 7333 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 7334 | |
| 7335 | // 32-63: f0-31, the 4-byte floating-point registers |
| 7336 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 7337 | |
| 7338 | // Y = 64 |
| 7339 | // PSR = 65 |
| 7340 | // WIM = 66 |
| 7341 | // TBR = 67 |
| 7342 | // PC = 68 |
| 7343 | // NPC = 69 |
| 7344 | // FSR = 70 |
| 7345 | // CSR = 71 |
| 7346 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7347 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7348 | // 72-87: d0-15, the 8-byte floating-point registers |
| 7349 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 7350 | |
| 7351 | return false; |
| 7352 | } |
| 7353 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7354 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7355 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7356 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7357 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7358 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7359 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7360 | |
| 7361 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 7362 | /// it by reference between functions that append to it. |
| 7363 | typedef llvm::SmallString<128> SmallStringEnc; |
| 7364 | |
| 7365 | /// TypeStringCache caches the meta encodings of Types. |
| 7366 | /// |
| 7367 | /// The reason for caching TypeStrings is two fold: |
| 7368 | /// 1. To cache a type's encoding for later uses; |
| 7369 | /// 2. As a means to break recursive member type inclusion. |
| 7370 | /// |
| 7371 | /// A cache Entry can have a Status of: |
| 7372 | /// NonRecursive: The type encoding is not recursive; |
| 7373 | /// Recursive: The type encoding is recursive; |
| 7374 | /// Incomplete: An incomplete TypeString; |
| 7375 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 7376 | /// Recursive type encoding. |
| 7377 | /// |
| 7378 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 7379 | /// as possible. Whilst it may contain types which are recursive, the type |
| 7380 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 7381 | /// the type is encountered. |
| 7382 | /// |
| 7383 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 7384 | /// possible. The type itself is recursive and it may contain other types which |
| 7385 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 7386 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 7387 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 7388 | /// |
| 7389 | /// An Incomplete entry is always a RecordType and only encodes its |
| 7390 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 7391 | /// are placed into the cache during type expansion as a means to identify and |
| 7392 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 7393 | /// the entry becomes IncompleteUsed. |
| 7394 | /// |
| 7395 | /// During the expansion of a RecordType's members: |
| 7396 | /// |
| 7397 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 7398 | /// cached encoding is used; |
| 7399 | /// |
| 7400 | /// If the cache contains a Recursive encoding for the member type, the |
| 7401 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 7402 | /// |
| 7403 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 7404 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 7405 | /// |
| 7406 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 7407 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 7408 | /// it is swapped back in; |
| 7409 | /// |
| 7410 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 7411 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 7412 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 7413 | /// |
| 7414 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 7415 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 7416 | /// Else the member is part of a recursive type and thus the recursion has |
| 7417 | /// been exited too soon for the encoding to be correct for the member. |
| 7418 | /// |
| 7419 | class TypeStringCache { |
| 7420 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 7421 | struct Entry { |
| 7422 | std::string Str; // The encoded TypeString for the type. |
| 7423 | enum Status State; // Information about the encoding in 'Str'. |
| 7424 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 7425 | // during the expansion of RecordType's members. |
| 7426 | }; |
| 7427 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 7428 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 7429 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 7430 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 7431 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7432 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 7433 | bool removeIncomplete(const IdentifierInfo *ID); |
| 7434 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7435 | bool IsRecursive); |
| 7436 | StringRef lookupStr(const IdentifierInfo *ID); |
| 7437 | }; |
| 7438 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7439 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7440 | /// FieldEncoding is a helper for this ordering process. |
| 7441 | class FieldEncoding { |
| 7442 | bool HasName; |
| 7443 | std::string Enc; |
| 7444 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 7445 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
| 7446 | StringRef str() {return Enc.c_str();} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7447 | bool operator<(const FieldEncoding &rhs) const { |
| 7448 | if (HasName != rhs.HasName) return HasName; |
| 7449 | return Enc < rhs.Enc; |
| 7450 | } |
| 7451 | }; |
| 7452 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7453 | class XCoreABIInfo : public DefaultABIInfo { |
| 7454 | public: |
| 7455 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7456 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7457 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7458 | }; |
| 7459 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7460 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7461 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7462 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7463 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7464 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 7465 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7466 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7467 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7468 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7469 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7470 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 7471 | // TODO: this implementation is likely now redundant with the default |
| 7472 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7473 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7474 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7475 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7476 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7477 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7478 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 7479 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7480 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7481 | // Handle the argument. |
| 7482 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7483 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7484 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7485 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7486 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7487 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7488 | |
| 7489 | Address Val = Address::invalid(); |
| 7490 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7491 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7492 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 7493 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 7494 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7495 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 7496 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7497 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 7498 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7499 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7500 | case ABIArgInfo::Extend: |
| 7501 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7502 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 7503 | ArgSize = CharUnits::fromQuantity( |
| 7504 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7505 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7506 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7507 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7508 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 7509 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 7510 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7511 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7512 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7513 | |
| 7514 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7515 | if (!ArgSize.isZero()) { |
| 7516 | llvm::Value *APN = |
| 7517 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 7518 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7519 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7520 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7521 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7522 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7523 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7524 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 7525 | /// into the cache as a means to identify and break recursion. |
| 7526 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 7527 | /// be reinserted by removeIncomplete(). |
| 7528 | /// All other types of encoding should have been used rather than arriving here. |
| 7529 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 7530 | std::string StubEnc) { |
| 7531 | if (!ID) |
| 7532 | return; |
| 7533 | Entry &E = Map[ID]; |
| 7534 | assert( (E.Str.empty() || E.State == Recursive) && |
| 7535 | "Incorrectly use of addIncomplete"); |
| 7536 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 7537 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 7538 | E.Str.swap(StubEnc); |
| 7539 | E.State = Incomplete; |
| 7540 | ++IncompleteCount; |
| 7541 | } |
| 7542 | |
| 7543 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 7544 | /// must be removed from the cache. |
| 7545 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 7546 | /// Returns true if the RecordType was defined recursively. |
| 7547 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 7548 | if (!ID) |
| 7549 | return false; |
| 7550 | auto I = Map.find(ID); |
| 7551 | assert(I != Map.end() && "Entry not present"); |
| 7552 | Entry &E = I->second; |
| 7553 | assert( (E.State == Incomplete || |
| 7554 | E.State == IncompleteUsed) && |
| 7555 | "Entry must be an incomplete type"); |
| 7556 | bool IsRecursive = false; |
| 7557 | if (E.State == IncompleteUsed) { |
| 7558 | // We made use of our Incomplete encoding, thus we are recursive. |
| 7559 | IsRecursive = true; |
| 7560 | --IncompleteUsedCount; |
| 7561 | } |
| 7562 | if (E.Swapped.empty()) |
| 7563 | Map.erase(I); |
| 7564 | else { |
| 7565 | // Swap the Recursive back. |
| 7566 | E.Swapped.swap(E.Str); |
| 7567 | E.Swapped.clear(); |
| 7568 | E.State = Recursive; |
| 7569 | } |
| 7570 | --IncompleteCount; |
| 7571 | return IsRecursive; |
| 7572 | } |
| 7573 | |
| 7574 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 7575 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 7576 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7577 | bool IsRecursive) { |
| 7578 | if (!ID || IncompleteUsedCount) |
| 7579 | return; // No key or it is is an incomplete sub-type so don't add. |
| 7580 | Entry &E = Map[ID]; |
| 7581 | if (IsRecursive && !E.Str.empty()) { |
| 7582 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 7583 | "This is not the same Recursive entry"); |
| 7584 | // The parent container was not recursive after all, so we could have used |
| 7585 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 7586 | // we started viz: IncompleteCount!=0. |
| 7587 | return; |
| 7588 | } |
| 7589 | assert(E.Str.empty() && "Entry already present"); |
| 7590 | E.Str = Str.str(); |
| 7591 | E.State = IsRecursive? Recursive : NonRecursive; |
| 7592 | } |
| 7593 | |
| 7594 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 7595 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 7596 | /// encoding is Recursive, return an empty StringRef. |
| 7597 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 7598 | if (!ID) |
| 7599 | return StringRef(); // We have no key. |
| 7600 | auto I = Map.find(ID); |
| 7601 | if (I == Map.end()) |
| 7602 | return StringRef(); // We have no encoding. |
| 7603 | Entry &E = I->second; |
| 7604 | if (E.State == Recursive && IncompleteCount) |
| 7605 | return StringRef(); // We don't use Recursive encodings for member types. |
| 7606 | |
| 7607 | if (E.State == Incomplete) { |
| 7608 | // The incomplete type is being used to break out of recursion. |
| 7609 | E.State = IncompleteUsed; |
| 7610 | ++IncompleteUsedCount; |
| 7611 | } |
| 7612 | return E.Str.c_str(); |
| 7613 | } |
| 7614 | |
| 7615 | /// The XCore ABI includes a type information section that communicates symbol |
| 7616 | /// type information to the linker. The linker uses this information to verify |
| 7617 | /// safety/correctness of things such as array bound and pointers et al. |
| 7618 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 7619 | /// This type information (TypeString) is emitted into meta data for all global |
| 7620 | /// symbols: definitions, declarations, functions & variables. |
| 7621 | /// |
| 7622 | /// The TypeString carries type, qualifier, name, size & value details. |
| 7623 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7624 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7625 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 7626 | /// |
| 7627 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7628 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 7629 | |
| 7630 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 7631 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7632 | CodeGen::CodeGenModule &CGM) const { |
| 7633 | SmallStringEnc Enc; |
| 7634 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 7635 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 7636 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 7637 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7638 | llvm::NamedMDNode *MD = |
| 7639 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 7640 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 7641 | } |
| 7642 | } |
| 7643 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 7644 | //===----------------------------------------------------------------------===// |
| 7645 | // SPIR ABI Implementation |
| 7646 | //===----------------------------------------------------------------------===// |
| 7647 | |
| 7648 | namespace { |
| 7649 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7650 | public: |
| 7651 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7652 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
| 7653 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7654 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7655 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 7656 | }; |
| 7657 | } // End anonymous namespace. |
| 7658 | |
| 7659 | /// Emit SPIR specific metadata: OpenCL and SPIR version. |
| 7660 | void SPIRTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7661 | CodeGen::CodeGenModule &CGM) const { |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 7662 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
| 7663 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
| 7664 | llvm::Module &M = CGM.getModule(); |
| 7665 | // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the |
| 7666 | // opencl.spir.version named metadata. |
| 7667 | llvm::Metadata *SPIRVerElts[] = { |
| 7668 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 2)), |
| 7669 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 0))}; |
| 7670 | llvm::NamedMDNode *SPIRVerMD = |
| 7671 | M.getOrInsertNamedMetadata("opencl.spir.version"); |
| 7672 | SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts)); |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7673 | appendOpenCLVersionMD(CGM); |
| 7674 | } |
| 7675 | |
Matt Arsenault | 8afb5cd | 2016-09-07 07:07:59 +0000 | [diff] [blame] | 7676 | static void appendOpenCLVersionMD(CodeGen::CodeGenModule &CGM) { |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7677 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
| 7678 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
| 7679 | llvm::Module &M = CGM.getModule(); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 7680 | // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the |
| 7681 | // opencl.ocl.version named metadata node. |
| 7682 | llvm::Metadata *OCLVerElts[] = { |
| 7683 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 7684 | Int32Ty, CGM.getLangOpts().OpenCLVersion / 100)), |
| 7685 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 7686 | Int32Ty, (CGM.getLangOpts().OpenCLVersion % 100) / 10))}; |
| 7687 | llvm::NamedMDNode *OCLVerMD = |
| 7688 | M.getOrInsertNamedMetadata("opencl.ocl.version"); |
| 7689 | OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts)); |
| 7690 | } |
| 7691 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7692 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7693 | return llvm::CallingConv::SPIR_KERNEL; |
| 7694 | } |
| 7695 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7696 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7697 | const CodeGen::CodeGenModule &CGM, |
| 7698 | TypeStringCache &TSC); |
| 7699 | |
| 7700 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7701 | /// Builds a SmallVector containing the encoded field types in declaration |
| 7702 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7703 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 7704 | const RecordDecl *RD, |
| 7705 | const CodeGen::CodeGenModule &CGM, |
| 7706 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7707 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7708 | SmallStringEnc Enc; |
| 7709 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7710 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7711 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7712 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7713 | Enc += "b("; |
| 7714 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7715 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7716 | Enc += ':'; |
| 7717 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7718 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7719 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7720 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7721 | Enc += ')'; |
| 7722 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 7723 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7724 | } |
| 7725 | return true; |
| 7726 | } |
| 7727 | |
| 7728 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 7729 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 7730 | /// Union types have their fields ordered according to the ABI. |
| 7731 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 7732 | const CodeGen::CodeGenModule &CGM, |
| 7733 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 7734 | // Append the cached TypeString if we have one. |
| 7735 | StringRef TypeString = TSC.lookupStr(ID); |
| 7736 | if (!TypeString.empty()) { |
| 7737 | Enc += TypeString; |
| 7738 | return true; |
| 7739 | } |
| 7740 | |
| 7741 | // Start to emit an incomplete TypeString. |
| 7742 | size_t Start = Enc.size(); |
| 7743 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 7744 | Enc += '('; |
| 7745 | if (ID) |
| 7746 | Enc += ID->getName(); |
| 7747 | Enc += "){"; |
| 7748 | |
| 7749 | // We collect all encoded fields and order as necessary. |
| 7750 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7751 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 7752 | if (RD && !RD->field_empty()) { |
| 7753 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 7754 | // so that recursive calls to this RecordType will use it whilst building a |
| 7755 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7756 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7757 | std::string StubEnc(Enc.substr(Start).str()); |
| 7758 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 7759 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 7760 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 7761 | (void) TSC.removeIncomplete(ID); |
| 7762 | return false; |
| 7763 | } |
| 7764 | IsRecursive = TSC.removeIncomplete(ID); |
| 7765 | // The ABI requires unions to be sorted but not structures. |
| 7766 | // See FieldEncoding::operator< for sort algorithm. |
| 7767 | if (RT->isUnionType()) |
| 7768 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7769 | // We can now complete the TypeString. |
| 7770 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7771 | for (unsigned I = 0; I != E; ++I) { |
| 7772 | if (I) |
| 7773 | Enc += ','; |
| 7774 | Enc += FE[I].str(); |
| 7775 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7776 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7777 | Enc += '}'; |
| 7778 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 7779 | return true; |
| 7780 | } |
| 7781 | |
| 7782 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 7783 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 7784 | TypeStringCache &TSC, |
| 7785 | const IdentifierInfo *ID) { |
| 7786 | // Append the cached TypeString if we have one. |
| 7787 | StringRef TypeString = TSC.lookupStr(ID); |
| 7788 | if (!TypeString.empty()) { |
| 7789 | Enc += TypeString; |
| 7790 | return true; |
| 7791 | } |
| 7792 | |
| 7793 | size_t Start = Enc.size(); |
| 7794 | Enc += "e("; |
| 7795 | if (ID) |
| 7796 | Enc += ID->getName(); |
| 7797 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7798 | |
| 7799 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7800 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7801 | SmallVector<FieldEncoding, 16> FE; |
| 7802 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 7803 | ++I) { |
| 7804 | SmallStringEnc EnumEnc; |
| 7805 | EnumEnc += "m("; |
| 7806 | EnumEnc += I->getName(); |
| 7807 | EnumEnc += "){"; |
| 7808 | I->getInitVal().toString(EnumEnc); |
| 7809 | EnumEnc += '}'; |
| 7810 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 7811 | } |
| 7812 | std::sort(FE.begin(), FE.end()); |
| 7813 | unsigned E = FE.size(); |
| 7814 | for (unsigned I = 0; I != E; ++I) { |
| 7815 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7816 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7817 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7818 | } |
| 7819 | } |
| 7820 | Enc += '}'; |
| 7821 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 7822 | return true; |
| 7823 | } |
| 7824 | |
| 7825 | /// Appends type's qualifier to Enc. |
| 7826 | /// This is done prior to appending the type's encoding. |
| 7827 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 7828 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 7829 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7830 | int Lookup = 0; |
| 7831 | if (QT.isConstQualified()) |
| 7832 | Lookup += 1<<0; |
| 7833 | if (QT.isRestrictQualified()) |
| 7834 | Lookup += 1<<1; |
| 7835 | if (QT.isVolatileQualified()) |
| 7836 | Lookup += 1<<2; |
| 7837 | Enc += Table[Lookup]; |
| 7838 | } |
| 7839 | |
| 7840 | /// Appends built-in types to Enc. |
| 7841 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 7842 | const char *EncType; |
| 7843 | switch (BT->getKind()) { |
| 7844 | case BuiltinType::Void: |
| 7845 | EncType = "0"; |
| 7846 | break; |
| 7847 | case BuiltinType::Bool: |
| 7848 | EncType = "b"; |
| 7849 | break; |
| 7850 | case BuiltinType::Char_U: |
| 7851 | EncType = "uc"; |
| 7852 | break; |
| 7853 | case BuiltinType::UChar: |
| 7854 | EncType = "uc"; |
| 7855 | break; |
| 7856 | case BuiltinType::SChar: |
| 7857 | EncType = "sc"; |
| 7858 | break; |
| 7859 | case BuiltinType::UShort: |
| 7860 | EncType = "us"; |
| 7861 | break; |
| 7862 | case BuiltinType::Short: |
| 7863 | EncType = "ss"; |
| 7864 | break; |
| 7865 | case BuiltinType::UInt: |
| 7866 | EncType = "ui"; |
| 7867 | break; |
| 7868 | case BuiltinType::Int: |
| 7869 | EncType = "si"; |
| 7870 | break; |
| 7871 | case BuiltinType::ULong: |
| 7872 | EncType = "ul"; |
| 7873 | break; |
| 7874 | case BuiltinType::Long: |
| 7875 | EncType = "sl"; |
| 7876 | break; |
| 7877 | case BuiltinType::ULongLong: |
| 7878 | EncType = "ull"; |
| 7879 | break; |
| 7880 | case BuiltinType::LongLong: |
| 7881 | EncType = "sll"; |
| 7882 | break; |
| 7883 | case BuiltinType::Float: |
| 7884 | EncType = "ft"; |
| 7885 | break; |
| 7886 | case BuiltinType::Double: |
| 7887 | EncType = "d"; |
| 7888 | break; |
| 7889 | case BuiltinType::LongDouble: |
| 7890 | EncType = "ld"; |
| 7891 | break; |
| 7892 | default: |
| 7893 | return false; |
| 7894 | } |
| 7895 | Enc += EncType; |
| 7896 | return true; |
| 7897 | } |
| 7898 | |
| 7899 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 7900 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 7901 | const CodeGen::CodeGenModule &CGM, |
| 7902 | TypeStringCache &TSC) { |
| 7903 | Enc += "p("; |
| 7904 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 7905 | return false; |
| 7906 | Enc += ')'; |
| 7907 | return true; |
| 7908 | } |
| 7909 | |
| 7910 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7911 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 7912 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7913 | const CodeGen::CodeGenModule &CGM, |
| 7914 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 7915 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 7916 | return false; |
| 7917 | Enc += "a("; |
| 7918 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 7919 | CAT->getSize().toStringUnsigned(Enc); |
| 7920 | else |
| 7921 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 7922 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7923 | // The Qualifiers should be attached to the type rather than the array. |
| 7924 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7925 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 7926 | return false; |
| 7927 | Enc += ')'; |
| 7928 | return true; |
| 7929 | } |
| 7930 | |
| 7931 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 7932 | /// and the arguments. |
| 7933 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 7934 | const CodeGen::CodeGenModule &CGM, |
| 7935 | TypeStringCache &TSC) { |
| 7936 | Enc += "f{"; |
| 7937 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 7938 | return false; |
| 7939 | Enc += "}("; |
| 7940 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 7941 | // N.B. we are only interested in the adjusted param types. |
| 7942 | auto I = FPT->param_type_begin(); |
| 7943 | auto E = FPT->param_type_end(); |
| 7944 | if (I != E) { |
| 7945 | do { |
| 7946 | if (!appendType(Enc, *I, CGM, TSC)) |
| 7947 | return false; |
| 7948 | ++I; |
| 7949 | if (I != E) |
| 7950 | Enc += ','; |
| 7951 | } while (I != E); |
| 7952 | if (FPT->isVariadic()) |
| 7953 | Enc += ",va"; |
| 7954 | } else { |
| 7955 | if (FPT->isVariadic()) |
| 7956 | Enc += "va"; |
| 7957 | else |
| 7958 | Enc += '0'; |
| 7959 | } |
| 7960 | } |
| 7961 | Enc += ')'; |
| 7962 | return true; |
| 7963 | } |
| 7964 | |
| 7965 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 7966 | /// type encodings. |
| 7967 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7968 | const CodeGen::CodeGenModule &CGM, |
| 7969 | TypeStringCache &TSC) { |
| 7970 | |
| 7971 | QualType QT = QType.getCanonicalType(); |
| 7972 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7973 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 7974 | // The Qualifiers should be attached to the type rather than the array. |
| 7975 | // Thus we don't call appendQualifier() here. |
| 7976 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 7977 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7978 | appendQualifier(Enc, QT); |
| 7979 | |
| 7980 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 7981 | return appendBuiltinType(Enc, BT); |
| 7982 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7983 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 7984 | return appendPointerType(Enc, PT, CGM, TSC); |
| 7985 | |
| 7986 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 7987 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 7988 | |
| 7989 | if (const RecordType *RT = QT->getAsStructureType()) |
| 7990 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7991 | |
| 7992 | if (const RecordType *RT = QT->getAsUnionType()) |
| 7993 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7994 | |
| 7995 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7996 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7997 | |
| 7998 | return false; |
| 7999 | } |
| 8000 | |
| 8001 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8002 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 8003 | if (!D) |
| 8004 | return false; |
| 8005 | |
| 8006 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8007 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 8008 | return false; |
| 8009 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 8010 | } |
| 8011 | |
| 8012 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8013 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 8014 | return false; |
| 8015 | QualType QT = VD->getType().getCanonicalType(); |
| 8016 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 8017 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8018 | // The Qualifiers should be attached to the type rather than the array. |
| 8019 | // Thus we don't call appendQualifier() here. |
| 8020 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8021 | } |
| 8022 | return appendType(Enc, QT, CGM, TSC); |
| 8023 | } |
| 8024 | return false; |
| 8025 | } |
| 8026 | |
| 8027 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8028 | //===----------------------------------------------------------------------===// |
| 8029 | // Driver code |
| 8030 | //===----------------------------------------------------------------------===// |
| 8031 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8032 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 8033 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8034 | } |
| 8035 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 8036 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8037 | if (TheTargetCodeGenInfo) |
| 8038 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8039 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8040 | // Helper to set the unique_ptr while still keeping the return value. |
| 8041 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 8042 | this->TheTargetCodeGenInfo.reset(P); |
| 8043 | return *P; |
| 8044 | }; |
| 8045 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 8046 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 8047 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8048 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8049 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8050 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 8051 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8052 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 8053 | case llvm::Triple::mips: |
| 8054 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 8055 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8056 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 8057 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8058 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 8059 | case llvm::Triple::mips64: |
| 8060 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8061 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8062 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 8063 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 8064 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8065 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 8066 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8067 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8068 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8069 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8070 | } |
| 8071 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8072 | case llvm::Triple::wasm32: |
| 8073 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8074 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8075 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8076 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 8077 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8078 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8079 | case llvm::Triple::thumbeb: { |
| 8080 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 8081 | return SetCGInfo( |
| 8082 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 8083 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8084 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8085 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 8086 | StringRef ABIStr = getTarget().getABI(); |
| 8087 | if (ABIStr == "apcs-gnu") |
| 8088 | Kind = ARMABIInfo::APCS; |
| 8089 | else if (ABIStr == "aapcs16") |
| 8090 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 8091 | else if (CodeGenOpts.FloatABI == "hard" || |
| 8092 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8093 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 8094 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8095 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8096 | Kind = ARMABIInfo::AAPCS_VFP; |
| 8097 | |
| 8098 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 8099 | } |
| 8100 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8101 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8102 | return SetCGInfo( |
| 8103 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 8104 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8105 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8106 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8107 | if (getTarget().getABI() == "elfv2") |
| 8108 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8109 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8110 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8111 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8112 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8113 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8114 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 8115 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8116 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8117 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8118 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8119 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8120 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8121 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8122 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8123 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 8124 | case llvm::Triple::nvptx: |
| 8125 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8126 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 8127 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8128 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8129 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8130 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8131 | case llvm::Triple::systemz: { |
| 8132 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8133 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8134 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 8135 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8136 | case llvm::Triple::tce: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8137 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8138 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8139 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8140 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 8141 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8142 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 8143 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 8144 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8145 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8146 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 8147 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8148 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8149 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8150 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 8151 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8152 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 8153 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8154 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8155 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8156 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8157 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8158 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8159 | X86AVXABILevel AVXLevel = |
| 8160 | (ABI == "avx512" |
| 8161 | ? X86AVXABILevel::AVX512 |
| 8162 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8163 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8164 | switch (Triple.getOS()) { |
| 8165 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8166 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 8167 | case llvm::Triple::PS4: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8168 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8169 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8170 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8171 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8172 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 8173 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8174 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 8175 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8176 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8177 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8178 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 8179 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8180 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 8181 | case llvm::Triple::sparc: |
| 8182 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8183 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8184 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8185 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8186 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8187 | case llvm::Triple::spir: |
| 8188 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8189 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8190 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8191 | } |