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 | |
| 404 | unsigned TargetCodeGenInfo::getOpenCLImageAddrSpace(CodeGen::CodeGenModule &CGM) const { |
| 405 | return CGM.getContext().getTargetAddressSpace(LangAS::opencl_global); |
| 406 | } |
| 407 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 408 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 409 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 410 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 411 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 412 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 413 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 414 | if (FD->isUnnamedBitfield()) |
| 415 | return true; |
| 416 | |
| 417 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 418 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 419 | // Constant arrays of empty records count as empty, strip them off. |
| 420 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 421 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 422 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 423 | if (AT->getSize() == 0) |
| 424 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 425 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 426 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 427 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 428 | const RecordType *RT = FT->getAs<RecordType>(); |
| 429 | if (!RT) |
| 430 | return false; |
| 431 | |
| 432 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 433 | // |
| 434 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 435 | // current ABI. |
| 436 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 437 | return false; |
| 438 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 439 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 442 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 443 | /// fields. Note that a structure with a flexible array member is not |
| 444 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 445 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 446 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 447 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 448 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 449 | const RecordDecl *RD = RT->getDecl(); |
| 450 | if (RD->hasFlexibleArrayMember()) |
| 451 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 452 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 453 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 454 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 455 | for (const auto &I : CXXRD->bases()) |
| 456 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 457 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 458 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 459 | for (const auto *I : RD->fields()) |
| 460 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 461 | return false; |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | /// isSingleElementStruct - Determine if a structure is a "single |
| 466 | /// element struct", i.e. it has exactly one non-empty field or |
| 467 | /// exactly one field which is itself a single element |
| 468 | /// struct. Structures with flexible array members are never |
| 469 | /// considered single element structs. |
| 470 | /// |
| 471 | /// \return The field declaration for the single non-empty field, if |
| 472 | /// it exists. |
| 473 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 474 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 475 | if (!RT) |
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 | |
| 478 | const RecordDecl *RD = RT->getDecl(); |
| 479 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 480 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 481 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 482 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 483 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 484 | // If this is a C++ record, check the bases first. |
| 485 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 486 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 487 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 488 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 489 | continue; |
| 490 | |
| 491 | // If we already found an element then this isn't a single-element struct. |
| 492 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 493 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 494 | |
| 495 | // If this is non-empty and not a single element struct, the composite |
| 496 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 497 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 498 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 499 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 504 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 505 | QualType FT = FD->getType(); |
| 506 | |
| 507 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 508 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 509 | continue; |
| 510 | |
| 511 | // If we already found an element then this isn't a single-element |
| 512 | // struct. |
| 513 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 514 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 515 | |
| 516 | // Treat single element arrays as the element. |
| 517 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 518 | if (AT->getSize().getZExtValue() != 1) |
| 519 | break; |
| 520 | FT = AT->getElementType(); |
| 521 | } |
| 522 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 523 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 524 | Found = FT.getTypePtr(); |
| 525 | } else { |
| 526 | Found = isSingleElementStruct(FT, Context); |
| 527 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 528 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 532 | // We don't consider a struct a single-element struct if it has |
| 533 | // padding beyond the element type. |
| 534 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 535 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 536 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 537 | return Found; |
| 538 | } |
| 539 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 540 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 541 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 542 | const ABIArgInfo &AI) { |
| 543 | // This default implementation defers to the llvm backend's va_arg |
| 544 | // instruction. It can handle only passing arguments directly |
| 545 | // (typically only handled in the backend for primitive types), or |
| 546 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 547 | // flag has ABI impact in the callee, this implementation cannot |
| 548 | // work.) |
| 549 | |
| 550 | // Only a few cases are covered here at the moment -- those needed |
| 551 | // by the default abi. |
| 552 | llvm::Value *Val; |
| 553 | |
| 554 | if (AI.isIndirect()) { |
| 555 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 556 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 557 | assert( |
| 558 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 559 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 560 | |
| 561 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 562 | CharUnits TyAlignForABI = TyInfo.second; |
| 563 | |
| 564 | llvm::Type *BaseTy = |
| 565 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 566 | llvm::Value *Addr = |
| 567 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 568 | return Address(Addr, TyAlignForABI); |
| 569 | } else { |
| 570 | assert((AI.isDirect() || AI.isExtend()) && |
| 571 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 572 | |
| 573 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 574 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 575 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 576 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 577 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 578 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 579 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 580 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 581 | |
| 582 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 583 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 584 | CGF.Builder.CreateStore(Val, Temp); |
| 585 | return Temp; |
| 586 | } |
| 587 | } |
| 588 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 589 | /// DefaultABIInfo - The default implementation for ABI specific |
| 590 | /// details. This implementation provides information which results in |
| 591 | /// self-consistent and sensible LLVM IR generation, but does not |
| 592 | /// conform to any particular ABI. |
| 593 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 594 | public: |
| 595 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 597 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 598 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 599 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 600 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 601 | if (!getCXXABI().classifyReturnType(FI)) |
| 602 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 603 | for (auto &I : FI.arguments()) |
| 604 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 605 | } |
| 606 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 607 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 608 | QualType Ty) const override { |
| 609 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 610 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 611 | }; |
| 612 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 613 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 614 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 615 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 616 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 617 | }; |
| 618 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 619 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 620 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 621 | |
| 622 | if (isAggregateTypeForABI(Ty)) { |
| 623 | // Records with non-trivial destructors/copy-constructors should not be |
| 624 | // passed by value. |
| 625 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 626 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 627 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 628 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 629 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 631 | // Treat an enum type as its underlying type. |
| 632 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 633 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 634 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 635 | return (Ty->isPromotableIntegerType() ? |
| 636 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 639 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 640 | if (RetTy->isVoidType()) |
| 641 | return ABIArgInfo::getIgnore(); |
| 642 | |
| 643 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 644 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 645 | |
| 646 | // Treat an enum type as its underlying type. |
| 647 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 648 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 649 | |
| 650 | return (RetTy->isPromotableIntegerType() ? |
| 651 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 652 | } |
| 653 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 654 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 655 | // WebAssembly ABI Implementation |
| 656 | // |
| 657 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 658 | //===----------------------------------------------------------------------===// |
| 659 | |
| 660 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 661 | public: |
| 662 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 663 | : DefaultABIInfo(CGT) {} |
| 664 | |
| 665 | private: |
| 666 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 667 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 668 | |
| 669 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 670 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 671 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 672 | void computeInfo(CGFunctionInfo &FI) const override { |
| 673 | if (!getCXXABI().classifyReturnType(FI)) |
| 674 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 675 | for (auto &Arg : FI.arguments()) |
| 676 | Arg.info = classifyArgumentType(Arg.type); |
| 677 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 678 | |
| 679 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 680 | QualType Ty) const override; |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 681 | }; |
| 682 | |
| 683 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 684 | public: |
| 685 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 686 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 687 | }; |
| 688 | |
| 689 | /// \brief Classify argument of given type \p Ty. |
| 690 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 691 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 692 | |
| 693 | if (isAggregateTypeForABI(Ty)) { |
| 694 | // Records with non-trivial destructors/copy-constructors should not be |
| 695 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 696 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 697 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 698 | // Ignore empty structs/unions. |
| 699 | if (isEmptyRecord(getContext(), Ty, true)) |
| 700 | return ABIArgInfo::getIgnore(); |
| 701 | // Lower single-element structs to just pass a regular value. TODO: We |
| 702 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 703 | // though watch out for things like bitfields. |
| 704 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 705 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | // Otherwise just do the default thing. |
| 709 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 710 | } |
| 711 | |
| 712 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 713 | if (isAggregateTypeForABI(RetTy)) { |
| 714 | // Records with non-trivial destructors/copy-constructors should not be |
| 715 | // returned by value. |
| 716 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 717 | // Ignore empty structs/unions. |
| 718 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 719 | return ABIArgInfo::getIgnore(); |
| 720 | // Lower single-element structs to just return a regular value. TODO: We |
| 721 | // could do reasonable-size multiple-element structs too, using |
| 722 | // ABIArgInfo::getDirect(). |
| 723 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 724 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | // Otherwise just do the default thing. |
| 729 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 730 | } |
| 731 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 732 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 733 | QualType Ty) const { |
| 734 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 735 | getContext().getTypeInfoInChars(Ty), |
| 736 | CharUnits::fromQuantity(4), |
| 737 | /*AllowHigherAlign=*/ true); |
| 738 | } |
| 739 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 740 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 741 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 742 | // |
| 743 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 744 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 745 | //===----------------------------------------------------------------------===// |
| 746 | |
| 747 | class PNaClABIInfo : public ABIInfo { |
| 748 | public: |
| 749 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 750 | |
| 751 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 752 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 753 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 754 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 755 | Address EmitVAArg(CodeGenFunction &CGF, |
| 756 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 757 | }; |
| 758 | |
| 759 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 760 | public: |
| 761 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 762 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 763 | }; |
| 764 | |
| 765 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 766 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 767 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 768 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 769 | for (auto &I : FI.arguments()) |
| 770 | I.info = classifyArgumentType(I.type); |
| 771 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 772 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 773 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 774 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 775 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 776 | // function classification. Structs get passed directly for varargs |
| 777 | // functions, through a rewriting transform in |
| 778 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 779 | // this target to actually support a va_arg instructions with an |
| 780 | // aggregate type, unlike other targets. |
| 781 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 784 | /// \brief Classify argument of given type \p Ty. |
| 785 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 786 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 787 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 788 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 789 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 790 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 791 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 792 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 793 | } else if (Ty->isFloatingType()) { |
| 794 | // Floating-point types don't go inreg. |
| 795 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 796 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 797 | |
| 798 | return (Ty->isPromotableIntegerType() ? |
| 799 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 803 | if (RetTy->isVoidType()) |
| 804 | return ABIArgInfo::getIgnore(); |
| 805 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 806 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 807 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 808 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 809 | |
| 810 | // Treat an enum type as its underlying type. |
| 811 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 812 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 813 | |
| 814 | return (RetTy->isPromotableIntegerType() ? |
| 815 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 816 | } |
| 817 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 818 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 819 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 820 | // 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] | 821 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 822 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 823 | IRType->getScalarSizeInBits() != 64; |
| 824 | } |
| 825 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 826 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 827 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 828 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 829 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 830 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 831 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 832 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 835 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 839 | return Ty; |
| 840 | } |
| 841 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 842 | /// Returns true if this type can be passed in SSE registers with the |
| 843 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 844 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 845 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 846 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 847 | return true; |
| 848 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 849 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 850 | // registers specially. |
| 851 | unsigned VecSize = Context.getTypeSize(VT); |
| 852 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 853 | return true; |
| 854 | } |
| 855 | return false; |
| 856 | } |
| 857 | |
| 858 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 859 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 860 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 861 | return NumMembers <= 4; |
| 862 | } |
| 863 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 864 | //===----------------------------------------------------------------------===// |
| 865 | // X86-32 ABI Implementation |
| 866 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 867 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 868 | /// \brief Similar to llvm::CCState, but for Clang. |
| 869 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 870 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 871 | |
| 872 | unsigned CC; |
| 873 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 874 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 875 | }; |
| 876 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 877 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 878 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 879 | enum Class { |
| 880 | Integer, |
| 881 | Float |
| 882 | }; |
| 883 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 884 | static const unsigned MinABIStackAlignInBytes = 4; |
| 885 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 886 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 887 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 888 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 889 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 890 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 891 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 892 | |
| 893 | static bool isRegisterSize(unsigned Size) { |
| 894 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 895 | } |
| 896 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 897 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 898 | // FIXME: Assumes vectorcall is in use. |
| 899 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 900 | } |
| 901 | |
| 902 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 903 | uint64_t NumMembers) const override { |
| 904 | // FIXME: Assumes vectorcall is in use. |
| 905 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 906 | } |
| 907 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 908 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 909 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 910 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 911 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 912 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 913 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 914 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 915 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 916 | /// \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] | 917 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 918 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 919 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 920 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 921 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 922 | /// \brief Updates the number of available free registers, returns |
| 923 | /// true if any registers were allocated. |
| 924 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 925 | |
| 926 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 927 | bool &NeedsPadding) const; |
| 928 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 929 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 930 | bool canExpandIndirectArgument(QualType Ty) const; |
| 931 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 932 | /// \brief Rewrite the function info so that all memory arguments use |
| 933 | /// inalloca. |
| 934 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 935 | |
| 936 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 937 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 938 | QualType Type) const; |
| 939 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 940 | public: |
| 941 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 942 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 943 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 944 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 945 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 946 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 947 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 948 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 949 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 950 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 951 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 952 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 953 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 954 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 955 | |
| 956 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 957 | ArrayRef<llvm::Type*> scalars, |
| 958 | bool asReturnValue) const override { |
| 959 | // LLVM's x86-32 lowering currently only assigns up to three |
| 960 | // integer registers and three fp registers. Oddly, it'll use up to |
| 961 | // four vector registers for vectors, but those can overlap with the |
| 962 | // scalar registers. |
| 963 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
| 964 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 965 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 966 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 967 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 968 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 969 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 970 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 971 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 972 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 973 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 974 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 975 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 976 | static bool isStructReturnInRegABI( |
| 977 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 978 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 979 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 980 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 981 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 982 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 983 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 984 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 985 | return 4; |
| 986 | } |
| 987 | |
| 988 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 989 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 990 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 991 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 992 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 993 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 994 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 995 | } |
| 996 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 997 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 998 | std::string &Constraints, |
| 999 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1000 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1001 | std::vector<LValue> &ResultRegDests, |
| 1002 | std::string &AsmString, |
| 1003 | unsigned NumOutputs) const override; |
| 1004 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1005 | llvm::Constant * |
| 1006 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1007 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1008 | (0x06 << 8) | // .+0x08 |
| 1009 | ('F' << 16) | |
| 1010 | ('T' << 24); |
| 1011 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1012 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1013 | |
| 1014 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1015 | return "movl\t%ebp, %ebp" |
| 1016 | "\t\t## marker for objc_retainAutoreleaseReturnValue"; |
| 1017 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1018 | }; |
| 1019 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1020 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1021 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1022 | /// Rewrite input constraint references after adding some output constraints. |
| 1023 | /// In the case where there is one output and one input and we add one output, |
| 1024 | /// we need to replace all operand references greater than or equal to 1: |
| 1025 | /// mov $0, $1 |
| 1026 | /// mov eax, $1 |
| 1027 | /// The result will be: |
| 1028 | /// mov $0, $2 |
| 1029 | /// mov eax, $2 |
| 1030 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1031 | unsigned NumNewOuts, |
| 1032 | std::string &AsmString) { |
| 1033 | std::string Buf; |
| 1034 | llvm::raw_string_ostream OS(Buf); |
| 1035 | size_t Pos = 0; |
| 1036 | while (Pos < AsmString.size()) { |
| 1037 | size_t DollarStart = AsmString.find('$', Pos); |
| 1038 | if (DollarStart == std::string::npos) |
| 1039 | DollarStart = AsmString.size(); |
| 1040 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1041 | if (DollarEnd == std::string::npos) |
| 1042 | DollarEnd = AsmString.size(); |
| 1043 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1044 | Pos = DollarEnd; |
| 1045 | size_t NumDollars = DollarEnd - DollarStart; |
| 1046 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1047 | // We have an operand reference. |
| 1048 | size_t DigitStart = Pos; |
| 1049 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1050 | if (DigitEnd == std::string::npos) |
| 1051 | DigitEnd = AsmString.size(); |
| 1052 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1053 | unsigned OperandIndex; |
| 1054 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1055 | if (OperandIndex >= FirstIn) |
| 1056 | OperandIndex += NumNewOuts; |
| 1057 | OS << OperandIndex; |
| 1058 | } else { |
| 1059 | OS << OperandStr; |
| 1060 | } |
| 1061 | Pos = DigitEnd; |
| 1062 | } |
| 1063 | } |
| 1064 | AsmString = std::move(OS.str()); |
| 1065 | } |
| 1066 | |
| 1067 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1068 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1069 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1070 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1071 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1072 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1073 | unsigned NumOutputs) const { |
| 1074 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1075 | |
| 1076 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1077 | // larger. |
| 1078 | if (!Constraints.empty()) |
| 1079 | Constraints += ','; |
| 1080 | if (RetWidth <= 32) { |
| 1081 | Constraints += "={eax}"; |
| 1082 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1083 | } else { |
| 1084 | // Use the 'A' constraint for EAX:EDX. |
| 1085 | Constraints += "=A"; |
| 1086 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1087 | } |
| 1088 | |
| 1089 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1090 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1091 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1092 | |
| 1093 | // Coerce the integer by bitcasting the return slot pointer. |
| 1094 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1095 | CoerceTy->getPointerTo())); |
| 1096 | ResultRegDests.push_back(ReturnSlot); |
| 1097 | |
| 1098 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1099 | } |
| 1100 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1101 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1102 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1103 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1104 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1105 | uint64_t Size = Context.getTypeSize(Ty); |
| 1106 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1107 | // For i386, type must be register sized. |
| 1108 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1109 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1110 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1111 | |
| 1112 | if (Ty->isVectorType()) { |
| 1113 | // 64- and 128- bit vectors inside structures are not returned in |
| 1114 | // registers. |
| 1115 | if (Size == 64 || Size == 128) |
| 1116 | return false; |
| 1117 | |
| 1118 | return true; |
| 1119 | } |
| 1120 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1121 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1122 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1123 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1124 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1125 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1126 | return true; |
| 1127 | |
| 1128 | // Arrays are treated like records. |
| 1129 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1130 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1131 | |
| 1132 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1133 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1134 | if (!RT) return false; |
| 1135 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1136 | // FIXME: Traverse bases here too. |
| 1137 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1138 | // Structure types are passed in register if all fields would be |
| 1139 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1140 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1141 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1142 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1143 | continue; |
| 1144 | |
| 1145 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1146 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1147 | return false; |
| 1148 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1149 | return true; |
| 1150 | } |
| 1151 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1152 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1153 | // Treat complex types as the element type. |
| 1154 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1155 | Ty = CTy->getElementType(); |
| 1156 | |
| 1157 | // Check for a type which we know has a simple scalar argument-passing |
| 1158 | // convention without any padding. (We're specifically looking for 32 |
| 1159 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1160 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1161 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1162 | return false; |
| 1163 | |
| 1164 | uint64_t Size = Context.getTypeSize(Ty); |
| 1165 | return Size == 32 || Size == 64; |
| 1166 | } |
| 1167 | |
| 1168 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1169 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1170 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1171 | /// optimizations. |
| 1172 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1173 | // We can only expand structure types. |
| 1174 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1175 | if (!RT) |
| 1176 | return false; |
| 1177 | const RecordDecl *RD = RT->getDecl(); |
| 1178 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1179 | if (!IsWin32StructABI ) { |
| 1180 | // On non-Windows, we have to conservatively match our old bitcode |
| 1181 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1182 | if (!CXXRD->isCLike()) |
| 1183 | return false; |
| 1184 | } else { |
| 1185 | // Don't do this for dynamic classes. |
| 1186 | if (CXXRD->isDynamicClass()) |
| 1187 | return false; |
| 1188 | // Don't do this if there are any non-empty bases. |
| 1189 | for (const CXXBaseSpecifier &Base : CXXRD->bases()) { |
| 1190 | if (!isEmptyRecord(getContext(), Base.getType(), /*AllowArrays=*/true)) |
| 1191 | return false; |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | uint64_t Size = 0; |
| 1197 | |
| 1198 | for (const auto *FD : RD->fields()) { |
| 1199 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1200 | // argument is smaller than 32-bits, expanding the struct will create |
| 1201 | // alignment padding. |
| 1202 | if (!is32Or64BitBasicType(FD->getType(), getContext())) |
| 1203 | return false; |
| 1204 | |
| 1205 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1206 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1207 | // counts as "basic" is more complicated than what we were doing previously. |
| 1208 | if (FD->isBitField()) |
| 1209 | return false; |
| 1210 | |
| 1211 | Size += getContext().getTypeSize(FD->getType()); |
| 1212 | } |
| 1213 | |
| 1214 | // We can do this if there was no alignment padding. |
| 1215 | return Size == getContext().getTypeSize(Ty); |
| 1216 | } |
| 1217 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1218 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1219 | // If the return value is indirect, then the hidden argument is consuming one |
| 1220 | // integer register. |
| 1221 | if (State.FreeRegs) { |
| 1222 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1223 | if (!IsMCUABI) |
| 1224 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1225 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1226 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1229 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1230 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1231 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1232 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1233 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1234 | const Type *Base = nullptr; |
| 1235 | uint64_t NumElts = 0; |
| 1236 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1237 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1238 | // The LLVM struct type for such an aggregate should lower properly. |
| 1239 | return ABIArgInfo::getDirect(); |
| 1240 | } |
| 1241 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1242 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1243 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1244 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1245 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1246 | |
| 1247 | // 128-bit vectors are a special case; they are returned in |
| 1248 | // registers and we need to make sure to pick a type the LLVM |
| 1249 | // backend will like. |
| 1250 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1251 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1252 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1253 | |
| 1254 | // Always return in register if it fits in a general purpose |
| 1255 | // register, or if it is 64 bits and has a single element. |
| 1256 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1257 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1258 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1259 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1260 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1261 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1265 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1266 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1267 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1268 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1269 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1270 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1271 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1272 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1273 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1274 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1275 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1276 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1277 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1278 | // Ignore empty structs/unions. |
| 1279 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1280 | return ABIArgInfo::getIgnore(); |
| 1281 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1282 | // Small structures which are register sized are generally returned |
| 1283 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1284 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1285 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1286 | |
| 1287 | // As a special-case, if the struct is a "single-element" struct, and |
| 1288 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1289 | // floating-point register. (MSVC does not apply this special case.) |
| 1290 | // We apply a similar transformation for pointer types to improve the |
| 1291 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1292 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1293 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1294 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1295 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1296 | |
| 1297 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1298 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1299 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1302 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1303 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1304 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1305 | // Treat an enum type as its underlying type. |
| 1306 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1307 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1308 | |
| 1309 | return (RetTy->isPromotableIntegerType() ? |
| 1310 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1313 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1314 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1315 | } |
| 1316 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1317 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1318 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1319 | if (!RT) |
| 1320 | return 0; |
| 1321 | const RecordDecl *RD = RT->getDecl(); |
| 1322 | |
| 1323 | // If this is a C++ record, check the bases first. |
| 1324 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1325 | for (const auto &I : CXXRD->bases()) |
| 1326 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1327 | return false; |
| 1328 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1329 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1330 | QualType FT = i->getType(); |
| 1331 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1332 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1333 | return true; |
| 1334 | |
| 1335 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1336 | return true; |
| 1337 | } |
| 1338 | |
| 1339 | return false; |
| 1340 | } |
| 1341 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1342 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1343 | unsigned Align) const { |
| 1344 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1345 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1346 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1347 | return 0; // Use default alignment. |
| 1348 | |
| 1349 | // On non-Darwin, the stack type alignment is always 4. |
| 1350 | if (!IsDarwinVectorABI) { |
| 1351 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1352 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1353 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1354 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1355 | // 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] | 1356 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1357 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1358 | return 16; |
| 1359 | |
| 1360 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1363 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1364 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1365 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1366 | if (State.FreeRegs) { |
| 1367 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1368 | if (!IsMCUABI) |
| 1369 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1370 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1371 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1372 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1373 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1374 | // Compute the byval alignment. |
| 1375 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1376 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1377 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1378 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1379 | |
| 1380 | // If the stack alignment is less than the type alignment, realign the |
| 1381 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1382 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1383 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1384 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1387 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1388 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1389 | if (!T) |
| 1390 | T = Ty.getTypePtr(); |
| 1391 | |
| 1392 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1393 | BuiltinType::Kind K = BT->getKind(); |
| 1394 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1395 | return Float; |
| 1396 | } |
| 1397 | return Integer; |
| 1398 | } |
| 1399 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1400 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1401 | if (!IsSoftFloatABI) { |
| 1402 | Class C = classify(Ty); |
| 1403 | if (C == Float) |
| 1404 | return false; |
| 1405 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1406 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1407 | unsigned Size = getContext().getTypeSize(Ty); |
| 1408 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1409 | |
| 1410 | if (SizeInRegs == 0) |
| 1411 | return false; |
| 1412 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1413 | if (!IsMCUABI) { |
| 1414 | if (SizeInRegs > State.FreeRegs) { |
| 1415 | State.FreeRegs = 0; |
| 1416 | return false; |
| 1417 | } |
| 1418 | } else { |
| 1419 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1420 | // earlier parameters that are passed on the stack. Also, |
| 1421 | // it does not allow passing >8-byte structs in-register, |
| 1422 | // even if there are 3 free registers available. |
| 1423 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1424 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1425 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1426 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1427 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1428 | return true; |
| 1429 | } |
| 1430 | |
| 1431 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
| 1432 | bool &InReg, |
| 1433 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1434 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1435 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1436 | // (HFAs) have already been dealt with at this point. |
| 1437 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1438 | return false; |
| 1439 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1440 | NeedsPadding = false; |
| 1441 | InReg = !IsMCUABI; |
| 1442 | |
| 1443 | if (!updateFreeRegs(Ty, State)) |
| 1444 | return false; |
| 1445 | |
| 1446 | if (IsMCUABI) |
| 1447 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1448 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1449 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1450 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1451 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1452 | NeedsPadding = true; |
| 1453 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1454 | return false; |
| 1455 | } |
| 1456 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1457 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1460 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1461 | if (!updateFreeRegs(Ty, State)) |
| 1462 | return false; |
| 1463 | |
| 1464 | if (IsMCUABI) |
| 1465 | return false; |
| 1466 | |
| 1467 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1468 | State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1469 | if (getContext().getTypeSize(Ty) > 32) |
| 1470 | return false; |
| 1471 | |
| 1472 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
| 1473 | Ty->isReferenceType()); |
| 1474 | } |
| 1475 | |
| 1476 | return true; |
| 1477 | } |
| 1478 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1479 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1480 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1481 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1482 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1483 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1484 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1485 | // Check with the C++ ABI first. |
| 1486 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1487 | if (RT) { |
| 1488 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1489 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1490 | return getIndirectResult(Ty, false, State); |
| 1491 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1492 | // The field index doesn't matter, we'll fix it up later. |
| 1493 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1498 | // to other targets. |
| 1499 | const Type *Base = nullptr; |
| 1500 | uint64_t NumElts = 0; |
| 1501 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1502 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1503 | if (State.FreeSSERegs >= NumElts) { |
| 1504 | State.FreeSSERegs -= NumElts; |
| 1505 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1506 | return ABIArgInfo::getDirect(); |
| 1507 | return ABIArgInfo::getExpand(); |
| 1508 | } |
| 1509 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1510 | } |
| 1511 | |
| 1512 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1513 | // Structures with flexible arrays are always indirect. |
| 1514 | // FIXME: This should not be byval! |
| 1515 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1516 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1517 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1518 | // Ignore empty structs/unions on non-Windows. |
| 1519 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1520 | return ABIArgInfo::getIgnore(); |
| 1521 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1522 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1523 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1524 | bool NeedsPadding = false; |
| 1525 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1526 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1527 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1528 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1529 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1530 | if (InReg) |
| 1531 | return ABIArgInfo::getDirectInReg(Result); |
| 1532 | else |
| 1533 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1534 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1535 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1536 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1537 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1538 | // of those arguments will match the struct. This is important because the |
| 1539 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1540 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1541 | // Don't do this for the MCU if there are still free integer registers |
| 1542 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1543 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1544 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1545 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1546 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1547 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1548 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1549 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1550 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1553 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1554 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1555 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1556 | if (IsDarwinVectorABI) { |
| 1557 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1558 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1559 | (Size == 64 && VT->getNumElements() == 1)) |
| 1560 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1561 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1562 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1563 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1564 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1565 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1566 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1567 | return ABIArgInfo::getDirect(); |
| 1568 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1569 | |
| 1570 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1571 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1572 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1573 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1574 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1575 | |
| 1576 | if (Ty->isPromotableIntegerType()) { |
| 1577 | if (InReg) |
| 1578 | return ABIArgInfo::getExtendInReg(); |
| 1579 | return ABIArgInfo::getExtend(); |
| 1580 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1581 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1582 | if (InReg) |
| 1583 | return ABIArgInfo::getDirectInReg(); |
| 1584 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1587 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1588 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1589 | if (IsMCUABI) |
| 1590 | State.FreeRegs = 3; |
| 1591 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1592 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1593 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1594 | State.FreeRegs = 2; |
| 1595 | State.FreeSSERegs = 6; |
| 1596 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1597 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1598 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1599 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1600 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1601 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1602 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1603 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1604 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1605 | // return value was sret and put it in a register ourselves if appropriate. |
| 1606 | if (State.FreeRegs) { |
| 1607 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1608 | if (!IsMCUABI) |
| 1609 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1610 | } |
| 1611 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1612 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1613 | // The chain argument effectively gives us another free register. |
| 1614 | if (FI.isChainCall()) |
| 1615 | ++State.FreeRegs; |
| 1616 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1617 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1618 | for (auto &I : FI.arguments()) { |
| 1619 | I.info = classifyArgumentType(I.type, State); |
| 1620 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1624 | // all the memory arguments to use inalloca. |
| 1625 | if (UsedInAlloca) |
| 1626 | rewriteWithInAlloca(FI); |
| 1627 | } |
| 1628 | |
| 1629 | void |
| 1630 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1631 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1632 | QualType Type) const { |
| 1633 | // Arguments are always 4-byte-aligned. |
| 1634 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1635 | |
| 1636 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1637 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1638 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1639 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1640 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1641 | // Insert padding bytes to respect alignment. |
| 1642 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1643 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1644 | if (StackOffset != FieldEnd) { |
| 1645 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1646 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1647 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1648 | FrameFields.push_back(Ty); |
| 1649 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1652 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1653 | // Leave ignored and inreg arguments alone. |
| 1654 | switch (Info.getKind()) { |
| 1655 | case ABIArgInfo::InAlloca: |
| 1656 | return true; |
| 1657 | case ABIArgInfo::Indirect: |
| 1658 | assert(Info.getIndirectByVal()); |
| 1659 | return true; |
| 1660 | case ABIArgInfo::Ignore: |
| 1661 | return false; |
| 1662 | case ABIArgInfo::Direct: |
| 1663 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1664 | if (Info.getInReg()) |
| 1665 | return false; |
| 1666 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1667 | case ABIArgInfo::Expand: |
| 1668 | case ABIArgInfo::CoerceAndExpand: |
| 1669 | // These are aggregate types which are never passed in registers when |
| 1670 | // inalloca is involved. |
| 1671 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1672 | } |
| 1673 | llvm_unreachable("invalid enum"); |
| 1674 | } |
| 1675 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1676 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1677 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1678 | |
| 1679 | // Build a packed struct type for all of the arguments in memory. |
| 1680 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1681 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1682 | // The stack alignment is always 4. |
| 1683 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1684 | |
| 1685 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1686 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1687 | |
| 1688 | // Put 'this' into the struct before 'sret', if necessary. |
| 1689 | bool IsThisCall = |
| 1690 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1691 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1692 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1693 | isArgInAlloca(I->info)) { |
| 1694 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1695 | ++I; |
| 1696 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1697 | |
| 1698 | // 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] | 1699 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1700 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1701 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1702 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1703 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1707 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1708 | ++I; |
| 1709 | |
| 1710 | // Put arguments passed in memory into the struct. |
| 1711 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1712 | if (isArgInAlloca(I->info)) |
| 1713 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1717 | /*isPacked=*/true), |
| 1718 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1721 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1722 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1723 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1724 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1725 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1726 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1727 | // |
| 1728 | // Just messing with TypeInfo like this works because we never pass |
| 1729 | // anything indirectly. |
| 1730 | TypeInfo.second = CharUnits::fromQuantity( |
| 1731 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1732 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1733 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1734 | TypeInfo, CharUnits::fromQuantity(4), |
| 1735 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1738 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1739 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1740 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1741 | |
| 1742 | switch (Opts.getStructReturnConvention()) { |
| 1743 | case CodeGenOptions::SRCK_Default: |
| 1744 | break; |
| 1745 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1746 | return false; |
| 1747 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1748 | return true; |
| 1749 | } |
| 1750 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1751 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1752 | return true; |
| 1753 | |
| 1754 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1755 | case llvm::Triple::DragonFly: |
| 1756 | case llvm::Triple::FreeBSD: |
| 1757 | case llvm::Triple::OpenBSD: |
| 1758 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1759 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1760 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1761 | default: |
| 1762 | return false; |
| 1763 | } |
| 1764 | } |
| 1765 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1766 | void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1767 | llvm::GlobalValue *GV, |
| 1768 | CodeGen::CodeGenModule &CGM) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1769 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1770 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1771 | // Get the LLVM function. |
| 1772 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1773 | |
| 1774 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1775 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1776 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1777 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1778 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1779 | llvm::AttributeSet::FunctionIndex, |
| 1780 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1781 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1782 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1783 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1784 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1785 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1789 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1790 | CodeGen::CodeGenFunction &CGF, |
| 1791 | llvm::Value *Address) const { |
| 1792 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1793 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1794 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1795 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1796 | // 0-7 are the eight integer registers; the order is different |
| 1797 | // on Darwin (for EH), but the range is the same. |
| 1798 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1799 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1800 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1801 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1802 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1803 | // These have size 16, which is sizeof(long double) on |
| 1804 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1805 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1806 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1807 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1808 | } else { |
| 1809 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1810 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1811 | Builder.CreateAlignedStore( |
| 1812 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1813 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1814 | |
| 1815 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1816 | // These have size 12, which is sizeof(long double) on |
| 1817 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1818 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1819 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1820 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1821 | |
| 1822 | return false; |
| 1823 | } |
| 1824 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1825 | //===----------------------------------------------------------------------===// |
| 1826 | // X86-64 ABI Implementation |
| 1827 | //===----------------------------------------------------------------------===// |
| 1828 | |
| 1829 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1830 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1831 | /// The AVX ABI level for X86 targets. |
| 1832 | enum class X86AVXABILevel { |
| 1833 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1834 | AVX, |
| 1835 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1836 | }; |
| 1837 | |
| 1838 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1839 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1840 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1841 | case X86AVXABILevel::AVX512: |
| 1842 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1843 | case X86AVXABILevel::AVX: |
| 1844 | return 256; |
| 1845 | case X86AVXABILevel::None: |
| 1846 | return 128; |
| 1847 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1848 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1851 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1852 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1853 | enum Class { |
| 1854 | Integer = 0, |
| 1855 | SSE, |
| 1856 | SSEUp, |
| 1857 | X87, |
| 1858 | X87Up, |
| 1859 | ComplexX87, |
| 1860 | NoClass, |
| 1861 | Memory |
| 1862 | }; |
| 1863 | |
| 1864 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1865 | /// |
| 1866 | /// Merge an accumulating classification \arg Accum with a field |
| 1867 | /// classification \arg Field. |
| 1868 | /// |
| 1869 | /// \param Accum - The accumulating classification. This should |
| 1870 | /// always be either NoClass or the result of a previous merge |
| 1871 | /// call. In addition, this should never be Memory (the caller |
| 1872 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1873 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1874 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1875 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1876 | /// |
| 1877 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1878 | /// final MEMORY or SSE classes when necessary. |
| 1879 | /// |
| 1880 | /// \param AggregateSize - The size of the current aggregate in |
| 1881 | /// the classification process. |
| 1882 | /// |
| 1883 | /// \param Lo - The classification for the parts of the type |
| 1884 | /// residing in the low word of the containing object. |
| 1885 | /// |
| 1886 | /// \param Hi - The classification for the parts of the type |
| 1887 | /// residing in the higher words of the containing object. |
| 1888 | /// |
| 1889 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1890 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1891 | /// classify - Determine the x86_64 register classes in which the |
| 1892 | /// given type T should be passed. |
| 1893 | /// |
| 1894 | /// \param Lo - The classification for the parts of the type |
| 1895 | /// residing in the low word of the containing object. |
| 1896 | /// |
| 1897 | /// \param Hi - The classification for the parts of the type |
| 1898 | /// residing in the high word of the containing object. |
| 1899 | /// |
| 1900 | /// \param OffsetBase - The bit offset of this type in the |
| 1901 | /// containing object. Some parameters are classified different |
| 1902 | /// depending on whether they straddle an eightbyte boundary. |
| 1903 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1904 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1905 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1906 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1907 | /// If a word is unused its result will be NoClass; if a type should |
| 1908 | /// be passed in Memory then at least the classification of \arg Lo |
| 1909 | /// will be Memory. |
| 1910 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1911 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1912 | /// |
| 1913 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1914 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1915 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1916 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1917 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1918 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1919 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1920 | unsigned IROffset, QualType SourceTy, |
| 1921 | unsigned SourceOffset) const; |
| 1922 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1923 | unsigned IROffset, QualType SourceTy, |
| 1924 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1925 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1926 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1927 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1928 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1929 | |
| 1930 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1931 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1932 | /// |
| 1933 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1934 | /// available. |
| 1935 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1936 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1937 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1938 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1939 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1940 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1941 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1942 | unsigned &neededSSE, |
| 1943 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1944 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1945 | bool IsIllegalVectorType(QualType Ty) const; |
| 1946 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1947 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1948 | /// unfortunately in ways that were not always consistent with |
| 1949 | /// certain previous compilers. In particular, platforms which |
| 1950 | /// required strict binary compatibility with older versions of GCC |
| 1951 | /// may need to exempt themselves. |
| 1952 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1953 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 1956 | /// GCC classifies <1 x long long> as SSE but compatibility with older clang |
| 1957 | // compilers require us to classify it as INTEGER. |
| 1958 | bool classifyIntegerMMXAsSSE() const { |
| 1959 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 1960 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 1961 | return false; |
| 1962 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 1963 | return false; |
| 1964 | return true; |
| 1965 | } |
| 1966 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1967 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1968 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1969 | // 64-bit hardware. |
| 1970 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1971 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1972 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1973 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1974 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1975 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1976 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1977 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1978 | bool isPassedUsingAVXType(QualType type) const { |
| 1979 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1980 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1981 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1982 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1983 | if (info.isDirect()) { |
| 1984 | llvm::Type *ty = info.getCoerceToType(); |
| 1985 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1986 | return (vectorTy->getBitWidth() > 128); |
| 1987 | } |
| 1988 | return false; |
| 1989 | } |
| 1990 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1991 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1992 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1993 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1994 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 1995 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1996 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1997 | |
| 1998 | bool has64BitPointers() const { |
| 1999 | return Has64BitPointers; |
| 2000 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2001 | |
| 2002 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 2003 | ArrayRef<llvm::Type*> scalars, |
| 2004 | bool asReturnValue) const override { |
| 2005 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2006 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2007 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2008 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2009 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2010 | class WinX86_64ABIInfo : public ABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2011 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2012 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
| 2013 | : ABIInfo(CGT), |
| 2014 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2015 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2016 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2017 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2018 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2019 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2020 | |
| 2021 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2022 | // FIXME: Assumes vectorcall is in use. |
| 2023 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2024 | } |
| 2025 | |
| 2026 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2027 | uint64_t NumMembers) const override { |
| 2028 | // FIXME: Assumes vectorcall is in use. |
| 2029 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2030 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2031 | |
| 2032 | private: |
| 2033 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 2034 | bool IsReturnType) const; |
| 2035 | |
| 2036 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2037 | }; |
| 2038 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2039 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2040 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2041 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2042 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2043 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2044 | const X86_64ABIInfo &getABIInfo() const { |
| 2045 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2046 | } |
| 2047 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2048 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2049 | return 7; |
| 2050 | } |
| 2051 | |
| 2052 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2053 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2054 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2055 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2056 | // 0-15 are the 16 integer registers. |
| 2057 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2058 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2059 | return false; |
| 2060 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2061 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2062 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2063 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2064 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2065 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2066 | } |
| 2067 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2068 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2069 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2070 | // The default CC on x86-64 sets %al to the number of SSA |
| 2071 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2072 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2073 | // that when AVX types are involved: the ABI explicitly states it is |
| 2074 | // undefined, and it doesn't work in practice because of how the ABI |
| 2075 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2076 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2077 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2078 | for (CallArgList::const_iterator |
| 2079 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2080 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2081 | HasAVXType = true; |
| 2082 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2083 | } |
| 2084 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2085 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2086 | if (!HasAVXType) |
| 2087 | return true; |
| 2088 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2089 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2090 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2093 | llvm::Constant * |
| 2094 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2095 | unsigned Sig; |
| 2096 | if (getABIInfo().has64BitPointers()) |
| 2097 | Sig = (0xeb << 0) | // jmp rel8 |
| 2098 | (0x0a << 8) | // .+0x0c |
| 2099 | ('F' << 16) | |
| 2100 | ('T' << 24); |
| 2101 | else |
| 2102 | Sig = (0xeb << 0) | // jmp rel8 |
| 2103 | (0x06 << 8) | // .+0x08 |
| 2104 | ('F' << 16) | |
| 2105 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2106 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2107 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2108 | |
| 2109 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2110 | CodeGen::CodeGenModule &CGM) const override { |
| 2111 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2112 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2113 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2114 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2115 | } |
| 2116 | } |
| 2117 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2118 | }; |
| 2119 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2120 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2121 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2122 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2123 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2124 | |
| 2125 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 2126 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2127 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 2128 | // If the argument contains a space, enclose it in quotes. |
| 2129 | if (Lib.find(" ") != StringRef::npos) |
| 2130 | Opt += "\"" + Lib.str() + "\""; |
| 2131 | else |
| 2132 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2133 | } |
| 2134 | }; |
| 2135 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2136 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2137 | // If the argument does not end in .lib, automatically add the suffix. |
| 2138 | // If the argument contains a space, enclose it in quotes. |
| 2139 | // This matches the behavior of MSVC. |
| 2140 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2141 | std::string ArgStr = Quote ? "\"" : ""; |
| 2142 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 2143 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2144 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2145 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2146 | return ArgStr; |
| 2147 | } |
| 2148 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2149 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2150 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2151 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2152 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2153 | unsigned NumRegisterParameters) |
| 2154 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2155 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2156 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2157 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2158 | CodeGen::CodeGenModule &CGM) const override; |
| 2159 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2160 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2161 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2162 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2163 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2164 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2165 | |
| 2166 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2167 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2168 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2169 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2170 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2171 | }; |
| 2172 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2173 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 2174 | llvm::GlobalValue *GV, |
| 2175 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 2176 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2177 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 2178 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2179 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2180 | Fn->addFnAttr("stack-probe-size", |
| 2181 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2186 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2187 | llvm::GlobalValue *GV, |
| 2188 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2189 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2190 | |
| 2191 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2192 | } |
| 2193 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2194 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2195 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2196 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2197 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2198 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2199 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2200 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2201 | CodeGen::CodeGenModule &CGM) const override; |
| 2202 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2203 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2204 | return 7; |
| 2205 | } |
| 2206 | |
| 2207 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2208 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2209 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2210 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2211 | // 0-15 are the 16 integer registers. |
| 2212 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2213 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2214 | return false; |
| 2215 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2216 | |
| 2217 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2218 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2219 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2220 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2221 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2222 | |
| 2223 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2224 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2225 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2226 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2227 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2228 | }; |
| 2229 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2230 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2231 | llvm::GlobalValue *GV, |
| 2232 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2233 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2234 | |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2235 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2236 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2237 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2238 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2239 | } |
| 2240 | } |
| 2241 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2242 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2243 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2244 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2245 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2246 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2247 | Class &Hi) const { |
| 2248 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2249 | // |
| 2250 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2251 | // memory. |
| 2252 | // |
| 2253 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2254 | // memory. |
| 2255 | // |
| 2256 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2257 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2258 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2259 | // ABI working for processors that don't support the __m256 type. |
| 2260 | // |
| 2261 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2262 | // |
| 2263 | // Some of these are enforced by the merging logic. Others can arise |
| 2264 | // only with unions; for example: |
| 2265 | // union { _Complex double; unsigned; } |
| 2266 | // |
| 2267 | // Note that clauses (b) and (c) were added in 0.98. |
| 2268 | // |
| 2269 | if (Hi == Memory) |
| 2270 | Lo = Memory; |
| 2271 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2272 | Lo = Memory; |
| 2273 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2274 | Lo = Memory; |
| 2275 | if (Hi == SSEUp && Lo != SSE) |
| 2276 | Hi = SSE; |
| 2277 | } |
| 2278 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2279 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2280 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2281 | // classified recursively so that always two fields are |
| 2282 | // considered. The resulting class is calculated according to |
| 2283 | // the classes of the fields in the eightbyte: |
| 2284 | // |
| 2285 | // (a) If both classes are equal, this is the resulting class. |
| 2286 | // |
| 2287 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2288 | // the other class. |
| 2289 | // |
| 2290 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2291 | // class. |
| 2292 | // |
| 2293 | // (d) If one of the classes is INTEGER, the result is the |
| 2294 | // INTEGER. |
| 2295 | // |
| 2296 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2297 | // MEMORY is used as class. |
| 2298 | // |
| 2299 | // (f) Otherwise class SSE is used. |
| 2300 | |
| 2301 | // Accum should never be memory (we should have returned) or |
| 2302 | // ComplexX87 (because this cannot be passed in a structure). |
| 2303 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2304 | "Invalid accumulated classification during merge."); |
| 2305 | if (Accum == Field || Field == NoClass) |
| 2306 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2307 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2308 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2309 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2310 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2311 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2312 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2313 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2314 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2315 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2316 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2319 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2320 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2321 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2322 | // Class pairs with appropriate constructor methods for the various |
| 2323 | // situations. |
| 2324 | |
| 2325 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2326 | // shouldn't be passed in registers for example, so there is no chance they |
| 2327 | // can straddle an eightbyte. Verify & simplify. |
| 2328 | |
| 2329 | Lo = Hi = NoClass; |
| 2330 | |
| 2331 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2332 | Current = Memory; |
| 2333 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2334 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2335 | BuiltinType::Kind k = BT->getKind(); |
| 2336 | |
| 2337 | if (k == BuiltinType::Void) { |
| 2338 | Current = NoClass; |
| 2339 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2340 | Lo = Integer; |
| 2341 | Hi = Integer; |
| 2342 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2343 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2344 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2345 | Current = SSE; |
| 2346 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2347 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2348 | if (LDF == &llvm::APFloat::IEEEquad) { |
| 2349 | Lo = SSE; |
| 2350 | Hi = SSEUp; |
| 2351 | } else if (LDF == &llvm::APFloat::x87DoubleExtended) { |
| 2352 | Lo = X87; |
| 2353 | Hi = X87Up; |
| 2354 | } else if (LDF == &llvm::APFloat::IEEEdouble) { |
| 2355 | Current = SSE; |
| 2356 | } else |
| 2357 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2358 | } |
| 2359 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2360 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2361 | return; |
| 2362 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2363 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2364 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2365 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2366 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2367 | return; |
| 2368 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2369 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2370 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2371 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2372 | return; |
| 2373 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2374 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2375 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2376 | if (Ty->isMemberFunctionPointerType()) { |
| 2377 | if (Has64BitPointers) { |
| 2378 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2379 | // Lo and Hi now. |
| 2380 | Lo = Hi = Integer; |
| 2381 | } else { |
| 2382 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2383 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2384 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2385 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2386 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2387 | Lo = Hi = Integer; |
| 2388 | } else { |
| 2389 | Current = Integer; |
| 2390 | } |
| 2391 | } |
| 2392 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2393 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2394 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2395 | return; |
| 2396 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2397 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2398 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2399 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2400 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2401 | // gcc passes the following as integer: |
| 2402 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2403 | // 2 bytes - <2 x char>, <1 x short> |
| 2404 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2405 | Current = Integer; |
| 2406 | |
| 2407 | // If this type crosses an eightbyte boundary, it should be |
| 2408 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2409 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2410 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2411 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2412 | Hi = Lo; |
| 2413 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2414 | QualType ElementType = VT->getElementType(); |
| 2415 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2416 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2417 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2418 | return; |
| 2419 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2420 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2421 | // pass them as integer. For platforms where clang is the de facto |
| 2422 | // platform compiler, we must continue to use integer. |
| 2423 | if (!classifyIntegerMMXAsSSE() && |
| 2424 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2425 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2426 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2427 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2428 | Current = Integer; |
| 2429 | else |
| 2430 | Current = SSE; |
| 2431 | |
| 2432 | // If this type crosses an eightbyte boundary, it should be |
| 2433 | // split. |
| 2434 | if (OffsetBase && OffsetBase != 64) |
| 2435 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2436 | } else if (Size == 128 || |
| 2437 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2438 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2439 | // least significant one belongs to class SSE and all the others to class |
| 2440 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2441 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2442 | // This design isn't correct for 256-bits, but since there're no cases |
| 2443 | // where the upper parts would need to be inspected, avoid adding |
| 2444 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2445 | // |
| 2446 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2447 | // registers if they are "named", i.e. not part of the "..." of a |
| 2448 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2449 | // |
| 2450 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2451 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2452 | Lo = SSE; |
| 2453 | Hi = SSEUp; |
| 2454 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2455 | return; |
| 2456 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2457 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2458 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2459 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2460 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2461 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2462 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2463 | if (Size <= 64) |
| 2464 | Current = Integer; |
| 2465 | else if (Size <= 128) |
| 2466 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2467 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2468 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2469 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2470 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2471 | } else if (ET == getContext().LongDoubleTy) { |
| 2472 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2473 | if (LDF == &llvm::APFloat::IEEEquad) |
| 2474 | Current = Memory; |
| 2475 | else if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 2476 | Current = ComplexX87; |
| 2477 | else if (LDF == &llvm::APFloat::IEEEdouble) |
| 2478 | Lo = Hi = SSE; |
| 2479 | else |
| 2480 | llvm_unreachable("unexpected long double representation!"); |
| 2481 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2482 | |
| 2483 | // If this complex type crosses an eightbyte boundary then it |
| 2484 | // should be split. |
| 2485 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2486 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2487 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2488 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2489 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2490 | return; |
| 2491 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2492 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2493 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2494 | // Arrays are treated like structures. |
| 2495 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2496 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2497 | |
| 2498 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2499 | // than four eightbytes, ..., it has class MEMORY. |
| 2500 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2501 | return; |
| 2502 | |
| 2503 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2504 | // fields, it has class MEMORY. |
| 2505 | // |
| 2506 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2507 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2508 | return; |
| 2509 | |
| 2510 | // Otherwise implement simplified merge. We could be smarter about |
| 2511 | // this, but it isn't worth it and would be harder to verify. |
| 2512 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2513 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2514 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2515 | |
| 2516 | // The only case a 256-bit wide vector could be used is when the array |
| 2517 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2518 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2519 | if (Size > 128 && EltSize != 256) |
| 2520 | return; |
| 2521 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2522 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2523 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2524 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2525 | Lo = merge(Lo, FieldLo); |
| 2526 | Hi = merge(Hi, FieldHi); |
| 2527 | if (Lo == Memory || Hi == Memory) |
| 2528 | break; |
| 2529 | } |
| 2530 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2531 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2532 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2533 | return; |
| 2534 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2535 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2536 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2537 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2538 | |
| 2539 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2540 | // than four eightbytes, ..., it has class MEMORY. |
| 2541 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2542 | return; |
| 2543 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2544 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2545 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2546 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2547 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2548 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2549 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2550 | const RecordDecl *RD = RT->getDecl(); |
| 2551 | |
| 2552 | // Assume variable sized types are passed in memory. |
| 2553 | if (RD->hasFlexibleArrayMember()) |
| 2554 | return; |
| 2555 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2556 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2557 | |
| 2558 | // Reset Lo class, this will be recomputed. |
| 2559 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2560 | |
| 2561 | // If this is a C++ record, classify the bases first. |
| 2562 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2563 | for (const auto &I : CXXRD->bases()) { |
| 2564 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2565 | "Unexpected base class!"); |
| 2566 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2567 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2568 | |
| 2569 | // Classify this field. |
| 2570 | // |
| 2571 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2572 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2573 | // initialized to class NO_CLASS. |
| 2574 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2575 | uint64_t Offset = |
| 2576 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2577 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2578 | Lo = merge(Lo, FieldLo); |
| 2579 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2580 | if (Lo == Memory || Hi == Memory) { |
| 2581 | postMerge(Size, Lo, Hi); |
| 2582 | return; |
| 2583 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2588 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2589 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2590 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2591 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2592 | bool BitField = i->isBitField(); |
| 2593 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2594 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2595 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2596 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2597 | // The only case a 256-bit wide vector could be used is when the struct |
| 2598 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2599 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2600 | // |
| 2601 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2602 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2603 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2604 | return; |
| 2605 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2606 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2607 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2608 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2609 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2610 | return; |
| 2611 | } |
| 2612 | |
| 2613 | // Classify this field. |
| 2614 | // |
| 2615 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2616 | // exceeds a single eightbyte, each is classified |
| 2617 | // separately. Each eightbyte gets initialized to class |
| 2618 | // NO_CLASS. |
| 2619 | Class FieldLo, FieldHi; |
| 2620 | |
| 2621 | // Bit-fields require special handling, they do not force the |
| 2622 | // structure to be passed in memory even if unaligned, and |
| 2623 | // therefore they can straddle an eightbyte. |
| 2624 | if (BitField) { |
| 2625 | // Ignore padding bit-fields. |
| 2626 | if (i->isUnnamedBitfield()) |
| 2627 | continue; |
| 2628 | |
| 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); |
| 2756 | assert((Size == 128 || Size == 256) && "Invalid type found!"); |
| 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 { |
| 3654 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3655 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3656 | CharUnits::fromQuantity(8), |
| 3657 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3658 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3659 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3660 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3661 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3662 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3663 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3664 | bool IsSoftFloatABI; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3665 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3666 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 3667 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3668 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3669 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3670 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3671 | }; |
| 3672 | |
| 3673 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3674 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3675 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 3676 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3677 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3678 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3679 | // This is recovered from gcc output. |
| 3680 | return 1; // r1 is the dedicated stack pointer |
| 3681 | } |
| 3682 | |
| 3683 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3684 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3685 | }; |
| 3686 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3687 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3688 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 3689 | // TODO: this implementation is now likely redundant with |
| 3690 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3691 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 3692 | QualType Ty) const { |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 3693 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3694 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3695 | // TODO: Implement this. For now ignore. |
| 3696 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 3697 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3700 | // struct __va_list_tag { |
| 3701 | // unsigned char gpr; |
| 3702 | // unsigned char fpr; |
| 3703 | // unsigned short reserved; |
| 3704 | // void *overflow_arg_area; |
| 3705 | // void *reg_save_area; |
| 3706 | // }; |
| 3707 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3708 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3709 | bool isInt = |
| 3710 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3711 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3712 | |
| 3713 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 3714 | // with the argument-lowering code. |
| 3715 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3716 | |
| 3717 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3718 | |
| 3719 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 3720 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3721 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3722 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 3723 | } else { |
| 3724 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3725 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3726 | |
| 3727 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 3728 | |
| 3729 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3730 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3731 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 3732 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 3733 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3734 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3735 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 3736 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3737 | |
| 3738 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3739 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3740 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3741 | |
| 3742 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3743 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3744 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 3745 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3746 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3747 | // Case 1: consume registers. |
| 3748 | Address RegAddr = Address::invalid(); |
| 3749 | { |
| 3750 | CGF.EmitBlock(UsingRegs); |
| 3751 | |
| 3752 | Address RegSaveAreaPtr = |
| 3753 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 3754 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 3755 | CharUnits::fromQuantity(8)); |
| 3756 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 3757 | |
| 3758 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3759 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3760 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 3761 | CharUnits::fromQuantity(32)); |
| 3762 | } |
| 3763 | |
| 3764 | // Get the address of the saved value by scaling the number of |
| 3765 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3766 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3767 | llvm::Value *RegOffset = |
| 3768 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 3769 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 3770 | RegAddr.getPointer(), RegOffset), |
| 3771 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 3772 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 3773 | |
| 3774 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3775 | NumRegs = |
| 3776 | Builder.CreateAdd(NumRegs, |
| 3777 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3778 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 3779 | |
| 3780 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3781 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3782 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3783 | // Case 2: consume space in the overflow area. |
| 3784 | Address MemAddr = Address::invalid(); |
| 3785 | { |
| 3786 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3787 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 3788 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 3789 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3790 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 3791 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 3792 | |
| 3793 | CharUnits Size; |
| 3794 | if (!isIndirect) { |
| 3795 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3796 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3797 | } else { |
| 3798 | Size = CGF.getPointerSize(); |
| 3799 | } |
| 3800 | |
| 3801 | Address OverflowAreaAddr = |
| 3802 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3803 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3804 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3805 | // Round up address of argument to alignment |
| 3806 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3807 | if (Align > OverflowAreaAlign) { |
| 3808 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 3809 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 3810 | Align); |
| 3811 | } |
| 3812 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3813 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 3814 | |
| 3815 | // Increase the overflow area. |
| 3816 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 3817 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 3818 | CGF.EmitBranch(Cont); |
| 3819 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3820 | |
| 3821 | CGF.EmitBlock(Cont); |
| 3822 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3823 | // Merge the cases with a phi. |
| 3824 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 3825 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3826 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3827 | // Load the pointer if the argument was passed indirectly. |
| 3828 | if (isIndirect) { |
| 3829 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 3830 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | return Result; |
| 3834 | } |
| 3835 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3836 | bool |
| 3837 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3838 | llvm::Value *Address) const { |
| 3839 | // This is calculated from the LLVM and GCC tables and verified |
| 3840 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3841 | |
| 3842 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3843 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3844 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3845 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3846 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3847 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3848 | |
| 3849 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3850 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3851 | |
| 3852 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3853 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3854 | |
| 3855 | // 64-76 are various 4-byte special-purpose registers: |
| 3856 | // 64: mq |
| 3857 | // 65: lr |
| 3858 | // 66: ctr |
| 3859 | // 67: ap |
| 3860 | // 68-75 cr0-7 |
| 3861 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3862 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3863 | |
| 3864 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3865 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3866 | |
| 3867 | // 109: vrsave |
| 3868 | // 110: vscr |
| 3869 | // 111: spe_acc |
| 3870 | // 112: spefscr |
| 3871 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3872 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3873 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3874 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3877 | // PowerPC-64 |
| 3878 | |
| 3879 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3880 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 3881 | class PPC64_SVR4_ABIInfo : public ABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3882 | public: |
| 3883 | enum ABIKind { |
| 3884 | ELFv1 = 0, |
| 3885 | ELFv2 |
| 3886 | }; |
| 3887 | |
| 3888 | private: |
| 3889 | static const unsigned GPRBits = 64; |
| 3890 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3891 | bool HasQPX; |
| 3892 | |
| 3893 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3894 | // will be passed in a QPX register. |
| 3895 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3896 | if (!HasQPX) |
| 3897 | return false; |
| 3898 | |
| 3899 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3900 | unsigned NumElements = VT->getNumElements(); |
| 3901 | if (NumElements == 1) |
| 3902 | return false; |
| 3903 | |
| 3904 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3905 | if (getContext().getTypeSize(Ty) <= 256) |
| 3906 | return true; |
| 3907 | } else if (VT->getElementType()-> |
| 3908 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3909 | if (getContext().getTypeSize(Ty) <= 128) |
| 3910 | return true; |
| 3911 | } |
| 3912 | } |
| 3913 | |
| 3914 | return false; |
| 3915 | } |
| 3916 | |
| 3917 | bool IsQPXVectorTy(QualType Ty) const { |
| 3918 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3919 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3920 | |
| 3921 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3922 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 3923 | : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3924 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3925 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3926 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3927 | |
| 3928 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3929 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3930 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3931 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3932 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3933 | uint64_t Members) const override; |
| 3934 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3935 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3936 | // Example: For aggregate arguments that fit in a register, we could |
| 3937 | // use getDirectInReg (as is done below for structs containing a single |
| 3938 | // floating-point value) to avoid pushing them to memory on function |
| 3939 | // entry. This would require changing the logic in PPCISelLowering |
| 3940 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3941 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3942 | if (!getCXXABI().classifyReturnType(FI)) |
| 3943 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3944 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3945 | // We rely on the default argument classification for the most part. |
| 3946 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3947 | // 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] | 3948 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3949 | if (T) { |
| 3950 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3951 | if (IsQPXVectorTy(T) || |
| 3952 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3953 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3954 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3955 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3956 | continue; |
| 3957 | } |
| 3958 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3959 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3960 | } |
| 3961 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3962 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3963 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3964 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3965 | }; |
| 3966 | |
| 3967 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3968 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3969 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3970 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3971 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 3972 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3973 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3974 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3975 | // This is recovered from gcc output. |
| 3976 | return 1; // r1 is the dedicated stack pointer |
| 3977 | } |
| 3978 | |
| 3979 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3980 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3981 | }; |
| 3982 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3983 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3984 | public: |
| 3985 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3986 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3987 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3988 | // This is recovered from gcc output. |
| 3989 | return 1; // r1 is the dedicated stack pointer |
| 3990 | } |
| 3991 | |
| 3992 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3993 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3994 | }; |
| 3995 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3996 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3997 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3998 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3999 | // extended to 64 bits. |
| 4000 | bool |
| 4001 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4002 | // Treat an enum type as its underlying type. |
| 4003 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4004 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4005 | |
| 4006 | // Promotable integer types are required to be promoted by the ABI. |
| 4007 | if (Ty->isPromotableIntegerType()) |
| 4008 | return true; |
| 4009 | |
| 4010 | // In addition to the usual promotable integer types, we also need to |
| 4011 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4012 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4013 | switch (BT->getKind()) { |
| 4014 | case BuiltinType::Int: |
| 4015 | case BuiltinType::UInt: |
| 4016 | return true; |
| 4017 | default: |
| 4018 | break; |
| 4019 | } |
| 4020 | |
| 4021 | return false; |
| 4022 | } |
| 4023 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4024 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4025 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4026 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4027 | // Complex types are passed just like their elements. |
| 4028 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4029 | Ty = CTy->getElementType(); |
| 4030 | |
| 4031 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4032 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4033 | if (IsQPXVectorTy(Ty)) { |
| 4034 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4035 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4036 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4037 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4038 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4039 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4040 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4041 | |
| 4042 | // For single-element float/vector structs, we consider the whole type |
| 4043 | // to have the same alignment requirements as its single element. |
| 4044 | const Type *AlignAsType = nullptr; |
| 4045 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4046 | if (EltType) { |
| 4047 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4048 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4049 | getContext().getTypeSize(EltType) == 128) || |
| 4050 | (BT && BT->isFloatingPoint())) |
| 4051 | AlignAsType = EltType; |
| 4052 | } |
| 4053 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4054 | // Likewise for ELFv2 homogeneous aggregates. |
| 4055 | const Type *Base = nullptr; |
| 4056 | uint64_t Members = 0; |
| 4057 | if (!AlignAsType && Kind == ELFv2 && |
| 4058 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4059 | AlignAsType = Base; |
| 4060 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4061 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4062 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4063 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4064 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4065 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4066 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4067 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4068 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4069 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4070 | |
| 4071 | // Otherwise, we only need alignment for any aggregate type that |
| 4072 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4073 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4074 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4075 | return CharUnits::fromQuantity(32); |
| 4076 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4077 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4078 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4079 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4082 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4083 | /// aggregate. Base is set to the base element type, and Members is set |
| 4084 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4085 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4086 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4087 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4088 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4089 | if (NElements == 0) |
| 4090 | return false; |
| 4091 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4092 | return false; |
| 4093 | Members *= NElements; |
| 4094 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4095 | const RecordDecl *RD = RT->getDecl(); |
| 4096 | if (RD->hasFlexibleArrayMember()) |
| 4097 | return false; |
| 4098 | |
| 4099 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4100 | |
| 4101 | // If this is a C++ record, check the bases first. |
| 4102 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4103 | for (const auto &I : CXXRD->bases()) { |
| 4104 | // Ignore empty records. |
| 4105 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4106 | continue; |
| 4107 | |
| 4108 | uint64_t FldMembers; |
| 4109 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4110 | return false; |
| 4111 | |
| 4112 | Members += FldMembers; |
| 4113 | } |
| 4114 | } |
| 4115 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4116 | for (const auto *FD : RD->fields()) { |
| 4117 | // Ignore (non-zero arrays of) empty records. |
| 4118 | QualType FT = FD->getType(); |
| 4119 | while (const ConstantArrayType *AT = |
| 4120 | getContext().getAsConstantArrayType(FT)) { |
| 4121 | if (AT->getSize().getZExtValue() == 0) |
| 4122 | return false; |
| 4123 | FT = AT->getElementType(); |
| 4124 | } |
| 4125 | if (isEmptyRecord(getContext(), FT, true)) |
| 4126 | continue; |
| 4127 | |
| 4128 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4129 | if (getContext().getLangOpts().CPlusPlus && |
| 4130 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4131 | continue; |
| 4132 | |
| 4133 | uint64_t FldMembers; |
| 4134 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4135 | return false; |
| 4136 | |
| 4137 | Members = (RD->isUnion() ? |
| 4138 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4139 | } |
| 4140 | |
| 4141 | if (!Base) |
| 4142 | return false; |
| 4143 | |
| 4144 | // Ensure there is no padding. |
| 4145 | if (getContext().getTypeSize(Base) * Members != |
| 4146 | getContext().getTypeSize(Ty)) |
| 4147 | return false; |
| 4148 | } else { |
| 4149 | Members = 1; |
| 4150 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4151 | Members = 2; |
| 4152 | Ty = CT->getElementType(); |
| 4153 | } |
| 4154 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4155 | // Most ABIs only support float, double, and some vector type widths. |
| 4156 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4157 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4158 | |
| 4159 | // The base type must be the same for all members. Types that |
| 4160 | // agree in both total size and mode (float vs. vector) are |
| 4161 | // treated as being equivalent here. |
| 4162 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4163 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4164 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4165 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4166 | // so make sure to widen it explicitly. |
| 4167 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4168 | QualType EltTy = VT->getElementType(); |
| 4169 | unsigned NumElements = |
| 4170 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4171 | Base = getContext() |
| 4172 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4173 | .getTypePtr(); |
| 4174 | } |
| 4175 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4176 | |
| 4177 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4178 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4179 | return false; |
| 4180 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4181 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4182 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4183 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4184 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4185 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4186 | // double, long double, or 128-bit vectors. |
| 4187 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4188 | if (BT->getKind() == BuiltinType::Float || |
| 4189 | BT->getKind() == BuiltinType::Double || |
| 4190 | BT->getKind() == BuiltinType::LongDouble) |
| 4191 | return true; |
| 4192 | } |
| 4193 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4194 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4195 | return true; |
| 4196 | } |
| 4197 | return false; |
| 4198 | } |
| 4199 | |
| 4200 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4201 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4202 | // Vector types require one register, floating point types require one |
| 4203 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4204 | uint32_t NumRegs = |
| 4205 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4206 | |
| 4207 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4208 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4211 | ABIArgInfo |
| 4212 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4213 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4214 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4215 | if (Ty->isAnyComplexType()) |
| 4216 | return ABIArgInfo::getDirect(); |
| 4217 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4218 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4219 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4220 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4221 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4222 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4223 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4224 | else if (Size < 128) { |
| 4225 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4226 | return ABIArgInfo::getDirect(CoerceTy); |
| 4227 | } |
| 4228 | } |
| 4229 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4230 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4231 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4232 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4233 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4234 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4235 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4236 | |
| 4237 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4238 | const Type *Base = nullptr; |
| 4239 | uint64_t Members = 0; |
| 4240 | if (Kind == ELFv2 && |
| 4241 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4242 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4243 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4244 | return ABIArgInfo::getDirect(CoerceTy); |
| 4245 | } |
| 4246 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4247 | // If an aggregate may end up fully in registers, we do not |
| 4248 | // use the ByVal method, but pass the aggregate as array. |
| 4249 | // This is usually beneficial since we avoid forcing the |
| 4250 | // back-end to store the argument to memory. |
| 4251 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4252 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4253 | llvm::Type *CoerceTy; |
| 4254 | |
| 4255 | // Types up to 8 bytes are passed as integer type (which will be |
| 4256 | // properly aligned in the argument save area doubleword). |
| 4257 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4258 | CoerceTy = |
| 4259 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4260 | // Larger types are passed as arrays, with the base type selected |
| 4261 | // according to the required alignment in the save area. |
| 4262 | else { |
| 4263 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4264 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4265 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4266 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4267 | } |
| 4268 | |
| 4269 | return ABIArgInfo::getDirect(CoerceTy); |
| 4270 | } |
| 4271 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4272 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4273 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4274 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4275 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | return (isPromotableTypeForABI(Ty) ? |
| 4279 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4280 | } |
| 4281 | |
| 4282 | ABIArgInfo |
| 4283 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4284 | if (RetTy->isVoidType()) |
| 4285 | return ABIArgInfo::getIgnore(); |
| 4286 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4287 | if (RetTy->isAnyComplexType()) |
| 4288 | return ABIArgInfo::getDirect(); |
| 4289 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4290 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4291 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4292 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4293 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4294 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4295 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4296 | else if (Size < 128) { |
| 4297 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4298 | return ABIArgInfo::getDirect(CoerceTy); |
| 4299 | } |
| 4300 | } |
| 4301 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4302 | if (isAggregateTypeForABI(RetTy)) { |
| 4303 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4304 | const Type *Base = nullptr; |
| 4305 | uint64_t Members = 0; |
| 4306 | if (Kind == ELFv2 && |
| 4307 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4308 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4309 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4310 | return ABIArgInfo::getDirect(CoerceTy); |
| 4311 | } |
| 4312 | |
| 4313 | // ELFv2 small aggregates are returned in up to two registers. |
| 4314 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4315 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4316 | if (Bits == 0) |
| 4317 | return ABIArgInfo::getIgnore(); |
| 4318 | |
| 4319 | llvm::Type *CoerceTy; |
| 4320 | if (Bits > GPRBits) { |
| 4321 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 4322 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4323 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4324 | CoerceTy = |
| 4325 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4326 | return ABIArgInfo::getDirect(CoerceTy); |
| 4327 | } |
| 4328 | |
| 4329 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4330 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4331 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4332 | |
| 4333 | return (isPromotableTypeForABI(RetTy) ? |
| 4334 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4335 | } |
| 4336 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4337 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4338 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4339 | QualType Ty) const { |
| 4340 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4341 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4342 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4343 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4344 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4345 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4346 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4347 | // in separate doublewords. However, Clang expects us to produce a |
| 4348 | // pointer to a structure with the two parts packed tightly. So generate |
| 4349 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4350 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4351 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4352 | CharUnits EltSize = TypeInfo.first / 2; |
| 4353 | if (EltSize < SlotSize) { |
| 4354 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4355 | SlotSize * 2, SlotSize, |
| 4356 | SlotSize, /*AllowHigher*/ true); |
| 4357 | |
| 4358 | Address RealAddr = Addr; |
| 4359 | Address ImagAddr = RealAddr; |
| 4360 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4361 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4362 | SlotSize - EltSize); |
| 4363 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4364 | 2 * SlotSize - EltSize); |
| 4365 | } else { |
| 4366 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4367 | } |
| 4368 | |
| 4369 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4370 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4371 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4372 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4373 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4374 | |
| 4375 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4376 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4377 | /*init*/ true); |
| 4378 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4379 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4382 | // Otherwise, just use the general rule. |
| 4383 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4384 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | static bool |
| 4388 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4389 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4390 | // This is calculated from the LLVM and GCC tables and verified |
| 4391 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4392 | |
| 4393 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4394 | |
| 4395 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4396 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4397 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4398 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4399 | |
| 4400 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4401 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4402 | |
| 4403 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4404 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4405 | |
| 4406 | // 64-76 are various 4-byte special-purpose registers: |
| 4407 | // 64: mq |
| 4408 | // 65: lr |
| 4409 | // 66: ctr |
| 4410 | // 67: ap |
| 4411 | // 68-75 cr0-7 |
| 4412 | // 76: xer |
| 4413 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 4414 | |
| 4415 | // 77-108: v0-31, the 16-byte vector registers |
| 4416 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4417 | |
| 4418 | // 109: vrsave |
| 4419 | // 110: vscr |
| 4420 | // 111: spe_acc |
| 4421 | // 112: spefscr |
| 4422 | // 113: sfp |
| 4423 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 4424 | |
| 4425 | return false; |
| 4426 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4427 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4428 | bool |
| 4429 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4430 | CodeGen::CodeGenFunction &CGF, |
| 4431 | llvm::Value *Address) const { |
| 4432 | |
| 4433 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4434 | } |
| 4435 | |
| 4436 | bool |
| 4437 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4438 | llvm::Value *Address) const { |
| 4439 | |
| 4440 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4441 | } |
| 4442 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4443 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4444 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4445 | //===----------------------------------------------------------------------===// |
| 4446 | |
| 4447 | namespace { |
| 4448 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4449 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4450 | public: |
| 4451 | enum ABIKind { |
| 4452 | AAPCS = 0, |
| 4453 | DarwinPCS |
| 4454 | }; |
| 4455 | |
| 4456 | private: |
| 4457 | ABIKind Kind; |
| 4458 | |
| 4459 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4460 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4461 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4462 | |
| 4463 | private: |
| 4464 | ABIKind getABIKind() const { return Kind; } |
| 4465 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4466 | |
| 4467 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4468 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4469 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4470 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4471 | uint64_t Members) const override; |
| 4472 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4473 | bool isIllegalVectorType(QualType Ty) const; |
| 4474 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4475 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4476 | if (!getCXXABI().classifyReturnType(FI)) |
| 4477 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4478 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4479 | for (auto &it : FI.arguments()) |
| 4480 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4483 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4484 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4485 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4486 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4487 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4488 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4489 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4490 | QualType Ty) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4491 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4492 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 4493 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4494 | |
| 4495 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 4496 | ArrayRef<llvm::Type*> scalars, |
| 4497 | bool asReturnValue) const override { |
| 4498 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4499 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4500 | }; |
| 4501 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4502 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4503 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4504 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4505 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4506 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4507 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4508 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 4509 | } |
| 4510 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4511 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4512 | return 31; |
| 4513 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4514 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4515 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4516 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4517 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4518 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4519 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4520 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4521 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4522 | // Handle illegal vector types here. |
| 4523 | if (isIllegalVectorType(Ty)) { |
| 4524 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4525 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 4526 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4527 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 4528 | return ABIArgInfo::getDirect(ResType); |
| 4529 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4530 | if (Size <= 32) { |
| 4531 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4532 | return ABIArgInfo::getDirect(ResType); |
| 4533 | } |
| 4534 | if (Size == 64) { |
| 4535 | llvm::Type *ResType = |
| 4536 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4537 | return ABIArgInfo::getDirect(ResType); |
| 4538 | } |
| 4539 | if (Size == 128) { |
| 4540 | llvm::Type *ResType = |
| 4541 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4542 | return ABIArgInfo::getDirect(ResType); |
| 4543 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4544 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4545 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4546 | |
| 4547 | if (!isAggregateTypeForABI(Ty)) { |
| 4548 | // Treat an enum type as its underlying type. |
| 4549 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4550 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4551 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4552 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4553 | ? ABIArgInfo::getExtend() |
| 4554 | : ABIArgInfo::getDirect()); |
| 4555 | } |
| 4556 | |
| 4557 | // Structures with either a non-trivial destructor or a non-trivial |
| 4558 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4559 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4560 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4561 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4565 | // elsewhere for GNU compatibility. |
| 4566 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4567 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4568 | return ABIArgInfo::getIgnore(); |
| 4569 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4570 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4571 | } |
| 4572 | |
| 4573 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4574 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4575 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4576 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4577 | return ABIArgInfo::getDirect( |
| 4578 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4582 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4583 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame^] | 4584 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 4585 | // same size and alignment. |
| 4586 | if (getTarget().isRenderScriptTarget()) { |
| 4587 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 4588 | } |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4589 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4590 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4591 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4592 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4593 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4594 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4595 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4596 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4597 | } |
| 4598 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4599 | } |
| 4600 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4601 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4604 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4605 | if (RetTy->isVoidType()) |
| 4606 | return ABIArgInfo::getIgnore(); |
| 4607 | |
| 4608 | // Large vector types should be returned via memory. |
| 4609 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4610 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4611 | |
| 4612 | if (!isAggregateTypeForABI(RetTy)) { |
| 4613 | // Treat an enum type as its underlying type. |
| 4614 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4615 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4616 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4617 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4618 | ? ABIArgInfo::getExtend() |
| 4619 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4622 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4623 | return ABIArgInfo::getIgnore(); |
| 4624 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4625 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4626 | uint64_t Members = 0; |
| 4627 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4628 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4629 | return ABIArgInfo::getDirect(); |
| 4630 | |
| 4631 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4632 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4633 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame^] | 4634 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 4635 | // same size and alignment. |
| 4636 | if (getTarget().isRenderScriptTarget()) { |
| 4637 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 4638 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4639 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4640 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4641 | |
| 4642 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4643 | // For aggregates with 16-byte alignment, we use i128. |
| 4644 | if (Alignment < 128 && Size == 128) { |
| 4645 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4646 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4647 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4648 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4649 | } |
| 4650 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4651 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4654 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4655 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4656 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4657 | // Check whether VT is legal. |
| 4658 | unsigned NumElements = VT->getNumElements(); |
| 4659 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 4660 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 4661 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4662 | return true; |
| 4663 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4664 | } |
| 4665 | return false; |
| 4666 | } |
| 4667 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4668 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4669 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4670 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4671 | // but with the difference that any floating-point type is allowed, |
| 4672 | // including __fp16. |
| 4673 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4674 | if (BT->isFloatingPoint()) |
| 4675 | return true; |
| 4676 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4677 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4678 | if (VecSize == 64 || VecSize == 128) |
| 4679 | return true; |
| 4680 | } |
| 4681 | return false; |
| 4682 | } |
| 4683 | |
| 4684 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4685 | uint64_t Members) const { |
| 4686 | return Members <= 4; |
| 4687 | } |
| 4688 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4689 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4690 | QualType Ty, |
| 4691 | CodeGenFunction &CGF) const { |
| 4692 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4693 | bool IsIndirect = AI.isIndirect(); |
| 4694 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4695 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4696 | if (IsIndirect) |
| 4697 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4698 | else if (AI.getCoerceToType()) |
| 4699 | BaseTy = AI.getCoerceToType(); |
| 4700 | |
| 4701 | unsigned NumRegs = 1; |
| 4702 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4703 | BaseTy = ArrTy->getElementType(); |
| 4704 | NumRegs = ArrTy->getNumElements(); |
| 4705 | } |
| 4706 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4707 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4708 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4709 | // Standard, section B.4: |
| 4710 | // |
| 4711 | // struct { |
| 4712 | // void *__stack; |
| 4713 | // void *__gr_top; |
| 4714 | // void *__vr_top; |
| 4715 | // int __gr_offs; |
| 4716 | // int __vr_offs; |
| 4717 | // }; |
| 4718 | |
| 4719 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4720 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4721 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4722 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4723 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4724 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4725 | CharUnits TyAlign = TyInfo.second; |
| 4726 | |
| 4727 | Address reg_offs_p = Address::invalid(); |
| 4728 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4729 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4730 | CharUnits reg_top_offset; |
| 4731 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4732 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4733 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4734 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4735 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 4736 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4737 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4738 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4739 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4740 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4741 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4742 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4743 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4744 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 4745 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4746 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4747 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4748 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4749 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4750 | } |
| 4751 | |
| 4752 | //======================================= |
| 4753 | // Find out where argument was passed |
| 4754 | //======================================= |
| 4755 | |
| 4756 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4757 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4758 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4759 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4760 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4761 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4762 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4763 | |
| 4764 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4765 | |
| 4766 | // 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] | 4767 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4768 | CGF.EmitBlock(MaybeRegBlock); |
| 4769 | |
| 4770 | // Integer arguments may need to correct register alignment (for example a |
| 4771 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4772 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4773 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 4774 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4775 | |
| 4776 | reg_offs = CGF.Builder.CreateAdd( |
| 4777 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4778 | "align_regoffs"); |
| 4779 | reg_offs = CGF.Builder.CreateAnd( |
| 4780 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4781 | "aligned_regoffs"); |
| 4782 | } |
| 4783 | |
| 4784 | // 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] | 4785 | // The fact that this is done unconditionally reflects the fact that |
| 4786 | // allocating an argument to the stack also uses up all the remaining |
| 4787 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4788 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4789 | NewOffset = CGF.Builder.CreateAdd( |
| 4790 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4791 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4792 | |
| 4793 | // Now we're in a position to decide whether this argument really was in |
| 4794 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4795 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4796 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4797 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4798 | |
| 4799 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4800 | |
| 4801 | //======================================= |
| 4802 | // Argument was in registers |
| 4803 | //======================================= |
| 4804 | |
| 4805 | // Now we emit the code for if the argument was originally passed in |
| 4806 | // registers. First start the appropriate block: |
| 4807 | CGF.EmitBlock(InRegBlock); |
| 4808 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4809 | llvm::Value *reg_top = nullptr; |
| 4810 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 4811 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4812 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4813 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 4814 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 4815 | Address RegAddr = Address::invalid(); |
| 4816 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4817 | |
| 4818 | if (IsIndirect) { |
| 4819 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4820 | // stored registers or on the stack will actually be a struct **. |
| 4821 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4822 | } |
| 4823 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4824 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4825 | uint64_t NumMembers = 0; |
| 4826 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4827 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4828 | // Homogeneous aggregates passed in registers will have their elements split |
| 4829 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4830 | // qN+1, ...). We reload and store into a temporary local variable |
| 4831 | // contiguously. |
| 4832 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4833 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4834 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4835 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4836 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 4837 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4838 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4839 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 4840 | int Offset = 0; |
| 4841 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4842 | BaseTyInfo.first.getQuantity() < 16) |
| 4843 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 4844 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4845 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4846 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 4847 | Address LoadAddr = |
| 4848 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 4849 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 4850 | |
| 4851 | Address StoreAddr = |
| 4852 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4853 | |
| 4854 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4855 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4856 | } |
| 4857 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4858 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4859 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4860 | // Otherwise the object is contiguous in memory. |
| 4861 | |
| 4862 | // It might be right-aligned in its slot. |
| 4863 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 4864 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4865 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4866 | TyInfo.first < SlotSize) { |
| 4867 | CharUnits Offset = SlotSize - TyInfo.first; |
| 4868 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4869 | } |
| 4870 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4871 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
| 4874 | CGF.EmitBranch(ContBlock); |
| 4875 | |
| 4876 | //======================================= |
| 4877 | // Argument was on the stack |
| 4878 | //======================================= |
| 4879 | CGF.EmitBlock(OnStackBlock); |
| 4880 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4881 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 4882 | CharUnits::Zero(), "stack_p"); |
| 4883 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4884 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4885 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4886 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4887 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 4888 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4889 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4890 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4891 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4892 | OnStackPtr = CGF.Builder.CreateAdd( |
| 4893 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4894 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4895 | OnStackPtr = CGF.Builder.CreateAnd( |
| 4896 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4897 | "align_stack"); |
| 4898 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4899 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
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 | Address OnStackAddr(OnStackPtr, |
| 4902 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4903 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4904 | // All stack slots are multiples of 8 bytes. |
| 4905 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 4906 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4907 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4908 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4909 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4910 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4911 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4912 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4913 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4914 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4915 | |
| 4916 | // Write the new value of __stack for the next call to va_arg |
| 4917 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4918 | |
| 4919 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4920 | TyInfo.first < StackSlotSize) { |
| 4921 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 4922 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4925 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4926 | |
| 4927 | CGF.EmitBranch(ContBlock); |
| 4928 | |
| 4929 | //======================================= |
| 4930 | // Tidy up |
| 4931 | //======================================= |
| 4932 | CGF.EmitBlock(ContBlock); |
| 4933 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4934 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 4935 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4936 | |
| 4937 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4938 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 4939 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4940 | |
| 4941 | return ResAddr; |
| 4942 | } |
| 4943 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4944 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4945 | CodeGenFunction &CGF) const { |
| 4946 | // The backend's lowering doesn't support va_arg for aggregates or |
| 4947 | // illegal vector types. Lower VAArg here for these cases and use |
| 4948 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4949 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4950 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4951 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4952 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4953 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4954 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4955 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4956 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 4957 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 4958 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4959 | } |
| 4960 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4961 | // The size of the actual thing passed, which might end up just |
| 4962 | // being a pointer for indirect types. |
| 4963 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4964 | |
| 4965 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 4966 | // aggregates should be passed indirectly. |
| 4967 | bool IsIndirect = false; |
| 4968 | if (TyInfo.first.getQuantity() > 16) { |
| 4969 | const Type *Base = nullptr; |
| 4970 | uint64_t Members = 0; |
| 4971 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4974 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 4975 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
| 4978 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4979 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4980 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4981 | |
| 4982 | namespace { |
| 4983 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4984 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4985 | public: |
| 4986 | enum ABIKind { |
| 4987 | APCS = 0, |
| 4988 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4989 | AAPCS_VFP = 2, |
| 4990 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4991 | }; |
| 4992 | |
| 4993 | private: |
| 4994 | ABIKind Kind; |
| 4995 | |
| 4996 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4997 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 4998 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4999 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5000 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5001 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5002 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5003 | switch (getTarget().getTriple().getEnvironment()) { |
| 5004 | case llvm::Triple::Android: |
| 5005 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5006 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5007 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5008 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5009 | case llvm::Triple::MuslEABI: |
| 5010 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5011 | return true; |
| 5012 | default: |
| 5013 | return false; |
| 5014 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5017 | bool isEABIHF() const { |
| 5018 | switch (getTarget().getTriple().getEnvironment()) { |
| 5019 | case llvm::Triple::EABIHF: |
| 5020 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5021 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5022 | return true; |
| 5023 | default: |
| 5024 | return false; |
| 5025 | } |
| 5026 | } |
| 5027 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5028 | ABIKind getABIKind() const { return Kind; } |
| 5029 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5030 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 5031 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5032 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5033 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5034 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5035 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5036 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5037 | uint64_t Members) const override; |
| 5038 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5039 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5040 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5041 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5042 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5043 | |
| 5044 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5045 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5046 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5047 | |
| 5048 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 5049 | ArrayRef<llvm::Type*> scalars, |
| 5050 | bool asReturnValue) const override { |
| 5051 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5052 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5053 | }; |
| 5054 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5055 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5056 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5057 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5058 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5059 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5060 | const ARMABIInfo &getABIInfo() const { |
| 5061 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5062 | } |
| 5063 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5064 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5065 | return 13; |
| 5066 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5067 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5068 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5069 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 5070 | } |
| 5071 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5072 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5073 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5074 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5075 | |
| 5076 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5077 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5078 | return false; |
| 5079 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5080 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5081 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5082 | if (getABIInfo().isEABI()) return 88; |
| 5083 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5084 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5085 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5086 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5087 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5088 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5089 | if (!FD) |
| 5090 | return; |
| 5091 | |
| 5092 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5093 | if (!Attr) |
| 5094 | return; |
| 5095 | |
| 5096 | const char *Kind; |
| 5097 | switch (Attr->getInterrupt()) { |
| 5098 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5099 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5100 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5101 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5102 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5103 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5104 | } |
| 5105 | |
| 5106 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5107 | |
| 5108 | Fn->addFnAttr("interrupt", Kind); |
| 5109 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5110 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5111 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5112 | return; |
| 5113 | |
| 5114 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5115 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5116 | // the backend to perform a realignment as part of the function prologue. |
| 5117 | llvm::AttrBuilder B; |
| 5118 | B.addStackAlignmentAttr(8); |
| 5119 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 5120 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 5121 | llvm::AttributeSet::FunctionIndex, |
| 5122 | B)); |
| 5123 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5124 | }; |
| 5125 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5126 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5127 | public: |
| 5128 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5129 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5130 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5131 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5132 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5133 | |
| 5134 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5135 | llvm::SmallString<24> &Opt) const override { |
| 5136 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5137 | } |
| 5138 | |
| 5139 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5140 | llvm::SmallString<32> &Opt) const override { |
| 5141 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5142 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5143 | }; |
| 5144 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5145 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5146 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5147 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5148 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 5149 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5150 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5151 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5152 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5153 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5154 | FI.getReturnInfo() = |
| 5155 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5156 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5157 | for (auto &I : FI.arguments()) |
| 5158 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5159 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5160 | // Always honor user-specified calling convention. |
| 5161 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5162 | return; |
| 5163 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5164 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5165 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5166 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5167 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5168 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5169 | /// Return the default calling convention that LLVM will use. |
| 5170 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5171 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5172 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5173 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5174 | else if (isEABI()) |
| 5175 | return llvm::CallingConv::ARM_AAPCS; |
| 5176 | else |
| 5177 | return llvm::CallingConv::ARM_APCS; |
| 5178 | } |
| 5179 | |
| 5180 | /// Return the calling convention that our ABI would like us to use |
| 5181 | /// as the C calling convention. |
| 5182 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5183 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5184 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5185 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5186 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5187 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5188 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5189 | llvm_unreachable("bad ABI kind"); |
| 5190 | } |
| 5191 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5192 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5193 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5194 | |
| 5195 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5196 | // they'd just match what LLVM will infer from the triple. |
| 5197 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5198 | if (abiCC != getLLVMDefaultCC()) |
| 5199 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5200 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5201 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 5202 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 5203 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
| 5204 | switch (getABIKind()) { |
| 5205 | case APCS: |
| 5206 | case AAPCS16_VFP: |
| 5207 | if (abiCC != getLLVMDefaultCC()) |
| 5208 | BuiltinCC = abiCC; |
| 5209 | break; |
| 5210 | case AAPCS: |
| 5211 | case AAPCS_VFP: |
| 5212 | BuiltinCC = llvm::CallingConv::ARM_AAPCS; |
| 5213 | break; |
| 5214 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5215 | } |
| 5216 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5217 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5218 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5219 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5220 | // A single-precision floating-point type (including promoted |
| 5221 | // half-precision types); A double-precision floating-point type; |
| 5222 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5223 | // with a Base Type of a single- or double-precision floating-point type, |
| 5224 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5225 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5226 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5227 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5228 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5229 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5230 | // Handle illegal vector types here. |
| 5231 | if (isIllegalVectorType(Ty)) { |
| 5232 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5233 | if (Size <= 32) { |
| 5234 | llvm::Type *ResType = |
| 5235 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5236 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5237 | } |
| 5238 | if (Size == 64) { |
| 5239 | llvm::Type *ResType = llvm::VectorType::get( |
| 5240 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5241 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5242 | } |
| 5243 | if (Size == 128) { |
| 5244 | llvm::Type *ResType = llvm::VectorType::get( |
| 5245 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5246 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5247 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5248 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5249 | } |
| 5250 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5251 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 5252 | // unspecified. This is not done for OpenCL as it handles the half type |
| 5253 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5254 | if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5255 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5256 | llvm::Type::getFloatTy(getVMContext()) : |
| 5257 | llvm::Type::getInt32Ty(getVMContext()); |
| 5258 | return ABIArgInfo::getDirect(ResType); |
| 5259 | } |
| 5260 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5261 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5262 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5263 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5264 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5265 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5266 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5267 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5268 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5269 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5270 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5271 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5272 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5273 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5274 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5275 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5276 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5277 | return ABIArgInfo::getIgnore(); |
| 5278 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5279 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5280 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5281 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5282 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5283 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5284 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5285 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5286 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5287 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5288 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5289 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5290 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5291 | // this convention even for a variadic function: the backend will use GPRs |
| 5292 | // if needed. |
| 5293 | const Type *Base = nullptr; |
| 5294 | uint64_t Members = 0; |
| 5295 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5296 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5297 | llvm::Type *Ty = |
| 5298 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5299 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5300 | } |
| 5301 | } |
| 5302 | |
| 5303 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5304 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5305 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5306 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5307 | // and a pointer is passed. |
| 5308 | return ABIArgInfo::getIndirect( |
| 5309 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5310 | } |
| 5311 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5312 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5313 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5314 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5315 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5316 | uint64_t ABIAlign = 4; |
| 5317 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5318 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5319 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5320 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5321 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5322 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5323 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5324 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5325 | /*ByVal=*/true, |
| 5326 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5327 | } |
| 5328 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame^] | 5329 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5330 | // same size and alignment. |
| 5331 | if (getTarget().isRenderScriptTarget()) { |
| 5332 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5333 | } |
| 5334 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5335 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5336 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5337 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5338 | // FIXME: Try to match the types of the arguments more accurately where |
| 5339 | // we can. |
| 5340 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5341 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5342 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5343 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5344 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5345 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5346 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5347 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5348 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5349 | } |
| 5350 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5351 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5352 | llvm::LLVMContext &VMContext) { |
| 5353 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5354 | // is called integer-like if its size is less than or equal to one word, and |
| 5355 | // the offset of each of its addressable sub-fields is zero. |
| 5356 | |
| 5357 | uint64_t Size = Context.getTypeSize(Ty); |
| 5358 | |
| 5359 | // Check that the type fits in a word. |
| 5360 | if (Size > 32) |
| 5361 | return false; |
| 5362 | |
| 5363 | // FIXME: Handle vector types! |
| 5364 | if (Ty->isVectorType()) |
| 5365 | return false; |
| 5366 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5367 | // Float types are never treated as "integer like". |
| 5368 | if (Ty->isRealFloatingType()) |
| 5369 | return false; |
| 5370 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5371 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5372 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5373 | return true; |
| 5374 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5375 | // Small complex integer types are "integer like". |
| 5376 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5377 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5378 | |
| 5379 | // Single element and zero sized arrays should be allowed, by the definition |
| 5380 | // above, but they are not. |
| 5381 | |
| 5382 | // Otherwise, it must be a record type. |
| 5383 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5384 | if (!RT) return false; |
| 5385 | |
| 5386 | // Ignore records with flexible arrays. |
| 5387 | const RecordDecl *RD = RT->getDecl(); |
| 5388 | if (RD->hasFlexibleArrayMember()) |
| 5389 | return false; |
| 5390 | |
| 5391 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5392 | // like". |
| 5393 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5394 | |
| 5395 | bool HadField = false; |
| 5396 | unsigned idx = 0; |
| 5397 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5398 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5399 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5400 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5401 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5402 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5403 | // struct { int : 0; int x } |
| 5404 | // is non-integer like according to gcc. |
| 5405 | if (FD->isBitField()) { |
| 5406 | if (!RD->isUnion()) |
| 5407 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5408 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5409 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5410 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5411 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5412 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5413 | } |
| 5414 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5415 | // Check if this field is at offset 0. |
| 5416 | if (Layout.getFieldOffset(idx) != 0) |
| 5417 | return false; |
| 5418 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5419 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5420 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5421 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5422 | // Only allow at most one field in a structure. This doesn't match the |
| 5423 | // wording above, but follows gcc in situations with a field following an |
| 5424 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5425 | if (!RD->isUnion()) { |
| 5426 | if (HadField) |
| 5427 | return false; |
| 5428 | |
| 5429 | HadField = true; |
| 5430 | } |
| 5431 | } |
| 5432 | |
| 5433 | return true; |
| 5434 | } |
| 5435 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5436 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5437 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5438 | bool IsEffectivelyAAPCS_VFP = |
| 5439 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5440 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5441 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5442 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5443 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5444 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5445 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5446 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5447 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5448 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5449 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5450 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5451 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5452 | if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5453 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5454 | llvm::Type::getFloatTy(getVMContext()) : |
| 5455 | llvm::Type::getInt32Ty(getVMContext()); |
| 5456 | return ABIArgInfo::getDirect(ResType); |
| 5457 | } |
| 5458 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5459 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5460 | // Treat an enum type as its underlying type. |
| 5461 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5462 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5463 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5464 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5465 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5466 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5467 | |
| 5468 | // Are we following APCS? |
| 5469 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5470 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5471 | return ABIArgInfo::getIgnore(); |
| 5472 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5473 | // Complex types are all returned as packed integers. |
| 5474 | // |
| 5475 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5476 | // correctly. |
| 5477 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5478 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5479 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5480 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5481 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5482 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5483 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5484 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5485 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5486 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5487 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5488 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5489 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5490 | } |
| 5491 | |
| 5492 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5493 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5494 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5495 | |
| 5496 | // Otherwise this is an AAPCS variant. |
| 5497 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5498 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5499 | return ABIArgInfo::getIgnore(); |
| 5500 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5501 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5502 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5503 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5504 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5505 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5506 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5507 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5508 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5509 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5510 | } |
| 5511 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5512 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5513 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5514 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5515 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame^] | 5516 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 5517 | // same size and alignment. |
| 5518 | if (getTarget().isRenderScriptTarget()) { |
| 5519 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5520 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5521 | if (getDataLayout().isBigEndian()) |
| 5522 | // 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] | 5523 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5524 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5525 | // Return in the smallest viable integer type. |
| 5526 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5527 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5528 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5529 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5530 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5531 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5532 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5533 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5534 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5535 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5538 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5541 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5542 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 5543 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 5544 | if (isAndroid()) { |
| 5545 | // Android shipped using Clang 3.1, which supported a slightly different |
| 5546 | // vector ABI. The primary differences were that 3-element vector types |
| 5547 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 5548 | // accepts that legacy behavior for Android only. |
| 5549 | // Check whether VT is legal. |
| 5550 | unsigned NumElements = VT->getNumElements(); |
| 5551 | // NumElements should be power of 2 or equal to 3. |
| 5552 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 5553 | return true; |
| 5554 | } else { |
| 5555 | // Check whether VT is legal. |
| 5556 | unsigned NumElements = VT->getNumElements(); |
| 5557 | uint64_t Size = getContext().getTypeSize(VT); |
| 5558 | // NumElements should be power of 2. |
| 5559 | if (!llvm::isPowerOf2_32(NumElements)) |
| 5560 | return true; |
| 5561 | // Size should be greater than 32 bits. |
| 5562 | return Size <= 32; |
| 5563 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5564 | } |
| 5565 | return false; |
| 5566 | } |
| 5567 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5568 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5569 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 5570 | // double, or 64-bit or 128-bit vectors. |
| 5571 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5572 | if (BT->getKind() == BuiltinType::Float || |
| 5573 | BT->getKind() == BuiltinType::Double || |
| 5574 | BT->getKind() == BuiltinType::LongDouble) |
| 5575 | return true; |
| 5576 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5577 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5578 | if (VecSize == 64 || VecSize == 128) |
| 5579 | return true; |
| 5580 | } |
| 5581 | return false; |
| 5582 | } |
| 5583 | |
| 5584 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5585 | uint64_t Members) const { |
| 5586 | return Members <= 4; |
| 5587 | } |
| 5588 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5589 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5590 | QualType Ty) const { |
| 5591 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5592 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5593 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5594 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5595 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 5596 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5597 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5598 | } |
| 5599 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5600 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5601 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5602 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5603 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 5604 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5605 | const Type *Base = nullptr; |
| 5606 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5607 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 5608 | IsIndirect = true; |
| 5609 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5610 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 5611 | // allocated by the caller. |
| 5612 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 5613 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5614 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 5615 | IsIndirect = true; |
| 5616 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5617 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5618 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 5619 | // 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] | 5620 | // Our callers should be prepared to handle an under-aligned address. |
| 5621 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 5622 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5623 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5624 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 5625 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5626 | // ARMv7k allows type alignment up to 16 bytes. |
| 5627 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5628 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5629 | } else { |
| 5630 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5631 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5632 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5633 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5634 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 5635 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5636 | } |
| 5637 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5638 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5639 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5640 | //===----------------------------------------------------------------------===// |
| 5641 | |
| 5642 | namespace { |
| 5643 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5644 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5645 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5646 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5647 | |
| 5648 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5649 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5650 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5651 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5652 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5653 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5654 | }; |
| 5655 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5656 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5657 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5658 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5659 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5660 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5661 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5662 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5663 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5664 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5665 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5666 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5667 | }; |
| 5668 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5669 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5670 | if (RetTy->isVoidType()) |
| 5671 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5672 | |
| 5673 | // note: this is different from default ABI |
| 5674 | if (!RetTy->isScalarType()) |
| 5675 | return ABIArgInfo::getDirect(); |
| 5676 | |
| 5677 | // Treat an enum type as its underlying type. |
| 5678 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5679 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5680 | |
| 5681 | return (RetTy->isPromotableIntegerType() ? |
| 5682 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
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::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5686 | // Treat an enum type as its underlying type. |
| 5687 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5688 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5689 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5690 | // Return aggregates type as indirect by value |
| 5691 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5692 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5693 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5694 | return (Ty->isPromotableIntegerType() ? |
| 5695 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5696 | } |
| 5697 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5698 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5699 | if (!getCXXABI().classifyReturnType(FI)) |
| 5700 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5701 | for (auto &I : FI.arguments()) |
| 5702 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5703 | |
| 5704 | // Always honor user-specified calling convention. |
| 5705 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5706 | return; |
| 5707 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5708 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5709 | } |
| 5710 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5711 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5712 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5713 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5714 | } |
| 5715 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5716 | void NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5717 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5718 | CodeGen::CodeGenModule &M) const{ |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5719 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5720 | if (!FD) return; |
| 5721 | |
| 5722 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5723 | |
| 5724 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5725 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5726 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5727 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5728 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5729 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5730 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5731 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5732 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5733 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5734 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5735 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5736 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5737 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5738 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5739 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5740 | // __global__ functions cannot be called from the device, we do not |
| 5741 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5742 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5743 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5744 | addNVVMMetadata(F, "kernel", 1); |
| 5745 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5746 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5747 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5748 | llvm::APSInt MaxThreads(32); |
| 5749 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5750 | if (MaxThreads > 0) |
| 5751 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5752 | |
| 5753 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5754 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5755 | // we don't have to add a PTX directive. |
| 5756 | if (Attr->getMinBlocks()) { |
| 5757 | llvm::APSInt MinBlocks(32); |
| 5758 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5759 | if (MinBlocks > 0) |
| 5760 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5761 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5762 | } |
| 5763 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5764 | } |
| 5765 | } |
| 5766 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5767 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5768 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5769 | llvm::Module *M = F->getParent(); |
| 5770 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5771 | |
| 5772 | // Get "nvvm.annotations" metadata node |
| 5773 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5774 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5775 | llvm::Metadata *MDVals[] = { |
| 5776 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5777 | llvm::ConstantAsMetadata::get( |
| 5778 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5779 | // Append metadata to nvvm.annotations |
| 5780 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5781 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5782 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5783 | |
| 5784 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5785 | // SystemZ ABI Implementation |
| 5786 | //===----------------------------------------------------------------------===// |
| 5787 | |
| 5788 | namespace { |
| 5789 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 5790 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5791 | bool HasVector; |
| 5792 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5793 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5794 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 5795 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5796 | |
| 5797 | bool isPromotableIntegerType(QualType Ty) const; |
| 5798 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5799 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5800 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5801 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5802 | |
| 5803 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5804 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5805 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5806 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5807 | if (!getCXXABI().classifyReturnType(FI)) |
| 5808 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5809 | for (auto &I : FI.arguments()) |
| 5810 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5811 | } |
| 5812 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5813 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5814 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 5815 | |
| 5816 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 5817 | ArrayRef<llvm::Type*> scalars, |
| 5818 | bool asReturnValue) const override { |
| 5819 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5820 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5821 | }; |
| 5822 | |
| 5823 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5824 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5825 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5826 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5827 | }; |
| 5828 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5829 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5830 | |
| 5831 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5832 | // Treat an enum type as its underlying type. |
| 5833 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5834 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5835 | |
| 5836 | // Promotable integer types are required to be promoted by the ABI. |
| 5837 | if (Ty->isPromotableIntegerType()) |
| 5838 | return true; |
| 5839 | |
| 5840 | // 32-bit values must also be promoted. |
| 5841 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5842 | switch (BT->getKind()) { |
| 5843 | case BuiltinType::Int: |
| 5844 | case BuiltinType::UInt: |
| 5845 | return true; |
| 5846 | default: |
| 5847 | return false; |
| 5848 | } |
| 5849 | return false; |
| 5850 | } |
| 5851 | |
| 5852 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5853 | return (Ty->isAnyComplexType() || |
| 5854 | Ty->isVectorType() || |
| 5855 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5856 | } |
| 5857 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5858 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5859 | return (HasVector && |
| 5860 | Ty->isVectorType() && |
| 5861 | getContext().getTypeSize(Ty) <= 128); |
| 5862 | } |
| 5863 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5864 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5865 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5866 | switch (BT->getKind()) { |
| 5867 | case BuiltinType::Float: |
| 5868 | case BuiltinType::Double: |
| 5869 | return true; |
| 5870 | default: |
| 5871 | return false; |
| 5872 | } |
| 5873 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5874 | return false; |
| 5875 | } |
| 5876 | |
| 5877 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5878 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5879 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5880 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5881 | |
| 5882 | // If this is a C++ record, check the bases first. |
| 5883 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5884 | for (const auto &I : CXXRD->bases()) { |
| 5885 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5886 | |
| 5887 | // Empty bases don't affect things either way. |
| 5888 | if (isEmptyRecord(getContext(), Base, true)) |
| 5889 | continue; |
| 5890 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5891 | if (!Found.isNull()) |
| 5892 | return Ty; |
| 5893 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5894 | } |
| 5895 | |
| 5896 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5897 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5898 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5899 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5900 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5901 | if (getContext().getLangOpts().CPlusPlus && |
| 5902 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5903 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5904 | |
| 5905 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5906 | // Nested structures still do though. |
| 5907 | if (!Found.isNull()) |
| 5908 | return Ty; |
| 5909 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5910 | } |
| 5911 | |
| 5912 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5913 | // 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] | 5914 | if (!Found.isNull()) |
| 5915 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5916 | } |
| 5917 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5918 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5919 | } |
| 5920 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5921 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5922 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5923 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5924 | // struct { |
| 5925 | // i64 __gpr; |
| 5926 | // i64 __fpr; |
| 5927 | // i8 *__overflow_arg_area; |
| 5928 | // i8 *__reg_save_area; |
| 5929 | // }; |
| 5930 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5931 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5932 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5933 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5934 | Ty = getContext().getCanonicalType(Ty); |
| 5935 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5936 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5937 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5938 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5939 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5940 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5941 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5942 | CharUnits UnpaddedSize; |
| 5943 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5944 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5945 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 5946 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5947 | } else { |
| 5948 | if (AI.getCoerceToType()) |
| 5949 | ArgTy = AI.getCoerceToType(); |
| 5950 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5951 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5952 | UnpaddedSize = TyInfo.first; |
| 5953 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5954 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5955 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 5956 | if (IsVector && UnpaddedSize > PaddedSize) |
| 5957 | PaddedSize = CharUnits::fromQuantity(16); |
| 5958 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5959 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5960 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5961 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5962 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5963 | llvm::Value *PaddedSizeV = |
| 5964 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5965 | |
| 5966 | if (IsVector) { |
| 5967 | // Work out the address of a vector argument on the stack. |
| 5968 | // Vector arguments are always passed in the high bits of a |
| 5969 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5970 | Address OverflowArgAreaPtr = |
| 5971 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5972 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5973 | Address OverflowArgArea = |
| 5974 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5975 | TyInfo.second); |
| 5976 | Address MemAddr = |
| 5977 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5978 | |
| 5979 | // Update overflow_arg_area_ptr pointer |
| 5980 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5981 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5982 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5983 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5984 | |
| 5985 | return MemAddr; |
| 5986 | } |
| 5987 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5988 | assert(PaddedSize.getQuantity() == 8); |
| 5989 | |
| 5990 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 5991 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5992 | if (InFPRs) { |
| 5993 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5994 | RegCountField = 1; // __fpr |
| 5995 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5996 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5997 | } else { |
| 5998 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5999 | RegCountField = 0; // __gpr |
| 6000 | RegSaveIndex = 2; // save offset for r2 |
| 6001 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6002 | } |
| 6003 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6004 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 6005 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 6006 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6007 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6008 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6009 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6010 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6011 | |
| 6012 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6013 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6014 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6015 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6016 | |
| 6017 | // Emit code to load the value if it was passed in registers. |
| 6018 | CGF.EmitBlock(InRegBlock); |
| 6019 | |
| 6020 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6021 | llvm::Value *ScaledRegCount = |
| 6022 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6023 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6024 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6025 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6026 | llvm::Value *RegOffset = |
| 6027 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6028 | Address RegSaveAreaPtr = |
| 6029 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 6030 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6031 | llvm::Value *RegSaveArea = |
| 6032 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6033 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6034 | "raw_reg_addr"), |
| 6035 | PaddedSize); |
| 6036 | Address RegAddr = |
| 6037 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6038 | |
| 6039 | // Update the register count |
| 6040 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6041 | llvm::Value *NewRegCount = |
| 6042 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6043 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6044 | CGF.EmitBranch(ContBlock); |
| 6045 | |
| 6046 | // Emit code to load the value if it was passed in memory. |
| 6047 | CGF.EmitBlock(InMemBlock); |
| 6048 | |
| 6049 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6050 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 6051 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 6052 | Address OverflowArgArea = |
| 6053 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6054 | PaddedSize); |
| 6055 | Address RawMemAddr = |
| 6056 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6057 | Address MemAddr = |
| 6058 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6059 | |
| 6060 | // Update overflow_arg_area_ptr pointer |
| 6061 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6062 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6063 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6064 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6065 | CGF.EmitBranch(ContBlock); |
| 6066 | |
| 6067 | // Return the appropriate result. |
| 6068 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6069 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6070 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6071 | |
| 6072 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6073 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6074 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6075 | |
| 6076 | return ResAddr; |
| 6077 | } |
| 6078 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6079 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6080 | if (RetTy->isVoidType()) |
| 6081 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6082 | if (isVectorArgumentType(RetTy)) |
| 6083 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6084 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6085 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6086 | return (isPromotableIntegerType(RetTy) ? |
| 6087 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6088 | } |
| 6089 | |
| 6090 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6091 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6092 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6093 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6094 | |
| 6095 | // Integers and enums are extended to full register width. |
| 6096 | if (isPromotableIntegerType(Ty)) |
| 6097 | return ABIArgInfo::getExtend(); |
| 6098 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6099 | // Handle vector types and vector-like structure types. Note that |
| 6100 | // as opposed to float-like structure types, we do not allow any |
| 6101 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6102 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6103 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6104 | if (isVectorArgumentType(SingleElementTy) && |
| 6105 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6106 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6107 | |
| 6108 | // 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] | 6109 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6110 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6111 | |
| 6112 | // Handle small structures. |
| 6113 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6114 | // Structures with flexible arrays have variable length, so really |
| 6115 | // fail the size test above. |
| 6116 | const RecordDecl *RD = RT->getDecl(); |
| 6117 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6118 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6119 | |
| 6120 | // The structure is passed as an unextended integer, a float, or a double. |
| 6121 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6122 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6123 | assert(Size == 32 || Size == 64); |
| 6124 | if (Size == 32) |
| 6125 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6126 | else |
| 6127 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6128 | } else |
| 6129 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6130 | return ABIArgInfo::getDirect(PassTy); |
| 6131 | } |
| 6132 | |
| 6133 | // Non-structure compounds are passed indirectly. |
| 6134 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6135 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6136 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6137 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6138 | } |
| 6139 | |
| 6140 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6141 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6142 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6143 | |
| 6144 | namespace { |
| 6145 | |
| 6146 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6147 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6148 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6149 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6150 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6151 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6152 | }; |
| 6153 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6154 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6155 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6156 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6157 | llvm::GlobalValue *GV, |
| 6158 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6159 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6160 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 6161 | // Handle 'interrupt' attribute: |
| 6162 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6163 | |
| 6164 | // Step 1: Set ISR calling convention. |
| 6165 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 6166 | |
| 6167 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6168 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6169 | |
| 6170 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 6171 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 6172 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 6173 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6174 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6175 | } |
| 6176 | } |
| 6177 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6178 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6179 | // MIPS ABI Implementation. This works for both little-endian and |
| 6180 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6181 | //===----------------------------------------------------------------------===// |
| 6182 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6183 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6184 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6185 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6186 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6187 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6188 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6189 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6190 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6191 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6192 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6193 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6194 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6195 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6196 | |
| 6197 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6198 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6199 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6200 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6201 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6202 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6203 | }; |
| 6204 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6205 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6206 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6207 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6208 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6209 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6210 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6211 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6212 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6213 | return 29; |
| 6214 | } |
| 6215 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6216 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6217 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6218 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6219 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6220 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6221 | if (FD->hasAttr<Mips16Attr>()) { |
| 6222 | Fn->addFnAttr("mips16"); |
| 6223 | } |
| 6224 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6225 | Fn->addFnAttr("nomips16"); |
| 6226 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6227 | |
| 6228 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6229 | if (!Attr) |
| 6230 | return; |
| 6231 | |
| 6232 | const char *Kind; |
| 6233 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6234 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6235 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6236 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6237 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6238 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6239 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6240 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6241 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6242 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6243 | } |
| 6244 | |
| 6245 | Fn->addFnAttr("interrupt", Kind); |
| 6246 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6247 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6248 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6249 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6250 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6251 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6252 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6253 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6254 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6255 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6256 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6257 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6258 | void MipsABIInfo::CoerceToIntArgs( |
| 6259 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6260 | llvm::IntegerType *IntTy = |
| 6261 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6262 | |
| 6263 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6264 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6265 | ArgList.push_back(IntTy); |
| 6266 | |
| 6267 | // If necessary, add one more integer type to ArgList. |
| 6268 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6269 | |
| 6270 | if (R) |
| 6271 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6272 | } |
| 6273 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6274 | // In N32/64, an aligned double precision floating point field is passed in |
| 6275 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6276 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6277 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6278 | |
| 6279 | if (IsO32) { |
| 6280 | CoerceToIntArgs(TySize, ArgList); |
| 6281 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6282 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6283 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6284 | if (Ty->isComplexType()) |
| 6285 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6286 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6287 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6288 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6289 | // Unions/vectors are passed in integer registers. |
| 6290 | if (!RT || !RT->isStructureOrClassType()) { |
| 6291 | CoerceToIntArgs(TySize, ArgList); |
| 6292 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6293 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6294 | |
| 6295 | const RecordDecl *RD = RT->getDecl(); |
| 6296 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6297 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6298 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6299 | uint64_t LastOffset = 0; |
| 6300 | unsigned idx = 0; |
| 6301 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6302 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6303 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6304 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6305 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6306 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6307 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6308 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6309 | |
| 6310 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6311 | continue; |
| 6312 | |
| 6313 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6314 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6315 | continue; |
| 6316 | |
| 6317 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6318 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6319 | ArgList.push_back(I64); |
| 6320 | |
| 6321 | // Add double type. |
| 6322 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6323 | LastOffset = Offset + 64; |
| 6324 | } |
| 6325 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6326 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6327 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6328 | |
| 6329 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6330 | } |
| 6331 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6332 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6333 | uint64_t Offset) const { |
| 6334 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6335 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6336 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6337 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6338 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6339 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6340 | ABIArgInfo |
| 6341 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6342 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6343 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6344 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6345 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6346 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6347 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6348 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6349 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6350 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6351 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6352 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6353 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6354 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6355 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6356 | return ABIArgInfo::getIgnore(); |
| 6357 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6358 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6359 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6360 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6361 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6362 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6363 | // If we have reached here, aggregates are passed directly by coercing to |
| 6364 | // another structure type. Padding is inserted if the offset of the |
| 6365 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6366 | ABIArgInfo ArgInfo = |
| 6367 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6368 | getPaddingType(OrigOffset, CurrOffset)); |
| 6369 | ArgInfo.setInReg(true); |
| 6370 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
| 6373 | // Treat an enum type as its underlying type. |
| 6374 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6375 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6376 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6377 | // All integral types are promoted to the GPR width. |
| 6378 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6379 | return ABIArgInfo::getExtend(); |
| 6380 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6381 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6382 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6383 | } |
| 6384 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6385 | llvm::Type* |
| 6386 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6387 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6388 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6389 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6390 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6391 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6392 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6393 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6394 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6395 | // N32/64 returns struct/classes in floating point registers if the |
| 6396 | // following conditions are met: |
| 6397 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6398 | // 2. The struct/class has one or two fields all of which are floating |
| 6399 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6400 | // 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] | 6401 | // |
| 6402 | // Any other composite results are returned in integer registers. |
| 6403 | // |
| 6404 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6405 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6406 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6407 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6408 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6409 | if (!BT || !BT->isFloatingPoint()) |
| 6410 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6411 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6412 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6413 | } |
| 6414 | |
| 6415 | if (b == e) |
| 6416 | return llvm::StructType::get(getVMContext(), RTList, |
| 6417 | RD->hasAttr<PackedAttr>()); |
| 6418 | |
| 6419 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6420 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6421 | } |
| 6422 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6423 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6424 | return llvm::StructType::get(getVMContext(), RTList); |
| 6425 | } |
| 6426 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6427 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6428 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6429 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6430 | if (RetTy->isVoidType()) |
| 6431 | return ABIArgInfo::getIgnore(); |
| 6432 | |
| 6433 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6434 | // However, N32/N64 ignores zero sized return values. |
| 6435 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6436 | return ABIArgInfo::getIgnore(); |
| 6437 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6438 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6439 | if (Size <= 128) { |
| 6440 | if (RetTy->isAnyComplexType()) |
| 6441 | return ABIArgInfo::getDirect(); |
| 6442 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6443 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6444 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6445 | if (!IsO32 || |
| 6446 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6447 | ABIArgInfo ArgInfo = |
| 6448 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6449 | ArgInfo.setInReg(true); |
| 6450 | return ArgInfo; |
| 6451 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6452 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6453 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6454 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6455 | } |
| 6456 | |
| 6457 | // Treat an enum type as its underlying type. |
| 6458 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6459 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6460 | |
| 6461 | return (RetTy->isPromotableIntegerType() ? |
| 6462 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6463 | } |
| 6464 | |
| 6465 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6466 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6467 | if (!getCXXABI().classifyReturnType(FI)) |
| 6468 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6469 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6470 | // 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] | 6471 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6472 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6473 | for (auto &I : FI.arguments()) |
| 6474 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6475 | } |
| 6476 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6477 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6478 | QualType OrigTy) const { |
| 6479 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6480 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6481 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6482 | // 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] | 6483 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6484 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6485 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6486 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6487 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6488 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6489 | DidPromote = true; |
| 6490 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6491 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6492 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6493 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6494 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6495 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6496 | // The alignment of things in the argument area is never larger than |
| 6497 | // StackAlignInBytes. |
| 6498 | TyInfo.second = |
| 6499 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6500 | |
| 6501 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6502 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6503 | |
| 6504 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6505 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6506 | |
| 6507 | |
| 6508 | // If there was a promotion, "unpromote" into a temporary. |
| 6509 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6510 | if (DidPromote) { |
| 6511 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6512 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6513 | |
| 6514 | // Truncate down to the right width. |
| 6515 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6516 | : CGF.IntPtrTy); |
| 6517 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6518 | if (OrigTy->isPointerType()) |
| 6519 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6520 | |
| 6521 | CGF.Builder.CreateStore(V, Temp); |
| 6522 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6523 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6524 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6525 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6526 | } |
| 6527 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6528 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6529 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6530 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6531 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 6532 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 6533 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6534 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6535 | return false; |
| 6536 | } |
| 6537 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6538 | bool |
| 6539 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6540 | llvm::Value *Address) const { |
| 6541 | // This information comes from gcc's implementation, which seems to |
| 6542 | // as canonical as it gets. |
| 6543 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6544 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 6545 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6546 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6547 | |
| 6548 | // 0-31 are the general purpose registers, $0 - $31. |
| 6549 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 6550 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 6551 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6552 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6553 | |
| 6554 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 6555 | // They are one bit wide and ignored here. |
| 6556 | |
| 6557 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 6558 | // (coprocessor 1 is the FP unit) |
| 6559 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 6560 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 6561 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6562 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6563 | return false; |
| 6564 | } |
| 6565 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6566 | //===----------------------------------------------------------------------===// |
| 6567 | // 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] | 6568 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6569 | // handling. |
| 6570 | //===----------------------------------------------------------------------===// |
| 6571 | |
| 6572 | namespace { |
| 6573 | |
| 6574 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 6575 | public: |
| 6576 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 6577 | : DefaultTargetCodeGenInfo(CGT) {} |
| 6578 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6579 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6580 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6581 | }; |
| 6582 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6583 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6584 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6585 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6586 | if (!FD) return; |
| 6587 | |
| 6588 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6589 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6590 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6591 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 6592 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6593 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 6594 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 6595 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6596 | // Convert the reqd_work_group_size() attributes to metadata. |
| 6597 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6598 | llvm::NamedMDNode *OpenCLMetadata = |
| 6599 | M.getModule().getOrInsertNamedMetadata( |
| 6600 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6601 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6602 | SmallVector<llvm::Metadata *, 5> Operands; |
| 6603 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6604 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6605 | Operands.push_back( |
| 6606 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6607 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 6608 | Operands.push_back( |
| 6609 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6610 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 6611 | Operands.push_back( |
| 6612 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6613 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6614 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6615 | // Add a boolean constant operand for "required" (true) or "hint" |
| 6616 | // (false) for implementing the work_group_size_hint attr later. |
| 6617 | // 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] | 6618 | Operands.push_back( |
| 6619 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6620 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 6621 | } |
| 6622 | } |
| 6623 | } |
| 6624 | } |
| 6625 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6626 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6627 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6628 | //===----------------------------------------------------------------------===// |
| 6629 | // Hexagon ABI Implementation |
| 6630 | //===----------------------------------------------------------------------===// |
| 6631 | |
| 6632 | namespace { |
| 6633 | |
| 6634 | class HexagonABIInfo : public ABIInfo { |
| 6635 | |
| 6636 | |
| 6637 | public: |
| 6638 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6639 | |
| 6640 | private: |
| 6641 | |
| 6642 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6643 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 6644 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6645 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6646 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6647 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6648 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6649 | }; |
| 6650 | |
| 6651 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6652 | public: |
| 6653 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6654 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 6655 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6656 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6657 | return 29; |
| 6658 | } |
| 6659 | }; |
| 6660 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6661 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6662 | |
| 6663 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6664 | if (!getCXXABI().classifyReturnType(FI)) |
| 6665 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6666 | for (auto &I : FI.arguments()) |
| 6667 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6668 | } |
| 6669 | |
| 6670 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 6671 | if (!isAggregateTypeForABI(Ty)) { |
| 6672 | // Treat an enum type as its underlying type. |
| 6673 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6674 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6675 | |
| 6676 | return (Ty->isPromotableIntegerType() ? |
| 6677 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6678 | } |
| 6679 | |
| 6680 | // Ignore empty records. |
| 6681 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6682 | return ABIArgInfo::getIgnore(); |
| 6683 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6684 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6685 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6686 | |
| 6687 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6688 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6689 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6690 | // Pass in the smallest viable integer type. |
| 6691 | else if (Size > 32) |
| 6692 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6693 | else if (Size > 16) |
| 6694 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6695 | else if (Size > 8) |
| 6696 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6697 | else |
| 6698 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6699 | } |
| 6700 | |
| 6701 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6702 | if (RetTy->isVoidType()) |
| 6703 | return ABIArgInfo::getIgnore(); |
| 6704 | |
| 6705 | // Large vector types should be returned via memory. |
| 6706 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6707 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6708 | |
| 6709 | if (!isAggregateTypeForABI(RetTy)) { |
| 6710 | // Treat an enum type as its underlying type. |
| 6711 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6712 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6713 | |
| 6714 | return (RetTy->isPromotableIntegerType() ? |
| 6715 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6716 | } |
| 6717 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6718 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6719 | return ABIArgInfo::getIgnore(); |
| 6720 | |
| 6721 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6722 | // are returned indirectly. |
| 6723 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6724 | if (Size <= 64) { |
| 6725 | // Return in the smallest viable integer type. |
| 6726 | if (Size <= 8) |
| 6727 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6728 | if (Size <= 16) |
| 6729 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6730 | if (Size <= 32) |
| 6731 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6732 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6733 | } |
| 6734 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6735 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6736 | } |
| 6737 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6738 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6739 | QualType Ty) const { |
| 6740 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 6741 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6742 | getContext().getTypeInfoInChars(Ty), |
| 6743 | CharUnits::fromQuantity(4), |
| 6744 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6745 | } |
| 6746 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6747 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6748 | // Lanai ABI Implementation |
| 6749 | //===----------------------------------------------------------------------===// |
| 6750 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 6751 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6752 | class LanaiABIInfo : public DefaultABIInfo { |
| 6753 | public: |
| 6754 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 6755 | |
| 6756 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 6757 | |
| 6758 | void computeInfo(CGFunctionInfo &FI) const override { |
| 6759 | CCState State(FI.getCallingConvention()); |
| 6760 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 6761 | // regparm attribute set. |
| 6762 | if (FI.getHasRegParm()) { |
| 6763 | State.FreeRegs = FI.getRegParm(); |
| 6764 | } else { |
| 6765 | State.FreeRegs = 4; |
| 6766 | } |
| 6767 | |
| 6768 | if (!getCXXABI().classifyReturnType(FI)) |
| 6769 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 6770 | for (auto &I : FI.arguments()) |
| 6771 | I.info = classifyArgumentType(I.type, State); |
| 6772 | } |
| 6773 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6774 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6775 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 6776 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 6777 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6778 | |
| 6779 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 6780 | unsigned Size = getContext().getTypeSize(Ty); |
| 6781 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 6782 | |
| 6783 | if (SizeInRegs == 0) |
| 6784 | return false; |
| 6785 | |
| 6786 | if (SizeInRegs > State.FreeRegs) { |
| 6787 | State.FreeRegs = 0; |
| 6788 | return false; |
| 6789 | } |
| 6790 | |
| 6791 | State.FreeRegs -= SizeInRegs; |
| 6792 | |
| 6793 | return true; |
| 6794 | } |
| 6795 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6796 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 6797 | CCState &State) const { |
| 6798 | if (!ByVal) { |
| 6799 | if (State.FreeRegs) { |
| 6800 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 6801 | return getNaturalAlignIndirectInReg(Ty); |
| 6802 | } |
| 6803 | return getNaturalAlignIndirect(Ty, false); |
| 6804 | } |
| 6805 | |
| 6806 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 6807 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6808 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 6809 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 6810 | /*Realign=*/TypeAlign > |
| 6811 | MinABIStackAlignInBytes); |
| 6812 | } |
| 6813 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6814 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 6815 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6816 | // Check with the C++ ABI first. |
| 6817 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 6818 | if (RT) { |
| 6819 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 6820 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 6821 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 6822 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 6823 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 6824 | } |
| 6825 | } |
| 6826 | |
| 6827 | if (isAggregateTypeForABI(Ty)) { |
| 6828 | // Structures with flexible arrays are always indirect. |
| 6829 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 6830 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 6831 | |
| 6832 | // Ignore empty structs/unions. |
| 6833 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6834 | return ABIArgInfo::getIgnore(); |
| 6835 | |
| 6836 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 6837 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 6838 | if (SizeInRegs <= State.FreeRegs) { |
| 6839 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 6840 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 6841 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 6842 | State.FreeRegs -= SizeInRegs; |
| 6843 | return ABIArgInfo::getDirectInReg(Result); |
| 6844 | } else { |
| 6845 | State.FreeRegs = 0; |
| 6846 | } |
| 6847 | return getIndirectResult(Ty, true, State); |
| 6848 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6849 | |
| 6850 | // Treat an enum type as its underlying type. |
| 6851 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 6852 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6853 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6854 | bool InReg = shouldUseInReg(Ty, State); |
| 6855 | if (Ty->isPromotableIntegerType()) { |
| 6856 | if (InReg) |
| 6857 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6858 | return ABIArgInfo::getExtend(); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 6859 | } |
| 6860 | if (InReg) |
| 6861 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 6862 | return ABIArgInfo::getDirect(); |
| 6863 | } |
| 6864 | |
| 6865 | namespace { |
| 6866 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6867 | public: |
| 6868 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 6869 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 6870 | }; |
| 6871 | } |
| 6872 | |
| 6873 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6874 | // AMDGPU ABI Implementation |
| 6875 | //===----------------------------------------------------------------------===// |
| 6876 | |
| 6877 | namespace { |
| 6878 | |
| 6879 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6880 | public: |
| 6881 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6882 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6883 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6884 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 6885 | unsigned getOpenCLKernelCallingConv() const override; |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 6886 | unsigned getOpenCLImageAddrSpace(CodeGen::CodeGenModule &CGM) const override; |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6887 | }; |
| 6888 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6889 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6890 | |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 6891 | static void appendOpenCLVersionMD (CodeGen::CodeGenModule &CGM); |
| 6892 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6893 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6894 | const Decl *D, |
| 6895 | llvm::GlobalValue *GV, |
| 6896 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6897 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6898 | if (!FD) |
| 6899 | return; |
| 6900 | |
| 6901 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6902 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6903 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6904 | if (NumVGPR != 0) |
| 6905 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6906 | } |
| 6907 | |
| 6908 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6909 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6910 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6911 | if (NumSGPR != 0) |
| 6912 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6913 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6914 | |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 6915 | appendOpenCLVersionMD(M); |
| 6916 | } |
| 6917 |
|
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6918 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 6919 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 6920 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 6921 | } |
| 6922 | |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 6923 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLImageAddrSpace(CodeGen::CodeGenModule &CGM) const { |
| 6924 | return CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant); |
| 6925 | } |
| 6926 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6927 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 6928 | // SPARC v8 ABI Implementation. |
| 6929 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6930 | // |
| 6931 | // Ensures that complex values are passed in registers. |
| 6932 | // |
| 6933 | namespace { |
| 6934 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 6935 | public: |
| 6936 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 6937 | |
| 6938 | private: |
| 6939 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6940 | void computeInfo(CGFunctionInfo &FI) const override; |
| 6941 | }; |
| 6942 | } // end anonymous namespace |
| 6943 | |
| 6944 | |
| 6945 | ABIArgInfo |
| 6946 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 6947 | if (Ty->isAnyComplexType()) { |
| 6948 | return ABIArgInfo::getDirect(); |
| 6949 | } |
| 6950 | else { |
| 6951 | return DefaultABIInfo::classifyReturnType(Ty); |
| 6952 | } |
| 6953 | } |
| 6954 | |
| 6955 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6956 | |
| 6957 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 6958 | for (auto &Arg : FI.arguments()) |
| 6959 | Arg.info = classifyArgumentType(Arg.type); |
| 6960 | } |
| 6961 | |
| 6962 | namespace { |
| 6963 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6964 | public: |
| 6965 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6966 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 6967 | }; |
| 6968 | } // end anonymous namespace |
| 6969 | |
| 6970 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6971 | // SPARC v9 ABI Implementation. |
| 6972 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6973 | // |
| 6974 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6975 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6976 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6977 | // |
| 6978 | // One case requires special care: |
| 6979 | // |
| 6980 | // struct mixed { |
| 6981 | // int i; |
| 6982 | // float f; |
| 6983 | // }; |
| 6984 | // |
| 6985 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6986 | // parameter array, but the int is passed in an integer register, and the float |
| 6987 | // is passed in a floating point register. This is represented as two arguments |
| 6988 | // with the LLVM IR inreg attribute: |
| 6989 | // |
| 6990 | // declare void f(i32 inreg %i, float inreg %f) |
| 6991 | // |
| 6992 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6993 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6994 | // bytes. |
| 6995 | // |
| 6996 | namespace { |
| 6997 | class SparcV9ABIInfo : public ABIInfo { |
| 6998 | public: |
| 6999 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7000 | |
| 7001 | private: |
| 7002 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7003 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7004 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7005 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7006 | |
| 7007 | // Coercion type builder for structs passed in registers. The coercion type |
| 7008 | // serves two purposes: |
| 7009 | // |
| 7010 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 7011 | // in registers. |
| 7012 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 7013 | // code generator knows to pass them in floating point registers. |
| 7014 | // |
| 7015 | // We also compute the InReg flag which indicates that the struct contains |
| 7016 | // aligned 32-bit floats. |
| 7017 | // |
| 7018 | struct CoerceBuilder { |
| 7019 | llvm::LLVMContext &Context; |
| 7020 | const llvm::DataLayout &DL; |
| 7021 | SmallVector<llvm::Type*, 8> Elems; |
| 7022 | uint64_t Size; |
| 7023 | bool InReg; |
| 7024 | |
| 7025 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 7026 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 7027 | |
| 7028 | // Pad Elems with integers until Size is ToSize. |
| 7029 | void pad(uint64_t ToSize) { |
| 7030 | assert(ToSize >= Size && "Cannot remove elements"); |
| 7031 | if (ToSize == Size) |
| 7032 | return; |
| 7033 | |
| 7034 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7035 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7036 | if (Aligned > Size && Aligned <= ToSize) { |
| 7037 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 7038 | Size = Aligned; |
| 7039 | } |
| 7040 | |
| 7041 | // Add whole 64-bit words. |
| 7042 | while (Size + 64 <= ToSize) { |
| 7043 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 7044 | Size += 64; |
| 7045 | } |
| 7046 | |
| 7047 | // Final in-word padding. |
| 7048 | if (Size < ToSize) { |
| 7049 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 7050 | Size = ToSize; |
| 7051 | } |
| 7052 | } |
| 7053 | |
| 7054 | // Add a floating point element at Offset. |
| 7055 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 7056 | // Unaligned floats are treated as integers. |
| 7057 | if (Offset % Bits) |
| 7058 | return; |
| 7059 | // The InReg flag is only required if there are any floats < 64 bits. |
| 7060 | if (Bits < 64) |
| 7061 | InReg = true; |
| 7062 | pad(Offset); |
| 7063 | Elems.push_back(Ty); |
| 7064 | Size = Offset + Bits; |
| 7065 | } |
| 7066 | |
| 7067 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 7068 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 7069 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 7070 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 7071 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 7072 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 7073 | switch (ElemTy->getTypeID()) { |
| 7074 | case llvm::Type::StructTyID: |
| 7075 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 7076 | break; |
| 7077 | case llvm::Type::FloatTyID: |
| 7078 | addFloat(ElemOffset, ElemTy, 32); |
| 7079 | break; |
| 7080 | case llvm::Type::DoubleTyID: |
| 7081 | addFloat(ElemOffset, ElemTy, 64); |
| 7082 | break; |
| 7083 | case llvm::Type::FP128TyID: |
| 7084 | addFloat(ElemOffset, ElemTy, 128); |
| 7085 | break; |
| 7086 | case llvm::Type::PointerTyID: |
| 7087 | if (ElemOffset % 64 == 0) { |
| 7088 | pad(ElemOffset); |
| 7089 | Elems.push_back(ElemTy); |
| 7090 | Size += 64; |
| 7091 | } |
| 7092 | break; |
| 7093 | default: |
| 7094 | break; |
| 7095 | } |
| 7096 | } |
| 7097 | } |
| 7098 | |
| 7099 | // Check if Ty is a usable substitute for the coercion type. |
| 7100 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 7101 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
| 7104 | // Get the coercion type as a literal struct type. |
| 7105 | llvm::Type *getType() const { |
| 7106 | if (Elems.size() == 1) |
| 7107 | return Elems.front(); |
| 7108 | else |
| 7109 | return llvm::StructType::get(Context, Elems); |
| 7110 | } |
| 7111 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7112 | }; |
| 7113 | } // end anonymous namespace |
| 7114 | |
| 7115 | ABIArgInfo |
| 7116 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 7117 | if (Ty->isVoidType()) |
| 7118 | return ABIArgInfo::getIgnore(); |
| 7119 | |
| 7120 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7121 | |
| 7122 | // Anything too big to fit in registers is passed with an explicit indirect |
| 7123 | // pointer / sret pointer. |
| 7124 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7125 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7126 | |
| 7127 | // Treat an enum type as its underlying type. |
| 7128 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7129 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7130 | |
| 7131 | // Integer types smaller than a register are extended. |
| 7132 | if (Size < 64 && Ty->isIntegerType()) |
| 7133 | return ABIArgInfo::getExtend(); |
| 7134 | |
| 7135 | // Other non-aggregates go in registers. |
| 7136 | if (!isAggregateTypeForABI(Ty)) |
| 7137 | return ABIArgInfo::getDirect(); |
| 7138 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7139 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 7140 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 7141 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7142 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7143 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7144 | // 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] | 7145 | // Build a coercion type from the LLVM struct type. |
| 7146 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7147 | if (!StrTy) |
| 7148 | return ABIArgInfo::getDirect(); |
| 7149 | |
| 7150 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 7151 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7152 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7153 | |
| 7154 | // Try to use the original type for coercion. |
| 7155 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 7156 | |
| 7157 | if (CB.InReg) |
| 7158 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 7159 | else |
| 7160 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7161 | } |
| 7162 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7163 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7164 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7165 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 7166 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7167 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7168 | AI.setCoerceToType(ArgTy); |
| 7169 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7170 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7171 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7172 | CGBuilderTy &Builder = CGF.Builder; |
| 7173 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 7174 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 7175 | |
| 7176 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 7177 | |
| 7178 | Address ArgAddr = Address::invalid(); |
| 7179 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7180 | switch (AI.getKind()) { |
| 7181 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 7182 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 7183 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7184 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 7185 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7186 | case ABIArgInfo::Extend: { |
| 7187 | Stride = SlotSize; |
| 7188 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 7189 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7190 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7191 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7192 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7193 | case ABIArgInfo::Direct: { |
| 7194 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7195 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7196 | ArgAddr = Addr; |
| 7197 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7198 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7199 | |
| 7200 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7201 | Stride = SlotSize; |
| 7202 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 7203 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 7204 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7205 | break; |
| 7206 | |
| 7207 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7208 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7209 | } |
| 7210 | |
| 7211 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7212 | llvm::Value *NextPtr = |
| 7213 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 7214 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7215 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7216 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7217 | } |
| 7218 | |
| 7219 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7220 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7221 | for (auto &I : FI.arguments()) |
| 7222 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7223 | } |
| 7224 | |
| 7225 | namespace { |
| 7226 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7227 | public: |
| 7228 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7229 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7230 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7231 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7232 | return 14; |
| 7233 | } |
| 7234 | |
| 7235 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7236 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7237 | }; |
| 7238 | } // end anonymous namespace |
| 7239 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7240 | bool |
| 7241 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7242 | llvm::Value *Address) const { |
| 7243 | // This is calculated from the LLVM and GCC tables and verified |
| 7244 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 7245 | |
| 7246 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 7247 | |
| 7248 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 7249 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 7250 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 7251 | |
| 7252 | // 0-31: the 8-byte general-purpose registers |
| 7253 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 7254 | |
| 7255 | // 32-63: f0-31, the 4-byte floating-point registers |
| 7256 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 7257 | |
| 7258 | // Y = 64 |
| 7259 | // PSR = 65 |
| 7260 | // WIM = 66 |
| 7261 | // TBR = 67 |
| 7262 | // PC = 68 |
| 7263 | // NPC = 69 |
| 7264 | // FSR = 70 |
| 7265 | // CSR = 71 |
| 7266 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7267 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 7268 | // 72-87: d0-15, the 8-byte floating-point registers |
| 7269 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 7270 | |
| 7271 | return false; |
| 7272 | } |
| 7273 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7274 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7275 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7276 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7277 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7278 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7279 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7280 | |
| 7281 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 7282 | /// it by reference between functions that append to it. |
| 7283 | typedef llvm::SmallString<128> SmallStringEnc; |
| 7284 | |
| 7285 | /// TypeStringCache caches the meta encodings of Types. |
| 7286 | /// |
| 7287 | /// The reason for caching TypeStrings is two fold: |
| 7288 | /// 1. To cache a type's encoding for later uses; |
| 7289 | /// 2. As a means to break recursive member type inclusion. |
| 7290 | /// |
| 7291 | /// A cache Entry can have a Status of: |
| 7292 | /// NonRecursive: The type encoding is not recursive; |
| 7293 | /// Recursive: The type encoding is recursive; |
| 7294 | /// Incomplete: An incomplete TypeString; |
| 7295 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 7296 | /// Recursive type encoding. |
| 7297 | /// |
| 7298 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 7299 | /// as possible. Whilst it may contain types which are recursive, the type |
| 7300 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 7301 | /// the type is encountered. |
| 7302 | /// |
| 7303 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 7304 | /// possible. The type itself is recursive and it may contain other types which |
| 7305 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 7306 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 7307 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 7308 | /// |
| 7309 | /// An Incomplete entry is always a RecordType and only encodes its |
| 7310 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 7311 | /// are placed into the cache during type expansion as a means to identify and |
| 7312 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 7313 | /// the entry becomes IncompleteUsed. |
| 7314 | /// |
| 7315 | /// During the expansion of a RecordType's members: |
| 7316 | /// |
| 7317 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 7318 | /// cached encoding is used; |
| 7319 | /// |
| 7320 | /// If the cache contains a Recursive encoding for the member type, the |
| 7321 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 7322 | /// |
| 7323 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 7324 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 7325 | /// |
| 7326 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 7327 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 7328 | /// it is swapped back in; |
| 7329 | /// |
| 7330 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 7331 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 7332 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 7333 | /// |
| 7334 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 7335 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 7336 | /// Else the member is part of a recursive type and thus the recursion has |
| 7337 | /// been exited too soon for the encoding to be correct for the member. |
| 7338 | /// |
| 7339 | class TypeStringCache { |
| 7340 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 7341 | struct Entry { |
| 7342 | std::string Str; // The encoded TypeString for the type. |
| 7343 | enum Status State; // Information about the encoding in 'Str'. |
| 7344 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 7345 | // during the expansion of RecordType's members. |
| 7346 | }; |
| 7347 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 7348 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 7349 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 7350 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 7351 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7352 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 7353 | bool removeIncomplete(const IdentifierInfo *ID); |
| 7354 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7355 | bool IsRecursive); |
| 7356 | StringRef lookupStr(const IdentifierInfo *ID); |
| 7357 | }; |
| 7358 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7359 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7360 | /// FieldEncoding is a helper for this ordering process. |
| 7361 | class FieldEncoding { |
| 7362 | bool HasName; |
| 7363 | std::string Enc; |
| 7364 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 7365 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
| 7366 | StringRef str() {return Enc.c_str();} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7367 | bool operator<(const FieldEncoding &rhs) const { |
| 7368 | if (HasName != rhs.HasName) return HasName; |
| 7369 | return Enc < rhs.Enc; |
| 7370 | } |
| 7371 | }; |
| 7372 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7373 | class XCoreABIInfo : public DefaultABIInfo { |
| 7374 | public: |
| 7375 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7376 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7377 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7378 | }; |
| 7379 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7380 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7381 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7382 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7383 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7384 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 7385 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7386 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7387 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7388 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7389 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7390 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 7391 | // TODO: this implementation is likely now redundant with the default |
| 7392 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7393 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7394 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7395 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7396 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7397 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7398 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 7399 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7400 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7401 | // Handle the argument. |
| 7402 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7403 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7404 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7405 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7406 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7407 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7408 | |
| 7409 | Address Val = Address::invalid(); |
| 7410 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7411 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7412 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 7413 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 7414 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7415 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 7416 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7417 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 7418 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7419 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7420 | case ABIArgInfo::Extend: |
| 7421 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7422 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 7423 | ArgSize = CharUnits::fromQuantity( |
| 7424 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7425 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7426 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7427 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7428 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 7429 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 7430 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7431 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7432 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7433 | |
| 7434 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7435 | if (!ArgSize.isZero()) { |
| 7436 | llvm::Value *APN = |
| 7437 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 7438 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7439 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7440 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7441 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7442 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7443 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7444 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 7445 | /// into the cache as a means to identify and break recursion. |
| 7446 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 7447 | /// be reinserted by removeIncomplete(). |
| 7448 | /// All other types of encoding should have been used rather than arriving here. |
| 7449 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 7450 | std::string StubEnc) { |
| 7451 | if (!ID) |
| 7452 | return; |
| 7453 | Entry &E = Map[ID]; |
| 7454 | assert( (E.Str.empty() || E.State == Recursive) && |
| 7455 | "Incorrectly use of addIncomplete"); |
| 7456 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 7457 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 7458 | E.Str.swap(StubEnc); |
| 7459 | E.State = Incomplete; |
| 7460 | ++IncompleteCount; |
| 7461 | } |
| 7462 | |
| 7463 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 7464 | /// must be removed from the cache. |
| 7465 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 7466 | /// Returns true if the RecordType was defined recursively. |
| 7467 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 7468 | if (!ID) |
| 7469 | return false; |
| 7470 | auto I = Map.find(ID); |
| 7471 | assert(I != Map.end() && "Entry not present"); |
| 7472 | Entry &E = I->second; |
| 7473 | assert( (E.State == Incomplete || |
| 7474 | E.State == IncompleteUsed) && |
| 7475 | "Entry must be an incomplete type"); |
| 7476 | bool IsRecursive = false; |
| 7477 | if (E.State == IncompleteUsed) { |
| 7478 | // We made use of our Incomplete encoding, thus we are recursive. |
| 7479 | IsRecursive = true; |
| 7480 | --IncompleteUsedCount; |
| 7481 | } |
| 7482 | if (E.Swapped.empty()) |
| 7483 | Map.erase(I); |
| 7484 | else { |
| 7485 | // Swap the Recursive back. |
| 7486 | E.Swapped.swap(E.Str); |
| 7487 | E.Swapped.clear(); |
| 7488 | E.State = Recursive; |
| 7489 | } |
| 7490 | --IncompleteCount; |
| 7491 | return IsRecursive; |
| 7492 | } |
| 7493 | |
| 7494 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 7495 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 7496 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7497 | bool IsRecursive) { |
| 7498 | if (!ID || IncompleteUsedCount) |
| 7499 | return; // No key or it is is an incomplete sub-type so don't add. |
| 7500 | Entry &E = Map[ID]; |
| 7501 | if (IsRecursive && !E.Str.empty()) { |
| 7502 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 7503 | "This is not the same Recursive entry"); |
| 7504 | // The parent container was not recursive after all, so we could have used |
| 7505 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 7506 | // we started viz: IncompleteCount!=0. |
| 7507 | return; |
| 7508 | } |
| 7509 | assert(E.Str.empty() && "Entry already present"); |
| 7510 | E.Str = Str.str(); |
| 7511 | E.State = IsRecursive? Recursive : NonRecursive; |
| 7512 | } |
| 7513 | |
| 7514 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 7515 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 7516 | /// encoding is Recursive, return an empty StringRef. |
| 7517 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 7518 | if (!ID) |
| 7519 | return StringRef(); // We have no key. |
| 7520 | auto I = Map.find(ID); |
| 7521 | if (I == Map.end()) |
| 7522 | return StringRef(); // We have no encoding. |
| 7523 | Entry &E = I->second; |
| 7524 | if (E.State == Recursive && IncompleteCount) |
| 7525 | return StringRef(); // We don't use Recursive encodings for member types. |
| 7526 | |
| 7527 | if (E.State == Incomplete) { |
| 7528 | // The incomplete type is being used to break out of recursion. |
| 7529 | E.State = IncompleteUsed; |
| 7530 | ++IncompleteUsedCount; |
| 7531 | } |
| 7532 | return E.Str.c_str(); |
| 7533 | } |
| 7534 | |
| 7535 | /// The XCore ABI includes a type information section that communicates symbol |
| 7536 | /// type information to the linker. The linker uses this information to verify |
| 7537 | /// safety/correctness of things such as array bound and pointers et al. |
| 7538 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 7539 | /// This type information (TypeString) is emitted into meta data for all global |
| 7540 | /// symbols: definitions, declarations, functions & variables. |
| 7541 | /// |
| 7542 | /// The TypeString carries type, qualifier, name, size & value details. |
| 7543 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7544 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7545 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 7546 | /// |
| 7547 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7548 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 7549 | |
| 7550 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 7551 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7552 | CodeGen::CodeGenModule &CGM) const { |
| 7553 | SmallStringEnc Enc; |
| 7554 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 7555 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 7556 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 7557 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7558 | llvm::NamedMDNode *MD = |
| 7559 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 7560 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 7561 | } |
| 7562 | } |
| 7563 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 7564 | //===----------------------------------------------------------------------===// |
| 7565 | // SPIR ABI Implementation |
| 7566 | //===----------------------------------------------------------------------===// |
| 7567 | |
| 7568 | namespace { |
| 7569 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7570 | public: |
| 7571 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7572 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
| 7573 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7574 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7575 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 7576 | }; |
| 7577 | } // End anonymous namespace. |
| 7578 | |
| 7579 | /// Emit SPIR specific metadata: OpenCL and SPIR version. |
| 7580 | void SPIRTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7581 | CodeGen::CodeGenModule &CGM) const { |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 7582 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
| 7583 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
| 7584 | llvm::Module &M = CGM.getModule(); |
| 7585 | // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the |
| 7586 | // opencl.spir.version named metadata. |
| 7587 | llvm::Metadata *SPIRVerElts[] = { |
| 7588 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 2)), |
| 7589 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 0))}; |
| 7590 | llvm::NamedMDNode *SPIRVerMD = |
| 7591 | M.getOrInsertNamedMetadata("opencl.spir.version"); |
| 7592 | SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts)); |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7593 | appendOpenCLVersionMD(CGM); |
| 7594 | } |
| 7595 | |
| 7596 | static void appendOpenCLVersionMD (CodeGen::CodeGenModule &CGM) { |
| 7597 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
| 7598 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
| 7599 | llvm::Module &M = CGM.getModule(); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 7600 | // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the |
| 7601 | // opencl.ocl.version named metadata node. |
| 7602 | llvm::Metadata *OCLVerElts[] = { |
| 7603 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 7604 | Int32Ty, CGM.getLangOpts().OpenCLVersion / 100)), |
| 7605 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 7606 | Int32Ty, (CGM.getLangOpts().OpenCLVersion % 100) / 10))}; |
| 7607 | llvm::NamedMDNode *OCLVerMD = |
| 7608 | M.getOrInsertNamedMetadata("opencl.ocl.version"); |
| 7609 | OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts)); |
| 7610 | } |
| 7611 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7612 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7613 | return llvm::CallingConv::SPIR_KERNEL; |
| 7614 | } |
| 7615 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7616 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7617 | const CodeGen::CodeGenModule &CGM, |
| 7618 | TypeStringCache &TSC); |
| 7619 | |
| 7620 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7621 | /// Builds a SmallVector containing the encoded field types in declaration |
| 7622 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7623 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 7624 | const RecordDecl *RD, |
| 7625 | const CodeGen::CodeGenModule &CGM, |
| 7626 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7627 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7628 | SmallStringEnc Enc; |
| 7629 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7630 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7631 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7632 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7633 | Enc += "b("; |
| 7634 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7635 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7636 | Enc += ':'; |
| 7637 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7638 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7639 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7640 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7641 | Enc += ')'; |
| 7642 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 7643 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7644 | } |
| 7645 | return true; |
| 7646 | } |
| 7647 | |
| 7648 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 7649 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 7650 | /// Union types have their fields ordered according to the ABI. |
| 7651 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 7652 | const CodeGen::CodeGenModule &CGM, |
| 7653 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 7654 | // Append the cached TypeString if we have one. |
| 7655 | StringRef TypeString = TSC.lookupStr(ID); |
| 7656 | if (!TypeString.empty()) { |
| 7657 | Enc += TypeString; |
| 7658 | return true; |
| 7659 | } |
| 7660 | |
| 7661 | // Start to emit an incomplete TypeString. |
| 7662 | size_t Start = Enc.size(); |
| 7663 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 7664 | Enc += '('; |
| 7665 | if (ID) |
| 7666 | Enc += ID->getName(); |
| 7667 | Enc += "){"; |
| 7668 | |
| 7669 | // We collect all encoded fields and order as necessary. |
| 7670 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7671 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 7672 | if (RD && !RD->field_empty()) { |
| 7673 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 7674 | // so that recursive calls to this RecordType will use it whilst building a |
| 7675 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7676 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7677 | std::string StubEnc(Enc.substr(Start).str()); |
| 7678 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 7679 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 7680 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 7681 | (void) TSC.removeIncomplete(ID); |
| 7682 | return false; |
| 7683 | } |
| 7684 | IsRecursive = TSC.removeIncomplete(ID); |
| 7685 | // The ABI requires unions to be sorted but not structures. |
| 7686 | // See FieldEncoding::operator< for sort algorithm. |
| 7687 | if (RT->isUnionType()) |
| 7688 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7689 | // We can now complete the TypeString. |
| 7690 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7691 | for (unsigned I = 0; I != E; ++I) { |
| 7692 | if (I) |
| 7693 | Enc += ','; |
| 7694 | Enc += FE[I].str(); |
| 7695 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7696 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7697 | Enc += '}'; |
| 7698 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 7699 | return true; |
| 7700 | } |
| 7701 | |
| 7702 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 7703 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 7704 | TypeStringCache &TSC, |
| 7705 | const IdentifierInfo *ID) { |
| 7706 | // Append the cached TypeString if we have one. |
| 7707 | StringRef TypeString = TSC.lookupStr(ID); |
| 7708 | if (!TypeString.empty()) { |
| 7709 | Enc += TypeString; |
| 7710 | return true; |
| 7711 | } |
| 7712 | |
| 7713 | size_t Start = Enc.size(); |
| 7714 | Enc += "e("; |
| 7715 | if (ID) |
| 7716 | Enc += ID->getName(); |
| 7717 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7718 | |
| 7719 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7720 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7721 | SmallVector<FieldEncoding, 16> FE; |
| 7722 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 7723 | ++I) { |
| 7724 | SmallStringEnc EnumEnc; |
| 7725 | EnumEnc += "m("; |
| 7726 | EnumEnc += I->getName(); |
| 7727 | EnumEnc += "){"; |
| 7728 | I->getInitVal().toString(EnumEnc); |
| 7729 | EnumEnc += '}'; |
| 7730 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 7731 | } |
| 7732 | std::sort(FE.begin(), FE.end()); |
| 7733 | unsigned E = FE.size(); |
| 7734 | for (unsigned I = 0; I != E; ++I) { |
| 7735 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7736 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7737 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7738 | } |
| 7739 | } |
| 7740 | Enc += '}'; |
| 7741 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 7742 | return true; |
| 7743 | } |
| 7744 | |
| 7745 | /// Appends type's qualifier to Enc. |
| 7746 | /// This is done prior to appending the type's encoding. |
| 7747 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 7748 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 7749 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7750 | int Lookup = 0; |
| 7751 | if (QT.isConstQualified()) |
| 7752 | Lookup += 1<<0; |
| 7753 | if (QT.isRestrictQualified()) |
| 7754 | Lookup += 1<<1; |
| 7755 | if (QT.isVolatileQualified()) |
| 7756 | Lookup += 1<<2; |
| 7757 | Enc += Table[Lookup]; |
| 7758 | } |
| 7759 | |
| 7760 | /// Appends built-in types to Enc. |
| 7761 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 7762 | const char *EncType; |
| 7763 | switch (BT->getKind()) { |
| 7764 | case BuiltinType::Void: |
| 7765 | EncType = "0"; |
| 7766 | break; |
| 7767 | case BuiltinType::Bool: |
| 7768 | EncType = "b"; |
| 7769 | break; |
| 7770 | case BuiltinType::Char_U: |
| 7771 | EncType = "uc"; |
| 7772 | break; |
| 7773 | case BuiltinType::UChar: |
| 7774 | EncType = "uc"; |
| 7775 | break; |
| 7776 | case BuiltinType::SChar: |
| 7777 | EncType = "sc"; |
| 7778 | break; |
| 7779 | case BuiltinType::UShort: |
| 7780 | EncType = "us"; |
| 7781 | break; |
| 7782 | case BuiltinType::Short: |
| 7783 | EncType = "ss"; |
| 7784 | break; |
| 7785 | case BuiltinType::UInt: |
| 7786 | EncType = "ui"; |
| 7787 | break; |
| 7788 | case BuiltinType::Int: |
| 7789 | EncType = "si"; |
| 7790 | break; |
| 7791 | case BuiltinType::ULong: |
| 7792 | EncType = "ul"; |
| 7793 | break; |
| 7794 | case BuiltinType::Long: |
| 7795 | EncType = "sl"; |
| 7796 | break; |
| 7797 | case BuiltinType::ULongLong: |
| 7798 | EncType = "ull"; |
| 7799 | break; |
| 7800 | case BuiltinType::LongLong: |
| 7801 | EncType = "sll"; |
| 7802 | break; |
| 7803 | case BuiltinType::Float: |
| 7804 | EncType = "ft"; |
| 7805 | break; |
| 7806 | case BuiltinType::Double: |
| 7807 | EncType = "d"; |
| 7808 | break; |
| 7809 | case BuiltinType::LongDouble: |
| 7810 | EncType = "ld"; |
| 7811 | break; |
| 7812 | default: |
| 7813 | return false; |
| 7814 | } |
| 7815 | Enc += EncType; |
| 7816 | return true; |
| 7817 | } |
| 7818 | |
| 7819 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 7820 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 7821 | const CodeGen::CodeGenModule &CGM, |
| 7822 | TypeStringCache &TSC) { |
| 7823 | Enc += "p("; |
| 7824 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 7825 | return false; |
| 7826 | Enc += ')'; |
| 7827 | return true; |
| 7828 | } |
| 7829 | |
| 7830 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7831 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 7832 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7833 | const CodeGen::CodeGenModule &CGM, |
| 7834 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 7835 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 7836 | return false; |
| 7837 | Enc += "a("; |
| 7838 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 7839 | CAT->getSize().toStringUnsigned(Enc); |
| 7840 | else |
| 7841 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 7842 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7843 | // The Qualifiers should be attached to the type rather than the array. |
| 7844 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7845 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 7846 | return false; |
| 7847 | Enc += ')'; |
| 7848 | return true; |
| 7849 | } |
| 7850 | |
| 7851 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 7852 | /// and the arguments. |
| 7853 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 7854 | const CodeGen::CodeGenModule &CGM, |
| 7855 | TypeStringCache &TSC) { |
| 7856 | Enc += "f{"; |
| 7857 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 7858 | return false; |
| 7859 | Enc += "}("; |
| 7860 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 7861 | // N.B. we are only interested in the adjusted param types. |
| 7862 | auto I = FPT->param_type_begin(); |
| 7863 | auto E = FPT->param_type_end(); |
| 7864 | if (I != E) { |
| 7865 | do { |
| 7866 | if (!appendType(Enc, *I, CGM, TSC)) |
| 7867 | return false; |
| 7868 | ++I; |
| 7869 | if (I != E) |
| 7870 | Enc += ','; |
| 7871 | } while (I != E); |
| 7872 | if (FPT->isVariadic()) |
| 7873 | Enc += ",va"; |
| 7874 | } else { |
| 7875 | if (FPT->isVariadic()) |
| 7876 | Enc += "va"; |
| 7877 | else |
| 7878 | Enc += '0'; |
| 7879 | } |
| 7880 | } |
| 7881 | Enc += ')'; |
| 7882 | return true; |
| 7883 | } |
| 7884 | |
| 7885 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 7886 | /// type encodings. |
| 7887 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7888 | const CodeGen::CodeGenModule &CGM, |
| 7889 | TypeStringCache &TSC) { |
| 7890 | |
| 7891 | QualType QT = QType.getCanonicalType(); |
| 7892 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7893 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 7894 | // The Qualifiers should be attached to the type rather than the array. |
| 7895 | // Thus we don't call appendQualifier() here. |
| 7896 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 7897 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7898 | appendQualifier(Enc, QT); |
| 7899 | |
| 7900 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 7901 | return appendBuiltinType(Enc, BT); |
| 7902 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7903 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 7904 | return appendPointerType(Enc, PT, CGM, TSC); |
| 7905 | |
| 7906 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 7907 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 7908 | |
| 7909 | if (const RecordType *RT = QT->getAsStructureType()) |
| 7910 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7911 | |
| 7912 | if (const RecordType *RT = QT->getAsUnionType()) |
| 7913 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7914 | |
| 7915 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7916 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7917 | |
| 7918 | return false; |
| 7919 | } |
| 7920 | |
| 7921 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7922 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 7923 | if (!D) |
| 7924 | return false; |
| 7925 | |
| 7926 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7927 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 7928 | return false; |
| 7929 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 7930 | } |
| 7931 | |
| 7932 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7933 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 7934 | return false; |
| 7935 | QualType QT = VD->getType().getCanonicalType(); |
| 7936 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 7937 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7938 | // The Qualifiers should be attached to the type rather than the array. |
| 7939 | // Thus we don't call appendQualifier() here. |
| 7940 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7941 | } |
| 7942 | return appendType(Enc, QT, CGM, TSC); |
| 7943 | } |
| 7944 | return false; |
| 7945 | } |
| 7946 | |
| 7947 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7948 | //===----------------------------------------------------------------------===// |
| 7949 | // Driver code |
| 7950 | //===----------------------------------------------------------------------===// |
| 7951 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 7952 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 7953 | return getTarget().getTriple(); |
| 7954 | } |
| 7955 | |
| 7956 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 7957 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 7958 | } |
| 7959 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7960 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7961 | if (TheTargetCodeGenInfo) |
| 7962 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7963 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 7964 | // Helper to set the unique_ptr while still keeping the return value. |
| 7965 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 7966 | this->TheTargetCodeGenInfo.reset(P); |
| 7967 | return *P; |
| 7968 | }; |
| 7969 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7970 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 7971 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7972 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 7973 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7974 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 7975 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 7976 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7977 | case llvm::Triple::mips: |
| 7978 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 7979 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 7980 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 7981 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7982 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7983 | case llvm::Triple::mips64: |
| 7984 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 7985 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7986 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 7987 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 7988 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7989 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7990 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7991 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7992 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 7993 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7994 | } |
| 7995 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 7996 | case llvm::Triple::wasm32: |
| 7997 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 7998 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 7999 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8000 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 8001 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8002 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8003 | case llvm::Triple::thumbeb: { |
| 8004 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 8005 | return SetCGInfo( |
| 8006 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 8007 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8008 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8009 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 8010 | StringRef ABIStr = getTarget().getABI(); |
| 8011 | if (ABIStr == "apcs-gnu") |
| 8012 | Kind = ARMABIInfo::APCS; |
| 8013 | else if (ABIStr == "aapcs16") |
| 8014 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 8015 | else if (CodeGenOpts.FloatABI == "hard" || |
| 8016 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8017 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 8018 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8019 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8020 | Kind = ARMABIInfo::AAPCS_VFP; |
| 8021 | |
| 8022 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 8023 | } |
| 8024 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8025 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8026 | return SetCGInfo( |
| 8027 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 8028 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8029 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8030 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8031 | if (getTarget().getABI() == "elfv2") |
| 8032 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8033 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8034 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8035 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8036 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8037 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8038 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 8039 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8040 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8041 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8042 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8043 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8044 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8045 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8046 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8047 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 8048 | case llvm::Triple::nvptx: |
| 8049 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8050 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 8051 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8052 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8053 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8054 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8055 | case llvm::Triple::systemz: { |
| 8056 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8057 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8058 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 8059 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8060 | case llvm::Triple::tce: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8061 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8062 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8063 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8064 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 8065 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8066 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 8067 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 8068 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8069 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8070 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 8071 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8072 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8073 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8074 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 8075 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8076 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 8077 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8078 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8079 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8080 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8081 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8082 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8083 | X86AVXABILevel AVXLevel = |
| 8084 | (ABI == "avx512" |
| 8085 | ? X86AVXABILevel::AVX512 |
| 8086 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8087 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8088 | switch (Triple.getOS()) { |
| 8089 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8090 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 8091 | case llvm::Triple::PS4: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8092 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8093 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8094 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8095 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8096 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 8097 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8098 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 8099 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8100 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8101 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8102 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 8103 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8104 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 8105 | case llvm::Triple::sparc: |
| 8106 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8107 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8108 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8109 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8110 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8111 | case llvm::Triple::spir: |
| 8112 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8113 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8114 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8115 | } |