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" |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 17 | #include "CGBlocks.h" |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 18 | #include "CGCXXABI.h" |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 19 | #include "CGValue.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 20 | #include "CodeGenFunction.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 22 | #include "clang/CodeGen/CGFunctionInfo.h" |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 23 | #include "clang/CodeGen/SwiftCallingConv.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CodeGenOptions.h" |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringSwitch.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Triple.h" |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame^] | 28 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DataLayout.h" |
| 30 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 32 | #include <algorithm> // std::sort |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 33 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | using namespace CodeGen; |
| 36 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 37 | // Helper for coercing an aggregate argument or return value into an integer |
| 38 | // array of the same size (including padding) and alignment. This alternate |
| 39 | // coercion happens only for the RenderScript ABI and can be removed after |
| 40 | // runtimes that rely on it are no longer supported. |
| 41 | // |
| 42 | // RenderScript assumes that the size of the argument / return value in the IR |
| 43 | // is the same as the size of the corresponding qualified type. This helper |
| 44 | // coerces the aggregate type into an array of the same size (including |
| 45 | // padding). This coercion is used in lieu of expansion of struct members or |
| 46 | // other canonical coercions that return a coerced-type of larger size. |
| 47 | // |
| 48 | // Ty - The argument / return value type |
| 49 | // Context - The associated ASTContext |
| 50 | // LLVMContext - The associated LLVMContext |
| 51 | static ABIArgInfo coerceToIntArray(QualType Ty, |
| 52 | ASTContext &Context, |
| 53 | llvm::LLVMContext &LLVMContext) { |
| 54 | // Alignment and Size are measured in bits. |
| 55 | const uint64_t Size = Context.getTypeSize(Ty); |
| 56 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 57 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment); |
| 58 | const uint64_t NumElements = (Size + Alignment - 1) / Alignment; |
| 59 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 60 | } |
| 61 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 62 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 63 | llvm::Value *Array, |
| 64 | llvm::Value *Value, |
| 65 | unsigned FirstIndex, |
| 66 | unsigned LastIndex) { |
| 67 | // Alternatively, we could emit this as a loop in the source. |
| 68 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 69 | llvm::Value *Cell = |
| 70 | Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 71 | Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 75 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 76 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 77 | T->isMemberFunctionPointerType(); |
| 78 | } |
| 79 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 80 | ABIArgInfo |
| 81 | ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, |
| 82 | llvm::Type *Padding) const { |
| 83 | return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), |
| 84 | ByRef, Realign, Padding); |
| 85 | } |
| 86 | |
| 87 | ABIArgInfo |
| 88 | ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { |
| 89 | return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), |
| 90 | /*ByRef*/ false, Realign); |
| 91 | } |
| 92 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 93 | Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 94 | QualType Ty) const { |
| 95 | return Address::invalid(); |
| 96 | } |
| 97 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 98 | ABIInfo::~ABIInfo() {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 99 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 100 | /// Does the given lowering require more than the given number of |
| 101 | /// registers when expanded? |
| 102 | /// |
| 103 | /// This is intended to be the basis of a reasonable basic implementation |
| 104 | /// of should{Pass,Return}IndirectlyForSwift. |
| 105 | /// |
| 106 | /// For most targets, a limit of four total registers is reasonable; this |
| 107 | /// limits the amount of code required in order to move around the value |
| 108 | /// in case it wasn't produced immediately prior to the call by the caller |
| 109 | /// (or wasn't produced in exactly the right registers) or isn't used |
| 110 | /// immediately within the callee. But some targets may need to further |
| 111 | /// limit the register count due to an inability to support that many |
| 112 | /// return registers. |
| 113 | static bool occupiesMoreThan(CodeGenTypes &cgt, |
| 114 | ArrayRef<llvm::Type*> scalarTypes, |
| 115 | unsigned maxAllRegisters) { |
| 116 | unsigned intCount = 0, fpCount = 0; |
| 117 | for (llvm::Type *type : scalarTypes) { |
| 118 | if (type->isPointerTy()) { |
| 119 | intCount++; |
| 120 | } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) { |
| 121 | auto ptrWidth = cgt.getTarget().getPointerWidth(0); |
| 122 | intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth; |
| 123 | } else { |
| 124 | assert(type->isVectorTy() || type->isFloatingPointTy()); |
| 125 | fpCount++; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return (intCount + fpCount > maxAllRegisters); |
| 130 | } |
| 131 | |
| 132 | bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 133 | llvm::Type *eltTy, |
| 134 | unsigned numElts) const { |
| 135 | // The default implementation of this assumes that the target guarantees |
| 136 | // 128-bit SIMD support but nothing more. |
| 137 | return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16); |
| 138 | } |
| 139 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 140 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 141 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 142 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 143 | if (!RD) |
| 144 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 145 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 149 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 150 | const RecordType *RT = T->getAs<RecordType>(); |
| 151 | if (!RT) |
| 152 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 153 | return getRecordArgABI(RT, CXXABI); |
| 154 | } |
| 155 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 156 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 157 | /// should ensure that all elements of the union have the same "machine type". |
| 158 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 159 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 160 | const RecordDecl *UD = UT->getDecl(); |
| 161 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 162 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 163 | return UD->field_begin()->getType(); |
| 164 | } |
| 165 | } |
| 166 | return Ty; |
| 167 | } |
| 168 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 169 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 170 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 173 | ASTContext &ABIInfo::getContext() const { |
| 174 | return CGT.getContext(); |
| 175 | } |
| 176 | |
| 177 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 178 | return CGT.getLLVMContext(); |
| 179 | } |
| 180 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 181 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 182 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 183 | } |
| 184 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 185 | const TargetInfo &ABIInfo::getTarget() const { |
| 186 | return CGT.getTarget(); |
| 187 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 188 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 189 | const CodeGenOptions &ABIInfo::getCodeGenOpts() const { |
| 190 | return CGT.getCodeGenOpts(); |
| 191 | } |
| 192 | |
| 193 | bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); } |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 194 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 195 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 200 | uint64_t Members) const { |
| 201 | return false; |
| 202 | } |
| 203 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 204 | bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 205 | return false; |
| 206 | } |
| 207 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 208 | LLVM_DUMP_METHOD void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 209 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 210 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 211 | switch (TheKind) { |
| 212 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 213 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 214 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 215 | Ty->print(OS); |
| 216 | else |
| 217 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 218 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 219 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 220 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 221 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 222 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 223 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 224 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 225 | case InAlloca: |
| 226 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 227 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 228 | case Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 229 | OS << "Indirect Align=" << getIndirectAlign().getQuantity() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 230 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 231 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 232 | break; |
| 233 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 234 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 235 | break; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 236 | case CoerceAndExpand: |
| 237 | OS << "CoerceAndExpand Type="; |
| 238 | getCoerceAndExpandType()->print(OS); |
| 239 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 240 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 241 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 244 | // Dynamically round a pointer up to a multiple of the given alignment. |
| 245 | static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, |
| 246 | llvm::Value *Ptr, |
| 247 | CharUnits Align) { |
| 248 | llvm::Value *PtrAsInt = Ptr; |
| 249 | // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; |
| 250 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 251 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 252 | llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); |
| 253 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 254 | llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); |
| 255 | PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, |
| 256 | Ptr->getType(), |
| 257 | Ptr->getName() + ".aligned"); |
| 258 | return PtrAsInt; |
| 259 | } |
| 260 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 261 | /// Emit va_arg for a platform using the common void* representation, |
| 262 | /// where arguments are simply emitted in an array of slots on the stack. |
| 263 | /// |
| 264 | /// This version implements the core direct-value passing rules. |
| 265 | /// |
| 266 | /// \param SlotSize - The size and alignment of a stack slot. |
| 267 | /// Each argument will be allocated to a multiple of this number of |
| 268 | /// slots, and all the slots will be aligned to this value. |
| 269 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 270 | /// an argument type with an alignment greater than the slot size |
| 271 | /// will be emitted on a higher-alignment address, potentially |
| 272 | /// leaving one or more empty slots behind as padding. If this |
| 273 | /// is false, the returned address might be less-aligned than |
| 274 | /// DirectAlign. |
| 275 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 276 | Address VAListAddr, |
| 277 | llvm::Type *DirectTy, |
| 278 | CharUnits DirectSize, |
| 279 | CharUnits DirectAlign, |
| 280 | CharUnits SlotSize, |
| 281 | bool AllowHigherAlign) { |
| 282 | // Cast the element type to i8* if necessary. Some platforms define |
| 283 | // va_list as a struct containing an i8* instead of just an i8*. |
| 284 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 285 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 286 | |
| 287 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 288 | |
| 289 | // If the CC aligns values higher than the slot size, do so if needed. |
| 290 | Address Addr = Address::invalid(); |
| 291 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 292 | Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), |
| 293 | DirectAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 294 | } else { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 295 | Addr = Address(Ptr, SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // Advance the pointer past the argument, then store that back. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 299 | CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 300 | llvm::Value *NextPtr = |
| 301 | CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, |
| 302 | "argp.next"); |
| 303 | CGF.Builder.CreateStore(NextPtr, VAListAddr); |
| 304 | |
| 305 | // If the argument is smaller than a slot, and this is a big-endian |
| 306 | // target, the argument will be right-adjusted in its slot. |
Strahinja Petrovic | 515a1eb | 2016-06-24 12:12:41 +0000 | [diff] [blame] | 307 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() && |
| 308 | !DirectTy->isStructTy()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 309 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 310 | } |
| 311 | |
| 312 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 313 | return Addr; |
| 314 | } |
| 315 | |
| 316 | /// Emit va_arg for a platform using the common void* representation, |
| 317 | /// where arguments are simply emitted in an array of slots on the stack. |
| 318 | /// |
| 319 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 320 | /// \param ValueInfo - The size and alignment of this type, generally |
| 321 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 322 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 323 | /// Each argument will be allocated to a multiple of this number of |
| 324 | /// slots, and all the slots will be aligned to this value. |
| 325 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 326 | /// an argument type with an alignment greater than the slot size |
| 327 | /// will be emitted on a higher-alignment address, potentially |
| 328 | /// leaving one or more empty slots behind as padding. |
| 329 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 330 | QualType ValueTy, bool IsIndirect, |
| 331 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 332 | CharUnits SlotSizeAndAlign, |
| 333 | bool AllowHigherAlign) { |
| 334 | // The size and alignment of the value that was passed directly. |
| 335 | CharUnits DirectSize, DirectAlign; |
| 336 | if (IsIndirect) { |
| 337 | DirectSize = CGF.getPointerSize(); |
| 338 | DirectAlign = CGF.getPointerAlign(); |
| 339 | } else { |
| 340 | DirectSize = ValueInfo.first; |
| 341 | DirectAlign = ValueInfo.second; |
| 342 | } |
| 343 | |
| 344 | // Cast the address we've calculated to the right type. |
| 345 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 346 | if (IsIndirect) |
| 347 | DirectTy = DirectTy->getPointerTo(0); |
| 348 | |
| 349 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 350 | DirectSize, DirectAlign, |
| 351 | SlotSizeAndAlign, |
| 352 | AllowHigherAlign); |
| 353 | |
| 354 | if (IsIndirect) { |
| 355 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 356 | } |
| 357 | |
| 358 | return Addr; |
| 359 | |
| 360 | } |
| 361 | |
| 362 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 363 | Address Addr1, llvm::BasicBlock *Block1, |
| 364 | Address Addr2, llvm::BasicBlock *Block2, |
| 365 | const llvm::Twine &Name = "") { |
| 366 | assert(Addr1.getType() == Addr2.getType()); |
| 367 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 368 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 369 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 370 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 371 | return Address(PHI, Align); |
| 372 | } |
| 373 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 374 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 375 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 376 | // If someone can figure out a general rule for this, that would be great. |
| 377 | // It's probably just doomed to be platform-dependent, though. |
| 378 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 379 | // Verified for: |
| 380 | // x86-64 FreeBSD, Linux, Darwin |
| 381 | // x86-32 FreeBSD, Linux, Darwin |
| 382 | // PowerPC Linux, Darwin |
| 383 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 384 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 385 | return 32; |
| 386 | } |
| 387 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 388 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 389 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 390 | // The following conventions are known to require this to be false: |
| 391 | // x86_stdcall |
| 392 | // MIPS |
| 393 | // For everything else, we just prefer false unless we opt out. |
| 394 | return false; |
| 395 | } |
| 396 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 397 | void |
| 398 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 399 | llvm::SmallString<24> &Opt) const { |
| 400 | // This assumes the user is passing a library name like "rt" instead of a |
| 401 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 402 | // dynamic. |
| 403 | Opt = "-l"; |
| 404 | Opt += Lib; |
| 405 | } |
| 406 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 407 | unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 408 | // OpenCL kernels are called via an explicit runtime API with arguments |
| 409 | // set with clSetKernelArg(), not as normal sub-functions. |
| 410 | // Return SPIR_KERNEL by default as the kernel calling convention to |
| 411 | // ensure the fingerprint is fixed such way that each OpenCL argument |
| 412 | // gets one matching argument in the produced kernel function argument |
| 413 | // list to enable feasible implementation of clSetKernelArg() with |
| 414 | // aggregates etc. In case we would use the default C calling conv here, |
| 415 | // clSetKernelArg() might break depending on the target-specific |
| 416 | // conventions; different targets might split structs passed as values |
| 417 | // to multiple function arguments etc. |
| 418 | return llvm::CallingConv::SPIR_KERNEL; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 419 | } |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 420 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 421 | llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 422 | llvm::PointerType *T, QualType QT) const { |
| 423 | return llvm::ConstantPointerNull::get(T); |
| 424 | } |
| 425 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 426 | unsigned TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 427 | const VarDecl *D) const { |
| 428 | assert(!CGM.getLangOpts().OpenCL && |
| 429 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 430 | "Address space agnostic languages only"); |
Yaxun Liu | 7bce642 | 2017-07-08 19:13:41 +0000 | [diff] [blame] | 431 | return D ? D->getType().getAddressSpace() |
| 432 | : static_cast<unsigned>(LangAS::Default); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 435 | llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 436 | CodeGen::CodeGenFunction &CGF, llvm::Value *Src, unsigned SrcAddr, |
| 437 | unsigned DestAddr, llvm::Type *DestTy, bool isNonNull) const { |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 438 | // Since target may map different address spaces in AST to the same address |
| 439 | // space, an address space conversion may end up as a bitcast. |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 440 | if (auto *C = dyn_cast<llvm::Constant>(Src)) |
| 441 | return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 442 | return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DestTy); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 445 | llvm::Constant * |
| 446 | TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src, |
| 447 | unsigned SrcAddr, unsigned DestAddr, |
| 448 | llvm::Type *DestTy) const { |
| 449 | // Since target may map different address spaces in AST to the same address |
| 450 | // space, an address space conversion may end up as a bitcast. |
| 451 | return llvm::ConstantExpr::getPointerCast(Src, DestTy); |
| 452 | } |
| 453 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 454 | llvm::SyncScope::ID |
| 455 | TargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, llvm::LLVMContext &C) const { |
| 456 | return C.getOrInsertSyncScopeID(""); /* default sync scope */ |
| 457 | } |
| 458 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 459 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 460 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 461 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 462 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 463 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 464 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 465 | if (FD->isUnnamedBitfield()) |
| 466 | return true; |
| 467 | |
| 468 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 469 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 470 | // Constant arrays of empty records count as empty, strip them off. |
| 471 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 472 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 473 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 474 | if (AT->getSize() == 0) |
| 475 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 476 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 477 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 478 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 479 | const RecordType *RT = FT->getAs<RecordType>(); |
| 480 | if (!RT) |
| 481 | return false; |
| 482 | |
| 483 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 484 | // |
| 485 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 486 | // current ABI. |
| 487 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 488 | return false; |
| 489 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 490 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 493 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 494 | /// fields. Note that a structure with a flexible array member is not |
| 495 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 496 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 497 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 498 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 499 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 500 | const RecordDecl *RD = RT->getDecl(); |
| 501 | if (RD->hasFlexibleArrayMember()) |
| 502 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 503 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 504 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 505 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 506 | for (const auto &I : CXXRD->bases()) |
| 507 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 508 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 509 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 510 | for (const auto *I : RD->fields()) |
| 511 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 512 | return false; |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | /// isSingleElementStruct - Determine if a structure is a "single |
| 517 | /// element struct", i.e. it has exactly one non-empty field or |
| 518 | /// exactly one field which is itself a single element |
| 519 | /// struct. Structures with flexible array members are never |
| 520 | /// considered single element structs. |
| 521 | /// |
| 522 | /// \return The field declaration for the single non-empty field, if |
| 523 | /// it exists. |
| 524 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 525 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 526 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 527 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 528 | |
| 529 | const RecordDecl *RD = RT->getDecl(); |
| 530 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 531 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 532 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 533 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 534 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 535 | // If this is a C++ record, check the bases first. |
| 536 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 537 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 538 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 539 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 540 | continue; |
| 541 | |
| 542 | // If we already found an element then this isn't a single-element struct. |
| 543 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 544 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 545 | |
| 546 | // If this is non-empty and not a single element struct, the composite |
| 547 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 548 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 549 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 550 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
| 554 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 555 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 556 | QualType FT = FD->getType(); |
| 557 | |
| 558 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 559 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 560 | continue; |
| 561 | |
| 562 | // If we already found an element then this isn't a single-element |
| 563 | // struct. |
| 564 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 565 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 566 | |
| 567 | // Treat single element arrays as the element. |
| 568 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 569 | if (AT->getSize().getZExtValue() != 1) |
| 570 | break; |
| 571 | FT = AT->getElementType(); |
| 572 | } |
| 573 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 574 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 575 | Found = FT.getTypePtr(); |
| 576 | } else { |
| 577 | Found = isSingleElementStruct(FT, Context); |
| 578 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 579 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 583 | // We don't consider a struct a single-element struct if it has |
| 584 | // padding beyond the element type. |
| 585 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 586 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 587 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 588 | return Found; |
| 589 | } |
| 590 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 591 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 592 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 593 | const ABIArgInfo &AI) { |
| 594 | // This default implementation defers to the llvm backend's va_arg |
| 595 | // instruction. It can handle only passing arguments directly |
| 596 | // (typically only handled in the backend for primitive types), or |
| 597 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 598 | // flag has ABI impact in the callee, this implementation cannot |
| 599 | // work.) |
| 600 | |
| 601 | // Only a few cases are covered here at the moment -- those needed |
| 602 | // by the default abi. |
| 603 | llvm::Value *Val; |
| 604 | |
| 605 | if (AI.isIndirect()) { |
| 606 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 607 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 608 | assert( |
| 609 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 610 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 611 | |
| 612 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 613 | CharUnits TyAlignForABI = TyInfo.second; |
| 614 | |
| 615 | llvm::Type *BaseTy = |
| 616 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 617 | llvm::Value *Addr = |
| 618 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 619 | return Address(Addr, TyAlignForABI); |
| 620 | } else { |
| 621 | assert((AI.isDirect() || AI.isExtend()) && |
| 622 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 623 | |
| 624 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 625 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 626 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 627 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 628 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 629 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 630 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 631 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 632 | |
| 633 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 634 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 635 | CGF.Builder.CreateStore(Val, Temp); |
| 636 | return Temp; |
| 637 | } |
| 638 | } |
| 639 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 640 | /// DefaultABIInfo - The default implementation for ABI specific |
| 641 | /// details. This implementation provides information which results in |
| 642 | /// self-consistent and sensible LLVM IR generation, but does not |
| 643 | /// conform to any particular ABI. |
| 644 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 645 | public: |
| 646 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 648 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 649 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 650 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 651 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 652 | if (!getCXXABI().classifyReturnType(FI)) |
| 653 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 654 | for (auto &I : FI.arguments()) |
| 655 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 656 | } |
| 657 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 658 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 659 | QualType Ty) const override { |
| 660 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 661 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 662 | }; |
| 663 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 664 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 665 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 666 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 667 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 668 | }; |
| 669 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 670 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 671 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 672 | |
| 673 | if (isAggregateTypeForABI(Ty)) { |
| 674 | // Records with non-trivial destructors/copy-constructors should not be |
| 675 | // passed by value. |
| 676 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 677 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 678 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 679 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 680 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 682 | // Treat an enum type as its underlying type. |
| 683 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 684 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 686 | return (Ty->isPromotableIntegerType() ? |
| 687 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 690 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 691 | if (RetTy->isVoidType()) |
| 692 | return ABIArgInfo::getIgnore(); |
| 693 | |
| 694 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 695 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 696 | |
| 697 | // Treat an enum type as its underlying type. |
| 698 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 699 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 700 | |
| 701 | return (RetTy->isPromotableIntegerType() ? |
| 702 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 703 | } |
| 704 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 705 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 706 | // WebAssembly ABI Implementation |
| 707 | // |
| 708 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 709 | //===----------------------------------------------------------------------===// |
| 710 | |
| 711 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 712 | public: |
| 713 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 714 | : DefaultABIInfo(CGT) {} |
| 715 | |
| 716 | private: |
| 717 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 718 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 719 | |
| 720 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 721 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 722 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 723 | void computeInfo(CGFunctionInfo &FI) const override { |
| 724 | if (!getCXXABI().classifyReturnType(FI)) |
| 725 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 726 | for (auto &Arg : FI.arguments()) |
| 727 | Arg.info = classifyArgumentType(Arg.type); |
| 728 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 729 | |
| 730 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 731 | QualType Ty) const override; |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 732 | }; |
| 733 | |
| 734 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 735 | public: |
| 736 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 737 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 738 | }; |
| 739 | |
| 740 | /// \brief Classify argument of given type \p Ty. |
| 741 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 742 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 743 | |
| 744 | if (isAggregateTypeForABI(Ty)) { |
| 745 | // Records with non-trivial destructors/copy-constructors should not be |
| 746 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 747 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 748 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 749 | // Ignore empty structs/unions. |
| 750 | if (isEmptyRecord(getContext(), Ty, true)) |
| 751 | return ABIArgInfo::getIgnore(); |
| 752 | // Lower single-element structs to just pass a regular value. TODO: We |
| 753 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 754 | // though watch out for things like bitfields. |
| 755 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 756 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | // Otherwise just do the default thing. |
| 760 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 761 | } |
| 762 | |
| 763 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 764 | if (isAggregateTypeForABI(RetTy)) { |
| 765 | // Records with non-trivial destructors/copy-constructors should not be |
| 766 | // returned by value. |
| 767 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 768 | // Ignore empty structs/unions. |
| 769 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 770 | return ABIArgInfo::getIgnore(); |
| 771 | // Lower single-element structs to just return a regular value. TODO: We |
| 772 | // could do reasonable-size multiple-element structs too, using |
| 773 | // ABIArgInfo::getDirect(). |
| 774 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 775 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | // Otherwise just do the default thing. |
| 780 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 781 | } |
| 782 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 783 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 784 | QualType Ty) const { |
| 785 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 786 | getContext().getTypeInfoInChars(Ty), |
| 787 | CharUnits::fromQuantity(4), |
| 788 | /*AllowHigherAlign=*/ true); |
| 789 | } |
| 790 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 791 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 792 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 793 | // |
| 794 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 795 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 796 | //===----------------------------------------------------------------------===// |
| 797 | |
| 798 | class PNaClABIInfo : public ABIInfo { |
| 799 | public: |
| 800 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 801 | |
| 802 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 803 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 804 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 805 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 806 | Address EmitVAArg(CodeGenFunction &CGF, |
| 807 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 808 | }; |
| 809 | |
| 810 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 811 | public: |
| 812 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 813 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 814 | }; |
| 815 | |
| 816 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 817 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 818 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 819 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 820 | for (auto &I : FI.arguments()) |
| 821 | I.info = classifyArgumentType(I.type); |
| 822 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 823 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 824 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 825 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 826 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 827 | // function classification. Structs get passed directly for varargs |
| 828 | // functions, through a rewriting transform in |
| 829 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 830 | // this target to actually support a va_arg instructions with an |
| 831 | // aggregate type, unlike other targets. |
| 832 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 835 | /// \brief Classify argument of given type \p Ty. |
| 836 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 837 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 838 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 839 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 840 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 841 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 842 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 843 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 844 | } else if (Ty->isFloatingType()) { |
| 845 | // Floating-point types don't go inreg. |
| 846 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 847 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 848 | |
| 849 | return (Ty->isPromotableIntegerType() ? |
| 850 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 854 | if (RetTy->isVoidType()) |
| 855 | return ABIArgInfo::getIgnore(); |
| 856 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 857 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 858 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 859 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 860 | |
| 861 | // Treat an enum type as its underlying type. |
| 862 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 863 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 864 | |
| 865 | return (RetTy->isPromotableIntegerType() ? |
| 866 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 867 | } |
| 868 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 869 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 870 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 871 | // 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] | 872 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 873 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 874 | IRType->getScalarSizeInBits() != 64; |
| 875 | } |
| 876 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 877 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 878 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 879 | llvm::Type* Ty) { |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 880 | bool IsMMXCons = llvm::StringSwitch<bool>(Constraint) |
| 881 | .Cases("y", "&y", "^Ym", true) |
| 882 | .Default(false); |
| 883 | if (IsMMXCons && Ty->isVectorTy()) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 884 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 885 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 886 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 889 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 893 | return Ty; |
| 894 | } |
| 895 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 896 | /// Returns true if this type can be passed in SSE registers with the |
| 897 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 898 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 899 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 900 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { |
| 901 | if (BT->getKind() == BuiltinType::LongDouble) { |
| 902 | if (&Context.getTargetInfo().getLongDoubleFormat() == |
| 903 | &llvm::APFloat::x87DoubleExtended()) |
| 904 | return false; |
| 905 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 906 | return true; |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 907 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 908 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 909 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 910 | // registers specially. |
| 911 | unsigned VecSize = Context.getTypeSize(VT); |
| 912 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 913 | return true; |
| 914 | } |
| 915 | return false; |
| 916 | } |
| 917 | |
| 918 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 919 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 920 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 921 | return NumMembers <= 4; |
| 922 | } |
| 923 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 924 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 925 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 926 | auto AI = ABIArgInfo::getDirect(T); |
| 927 | AI.setInReg(true); |
| 928 | AI.setCanBeFlattened(false); |
| 929 | return AI; |
| 930 | } |
| 931 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 932 | //===----------------------------------------------------------------------===// |
| 933 | // X86-32 ABI Implementation |
| 934 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 935 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 936 | /// \brief Similar to llvm::CCState, but for Clang. |
| 937 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 938 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 939 | |
| 940 | unsigned CC; |
| 941 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 942 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 943 | }; |
| 944 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 945 | enum { |
| 946 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 947 | VectorcallMaxParamNumAsReg = 6 |
| 948 | }; |
| 949 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 950 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 951 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 952 | enum Class { |
| 953 | Integer, |
| 954 | Float |
| 955 | }; |
| 956 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 957 | static const unsigned MinABIStackAlignInBytes = 4; |
| 958 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 959 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 960 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 961 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 962 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 963 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 964 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 965 | |
| 966 | static bool isRegisterSize(unsigned Size) { |
| 967 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 968 | } |
| 969 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 970 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 971 | // FIXME: Assumes vectorcall is in use. |
| 972 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 973 | } |
| 974 | |
| 975 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 976 | uint64_t NumMembers) const override { |
| 977 | // FIXME: Assumes vectorcall is in use. |
| 978 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 979 | } |
| 980 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 981 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 982 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 983 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 984 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 985 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 986 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 987 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 988 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 989 | /// \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] | 990 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 991 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 992 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 993 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 994 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 995 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 996 | /// \brief Updates the number of available free registers, returns |
| 997 | /// true if any registers were allocated. |
| 998 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 999 | |
| 1000 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 1001 | bool &NeedsPadding) const; |
| 1002 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1003 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1004 | bool canExpandIndirectArgument(QualType Ty) const; |
| 1005 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1006 | /// \brief Rewrite the function info so that all memory arguments use |
| 1007 | /// inalloca. |
| 1008 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 1009 | |
| 1010 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1011 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1012 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1013 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1014 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1015 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 1016 | public: |
| 1017 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1018 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1019 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1020 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1021 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1022 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1023 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1024 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1025 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1026 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 1027 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1028 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1029 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1030 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1031 | |
| 1032 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 1033 | ArrayRef<llvm::Type*> scalars, |
| 1034 | bool asReturnValue) const override { |
| 1035 | // LLVM's x86-32 lowering currently only assigns up to three |
| 1036 | // integer registers and three fp registers. Oddly, it'll use up to |
| 1037 | // four vector registers for vectors, but those can overlap with the |
| 1038 | // scalar registers. |
| 1039 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
| 1040 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 1041 | |
| 1042 | bool isSwiftErrorInRegister() const override { |
| 1043 | // x86-32 lowering does not support passing swifterror in a register. |
| 1044 | return false; |
| 1045 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1046 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1047 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1048 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1049 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1050 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1051 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1052 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 1053 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1054 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 1055 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1056 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1057 | static bool isStructReturnInRegABI( |
| 1058 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1059 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1060 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1061 | CodeGen::CodeGenModule &CGM, |
| 1062 | ForDefinition_t IsForDefinition) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1063 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1064 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1065 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1066 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1067 | return 4; |
| 1068 | } |
| 1069 | |
| 1070 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1071 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1072 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1073 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1074 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1075 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1076 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1077 | } |
| 1078 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1079 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1080 | std::string &Constraints, |
| 1081 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1082 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1083 | std::vector<LValue> &ResultRegDests, |
| 1084 | std::string &AsmString, |
| 1085 | unsigned NumOutputs) const override; |
| 1086 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1087 | llvm::Constant * |
| 1088 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1089 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1090 | (0x06 << 8) | // .+0x08 |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 1091 | ('v' << 16) | |
| 1092 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1093 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1094 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1095 | |
| 1096 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1097 | return "movl\t%ebp, %ebp" |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 1098 | "\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1099 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1100 | }; |
| 1101 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1102 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1103 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1104 | /// Rewrite input constraint references after adding some output constraints. |
| 1105 | /// In the case where there is one output and one input and we add one output, |
| 1106 | /// we need to replace all operand references greater than or equal to 1: |
| 1107 | /// mov $0, $1 |
| 1108 | /// mov eax, $1 |
| 1109 | /// The result will be: |
| 1110 | /// mov $0, $2 |
| 1111 | /// mov eax, $2 |
| 1112 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1113 | unsigned NumNewOuts, |
| 1114 | std::string &AsmString) { |
| 1115 | std::string Buf; |
| 1116 | llvm::raw_string_ostream OS(Buf); |
| 1117 | size_t Pos = 0; |
| 1118 | while (Pos < AsmString.size()) { |
| 1119 | size_t DollarStart = AsmString.find('$', Pos); |
| 1120 | if (DollarStart == std::string::npos) |
| 1121 | DollarStart = AsmString.size(); |
| 1122 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1123 | if (DollarEnd == std::string::npos) |
| 1124 | DollarEnd = AsmString.size(); |
| 1125 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1126 | Pos = DollarEnd; |
| 1127 | size_t NumDollars = DollarEnd - DollarStart; |
| 1128 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1129 | // We have an operand reference. |
| 1130 | size_t DigitStart = Pos; |
| 1131 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1132 | if (DigitEnd == std::string::npos) |
| 1133 | DigitEnd = AsmString.size(); |
| 1134 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1135 | unsigned OperandIndex; |
| 1136 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1137 | if (OperandIndex >= FirstIn) |
| 1138 | OperandIndex += NumNewOuts; |
| 1139 | OS << OperandIndex; |
| 1140 | } else { |
| 1141 | OS << OperandStr; |
| 1142 | } |
| 1143 | Pos = DigitEnd; |
| 1144 | } |
| 1145 | } |
| 1146 | AsmString = std::move(OS.str()); |
| 1147 | } |
| 1148 | |
| 1149 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1150 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1151 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1152 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1153 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1154 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1155 | unsigned NumOutputs) const { |
| 1156 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1157 | |
| 1158 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1159 | // larger. |
| 1160 | if (!Constraints.empty()) |
| 1161 | Constraints += ','; |
| 1162 | if (RetWidth <= 32) { |
| 1163 | Constraints += "={eax}"; |
| 1164 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1165 | } else { |
| 1166 | // Use the 'A' constraint for EAX:EDX. |
| 1167 | Constraints += "=A"; |
| 1168 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1169 | } |
| 1170 | |
| 1171 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1172 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1173 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1174 | |
| 1175 | // Coerce the integer by bitcasting the return slot pointer. |
| 1176 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1177 | CoerceTy->getPointerTo())); |
| 1178 | ResultRegDests.push_back(ReturnSlot); |
| 1179 | |
| 1180 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1181 | } |
| 1182 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1183 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1184 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1185 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1186 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1187 | uint64_t Size = Context.getTypeSize(Ty); |
| 1188 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1189 | // For i386, type must be register sized. |
| 1190 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1191 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1192 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1193 | |
| 1194 | if (Ty->isVectorType()) { |
| 1195 | // 64- and 128- bit vectors inside structures are not returned in |
| 1196 | // registers. |
| 1197 | if (Size == 64 || Size == 128) |
| 1198 | return false; |
| 1199 | |
| 1200 | return true; |
| 1201 | } |
| 1202 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1203 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1204 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1205 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1206 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1207 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1208 | return true; |
| 1209 | |
| 1210 | // Arrays are treated like records. |
| 1211 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1212 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1213 | |
| 1214 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1215 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1216 | if (!RT) return false; |
| 1217 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1218 | // FIXME: Traverse bases here too. |
| 1219 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1220 | // Structure types are passed in register if all fields would be |
| 1221 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1222 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1223 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1224 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1225 | continue; |
| 1226 | |
| 1227 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1228 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1229 | return false; |
| 1230 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1231 | return true; |
| 1232 | } |
| 1233 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1234 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1235 | // Treat complex types as the element type. |
| 1236 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1237 | Ty = CTy->getElementType(); |
| 1238 | |
| 1239 | // Check for a type which we know has a simple scalar argument-passing |
| 1240 | // convention without any padding. (We're specifically looking for 32 |
| 1241 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1242 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1243 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1244 | return false; |
| 1245 | |
| 1246 | uint64_t Size = Context.getTypeSize(Ty); |
| 1247 | return Size == 32 || Size == 64; |
| 1248 | } |
| 1249 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1250 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1251 | uint64_t &Size) { |
| 1252 | for (const auto *FD : RD->fields()) { |
| 1253 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1254 | // argument is smaller than 32-bits, expanding the struct will create |
| 1255 | // alignment padding. |
| 1256 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1257 | return false; |
| 1258 | |
| 1259 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1260 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1261 | // counts as "basic" is more complicated than what we were doing previously. |
| 1262 | if (FD->isBitField()) |
| 1263 | return false; |
| 1264 | |
| 1265 | Size += Context.getTypeSize(FD->getType()); |
| 1266 | } |
| 1267 | return true; |
| 1268 | } |
| 1269 | |
| 1270 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1271 | uint64_t &Size) { |
| 1272 | // Don't do this if there are any non-empty bases. |
| 1273 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1274 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1275 | Size)) |
| 1276 | return false; |
| 1277 | } |
| 1278 | if (!addFieldSizes(Context, RD, Size)) |
| 1279 | return false; |
| 1280 | return true; |
| 1281 | } |
| 1282 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1283 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1284 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1285 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1286 | /// optimizations. |
| 1287 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1288 | // We can only expand structure types. |
| 1289 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1290 | if (!RT) |
| 1291 | return false; |
| 1292 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1293 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1294 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1295 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1296 | // On non-Windows, we have to conservatively match our old bitcode |
| 1297 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1298 | if (!CXXRD->isCLike()) |
| 1299 | return false; |
| 1300 | } else { |
| 1301 | // Don't do this for dynamic classes. |
| 1302 | if (CXXRD->isDynamicClass()) |
| 1303 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1304 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1305 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1306 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1307 | } else { |
| 1308 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1309 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | // We can do this if there was no alignment padding. |
| 1313 | return Size == getContext().getTypeSize(Ty); |
| 1314 | } |
| 1315 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1316 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1317 | // If the return value is indirect, then the hidden argument is consuming one |
| 1318 | // integer register. |
| 1319 | if (State.FreeRegs) { |
| 1320 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1321 | if (!IsMCUABI) |
| 1322 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1323 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1324 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1327 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1328 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1329 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1330 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1331 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1332 | const Type *Base = nullptr; |
| 1333 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1334 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1335 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1336 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1337 | // The LLVM struct type for such an aggregate should lower properly. |
| 1338 | return ABIArgInfo::getDirect(); |
| 1339 | } |
| 1340 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1341 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1342 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1343 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1344 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1345 | |
| 1346 | // 128-bit vectors are a special case; they are returned in |
| 1347 | // registers and we need to make sure to pick a type the LLVM |
| 1348 | // backend will like. |
| 1349 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1350 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1351 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1352 | |
| 1353 | // Always return in register if it fits in a general purpose |
| 1354 | // register, or if it is 64 bits and has a single element. |
| 1355 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1356 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1357 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1358 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1359 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1360 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1364 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1365 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1366 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1367 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1368 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1369 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1370 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1371 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1372 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1373 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1374 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1375 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1376 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1377 | // Ignore empty structs/unions. |
| 1378 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1379 | return ABIArgInfo::getIgnore(); |
| 1380 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1381 | // Small structures which are register sized are generally returned |
| 1382 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1383 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1384 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1385 | |
| 1386 | // As a special-case, if the struct is a "single-element" struct, and |
| 1387 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1388 | // floating-point register. (MSVC does not apply this special case.) |
| 1389 | // We apply a similar transformation for pointer types to improve the |
| 1390 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1391 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1392 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1393 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1394 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1395 | |
| 1396 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1397 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1398 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1401 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1402 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1403 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1404 | // Treat an enum type as its underlying type. |
| 1405 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1406 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1407 | |
| 1408 | return (RetTy->isPromotableIntegerType() ? |
| 1409 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1412 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1413 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1414 | } |
| 1415 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1416 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1417 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1418 | if (!RT) |
| 1419 | return 0; |
| 1420 | const RecordDecl *RD = RT->getDecl(); |
| 1421 | |
| 1422 | // If this is a C++ record, check the bases first. |
| 1423 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1424 | for (const auto &I : CXXRD->bases()) |
| 1425 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1426 | return false; |
| 1427 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1428 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1429 | QualType FT = i->getType(); |
| 1430 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1431 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1432 | return true; |
| 1433 | |
| 1434 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1435 | return true; |
| 1436 | } |
| 1437 | |
| 1438 | return false; |
| 1439 | } |
| 1440 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1441 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1442 | unsigned Align) const { |
| 1443 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1444 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1445 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1446 | return 0; // Use default alignment. |
| 1447 | |
| 1448 | // On non-Darwin, the stack type alignment is always 4. |
| 1449 | if (!IsDarwinVectorABI) { |
| 1450 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1451 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1452 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1453 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1454 | // 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] | 1455 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1456 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1457 | return 16; |
| 1458 | |
| 1459 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1462 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1463 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1464 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1465 | if (State.FreeRegs) { |
| 1466 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1467 | if (!IsMCUABI) |
| 1468 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1469 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1470 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1471 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1472 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1473 | // Compute the byval alignment. |
| 1474 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1475 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1476 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1477 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1478 | |
| 1479 | // If the stack alignment is less than the type alignment, realign the |
| 1480 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1481 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1482 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1483 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1486 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1487 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1488 | if (!T) |
| 1489 | T = Ty.getTypePtr(); |
| 1490 | |
| 1491 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1492 | BuiltinType::Kind K = BT->getKind(); |
| 1493 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1494 | return Float; |
| 1495 | } |
| 1496 | return Integer; |
| 1497 | } |
| 1498 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1499 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1500 | if (!IsSoftFloatABI) { |
| 1501 | Class C = classify(Ty); |
| 1502 | if (C == Float) |
| 1503 | return false; |
| 1504 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1505 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1506 | unsigned Size = getContext().getTypeSize(Ty); |
| 1507 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1508 | |
| 1509 | if (SizeInRegs == 0) |
| 1510 | return false; |
| 1511 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1512 | if (!IsMCUABI) { |
| 1513 | if (SizeInRegs > State.FreeRegs) { |
| 1514 | State.FreeRegs = 0; |
| 1515 | return false; |
| 1516 | } |
| 1517 | } else { |
| 1518 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1519 | // earlier parameters that are passed on the stack. Also, |
| 1520 | // it does not allow passing >8-byte structs in-register, |
| 1521 | // even if there are 3 free registers available. |
| 1522 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1523 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1524 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1525 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1526 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1527 | return true; |
| 1528 | } |
| 1529 | |
| 1530 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
| 1531 | bool &InReg, |
| 1532 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1533 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1534 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1535 | // (HFAs) have already been dealt with at this point. |
| 1536 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1537 | return false; |
| 1538 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1539 | NeedsPadding = false; |
| 1540 | InReg = !IsMCUABI; |
| 1541 | |
| 1542 | if (!updateFreeRegs(Ty, State)) |
| 1543 | return false; |
| 1544 | |
| 1545 | if (IsMCUABI) |
| 1546 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1547 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1548 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1549 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1550 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1551 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1552 | NeedsPadding = true; |
| 1553 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1554 | return false; |
| 1555 | } |
| 1556 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1557 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1560 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1561 | if (!updateFreeRegs(Ty, State)) |
| 1562 | return false; |
| 1563 | |
| 1564 | if (IsMCUABI) |
| 1565 | return false; |
| 1566 | |
| 1567 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1568 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1569 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1570 | if (getContext().getTypeSize(Ty) > 32) |
| 1571 | return false; |
| 1572 | |
| 1573 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
| 1574 | Ty->isReferenceType()); |
| 1575 | } |
| 1576 | |
| 1577 | return true; |
| 1578 | } |
| 1579 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1580 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1581 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1582 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1583 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1584 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1585 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1586 | // Check with the C++ ABI first. |
| 1587 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1588 | if (RT) { |
| 1589 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1590 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1591 | return getIndirectResult(Ty, false, State); |
| 1592 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1593 | // The field index doesn't matter, we'll fix it up later. |
| 1594 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1595 | } |
| 1596 | } |
| 1597 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1598 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1599 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1600 | const Type *Base = nullptr; |
| 1601 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1602 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1603 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1604 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1605 | if (State.FreeSSERegs >= NumElts) { |
| 1606 | State.FreeSSERegs -= NumElts; |
| 1607 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1608 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1609 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1610 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1611 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1615 | // Structures with flexible arrays are always indirect. |
| 1616 | // FIXME: This should not be byval! |
| 1617 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1618 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1619 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1620 | // Ignore empty structs/unions on non-Windows. |
| 1621 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1622 | return ABIArgInfo::getIgnore(); |
| 1623 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1624 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1625 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1626 | bool NeedsPadding = false; |
| 1627 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1628 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1629 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1630 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1631 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1632 | if (InReg) |
| 1633 | return ABIArgInfo::getDirectInReg(Result); |
| 1634 | else |
| 1635 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1636 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1637 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1638 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1639 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1640 | // of those arguments will match the struct. This is important because the |
| 1641 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1642 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1643 | // Don't do this for the MCU if there are still free integer registers |
| 1644 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1645 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1646 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1647 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1648 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1649 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1650 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1651 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1652 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1653 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1656 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1657 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1658 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1659 | if (IsDarwinVectorABI) { |
| 1660 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1661 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1662 | (Size == 64 && VT->getNumElements() == 1)) |
| 1663 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1664 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1665 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1666 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1667 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1668 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1669 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1670 | return ABIArgInfo::getDirect(); |
| 1671 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1672 | |
| 1673 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1674 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1675 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1676 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1677 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1678 | |
| 1679 | if (Ty->isPromotableIntegerType()) { |
| 1680 | if (InReg) |
| 1681 | return ABIArgInfo::getExtendInReg(); |
| 1682 | return ABIArgInfo::getExtend(); |
| 1683 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1684 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1685 | if (InReg) |
| 1686 | return ABIArgInfo::getDirectInReg(); |
| 1687 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1690 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1691 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1692 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1693 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1694 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1695 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1696 | // registers. |
| 1697 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1698 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1699 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1700 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1701 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1702 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1703 | // First pass do all the vector types. |
| 1704 | const Type *Base = nullptr; |
| 1705 | uint64_t NumElts = 0; |
| 1706 | const QualType& Ty = I.type; |
| 1707 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1708 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1709 | if (State.FreeSSERegs >= NumElts) { |
| 1710 | State.FreeSSERegs -= NumElts; |
| 1711 | I.info = ABIArgInfo::getDirect(); |
| 1712 | } else { |
| 1713 | I.info = classifyArgumentType(Ty, State); |
| 1714 | } |
| 1715 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1716 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1717 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1718 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1719 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1720 | // Second pass, do the rest! |
| 1721 | const Type *Base = nullptr; |
| 1722 | uint64_t NumElts = 0; |
| 1723 | const QualType& Ty = I.type; |
| 1724 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1725 | |
| 1726 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1727 | // Assign true HVAs (non vector/native FP types). |
| 1728 | if (State.FreeSSERegs >= NumElts) { |
| 1729 | State.FreeSSERegs -= NumElts; |
| 1730 | I.info = getDirectX86Hva(); |
| 1731 | } else { |
| 1732 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1733 | } |
| 1734 | } else if (!IsHva) { |
| 1735 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1736 | I.info = classifyArgumentType(Ty, State); |
| 1737 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1738 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1739 | } |
| 1740 | } |
| 1741 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1742 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1743 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1744 | if (IsMCUABI) |
| 1745 | State.FreeRegs = 3; |
| 1746 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1747 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1748 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1749 | State.FreeRegs = 2; |
| 1750 | State.FreeSSERegs = 6; |
| 1751 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1752 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1753 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1754 | State.FreeRegs = 5; |
| 1755 | State.FreeSSERegs = 8; |
| 1756 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1757 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1758 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1759 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1760 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1761 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1762 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1763 | // return value was sret and put it in a register ourselves if appropriate. |
| 1764 | if (State.FreeRegs) { |
| 1765 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1766 | if (!IsMCUABI) |
| 1767 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1768 | } |
| 1769 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1770 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1771 | // The chain argument effectively gives us another free register. |
| 1772 | if (FI.isChainCall()) |
| 1773 | ++State.FreeRegs; |
| 1774 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1775 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1776 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1777 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1778 | } else { |
| 1779 | // If not vectorcall, revert to normal behavior. |
| 1780 | for (auto &I : FI.arguments()) { |
| 1781 | I.info = classifyArgumentType(I.type, State); |
| 1782 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1783 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1787 | // all the memory arguments to use inalloca. |
| 1788 | if (UsedInAlloca) |
| 1789 | rewriteWithInAlloca(FI); |
| 1790 | } |
| 1791 | |
| 1792 | void |
| 1793 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1794 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1795 | QualType Type) const { |
| 1796 | // Arguments are always 4-byte-aligned. |
| 1797 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1798 | |
| 1799 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1800 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1801 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1802 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1803 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1804 | // Insert padding bytes to respect alignment. |
| 1805 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1806 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1807 | if (StackOffset != FieldEnd) { |
| 1808 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1809 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1810 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1811 | FrameFields.push_back(Ty); |
| 1812 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1815 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1816 | // Leave ignored and inreg arguments alone. |
| 1817 | switch (Info.getKind()) { |
| 1818 | case ABIArgInfo::InAlloca: |
| 1819 | return true; |
| 1820 | case ABIArgInfo::Indirect: |
| 1821 | assert(Info.getIndirectByVal()); |
| 1822 | return true; |
| 1823 | case ABIArgInfo::Ignore: |
| 1824 | return false; |
| 1825 | case ABIArgInfo::Direct: |
| 1826 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1827 | if (Info.getInReg()) |
| 1828 | return false; |
| 1829 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1830 | case ABIArgInfo::Expand: |
| 1831 | case ABIArgInfo::CoerceAndExpand: |
| 1832 | // These are aggregate types which are never passed in registers when |
| 1833 | // inalloca is involved. |
| 1834 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1835 | } |
| 1836 | llvm_unreachable("invalid enum"); |
| 1837 | } |
| 1838 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1839 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1840 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1841 | |
| 1842 | // Build a packed struct type for all of the arguments in memory. |
| 1843 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1844 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1845 | // The stack alignment is always 4. |
| 1846 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1847 | |
| 1848 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1849 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1850 | |
| 1851 | // Put 'this' into the struct before 'sret', if necessary. |
| 1852 | bool IsThisCall = |
| 1853 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1854 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1855 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1856 | isArgInAlloca(I->info)) { |
| 1857 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1858 | ++I; |
| 1859 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1860 | |
| 1861 | // 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] | 1862 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1863 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1864 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1865 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1866 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1870 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1871 | ++I; |
| 1872 | |
| 1873 | // Put arguments passed in memory into the struct. |
| 1874 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1875 | if (isArgInAlloca(I->info)) |
| 1876 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1880 | /*isPacked=*/true), |
| 1881 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1884 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1885 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1886 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1887 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1888 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1889 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1890 | // |
| 1891 | // Just messing with TypeInfo like this works because we never pass |
| 1892 | // anything indirectly. |
| 1893 | TypeInfo.second = CharUnits::fromQuantity( |
| 1894 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1895 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1896 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1897 | TypeInfo, CharUnits::fromQuantity(4), |
| 1898 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1901 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1902 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1903 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1904 | |
| 1905 | switch (Opts.getStructReturnConvention()) { |
| 1906 | case CodeGenOptions::SRCK_Default: |
| 1907 | break; |
| 1908 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1909 | return false; |
| 1910 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1911 | return true; |
| 1912 | } |
| 1913 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1914 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1915 | return true; |
| 1916 | |
| 1917 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1918 | case llvm::Triple::DragonFly: |
| 1919 | case llvm::Triple::FreeBSD: |
| 1920 | case llvm::Triple::OpenBSD: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1921 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1922 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1923 | default: |
| 1924 | return false; |
| 1925 | } |
| 1926 | } |
| 1927 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1928 | void X86_32TargetCodeGenInfo::setTargetAttributes( |
| 1929 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 1930 | ForDefinition_t IsForDefinition) const { |
| 1931 | if (!IsForDefinition) |
| 1932 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1933 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1934 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1935 | // Get the LLVM function. |
| 1936 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1937 | |
| 1938 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1939 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1940 | B.addStackAlignmentAttr(16); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1941 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1942 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1943 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1944 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1945 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1946 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1950 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1951 | CodeGen::CodeGenFunction &CGF, |
| 1952 | llvm::Value *Address) const { |
| 1953 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1954 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1955 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1956 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1957 | // 0-7 are the eight integer registers; the order is different |
| 1958 | // on Darwin (for EH), but the range is the same. |
| 1959 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1960 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1961 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1962 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1963 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1964 | // These have size 16, which is sizeof(long double) on |
| 1965 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1966 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1967 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1968 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1969 | } else { |
| 1970 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1971 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1972 | Builder.CreateAlignedStore( |
| 1973 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1974 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1975 | |
| 1976 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1977 | // These have size 12, which is sizeof(long double) on |
| 1978 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1979 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1980 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1981 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1982 | |
| 1983 | return false; |
| 1984 | } |
| 1985 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1986 | //===----------------------------------------------------------------------===// |
| 1987 | // X86-64 ABI Implementation |
| 1988 | //===----------------------------------------------------------------------===// |
| 1989 | |
| 1990 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1991 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1992 | /// The AVX ABI level for X86 targets. |
| 1993 | enum class X86AVXABILevel { |
| 1994 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1995 | AVX, |
| 1996 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1997 | }; |
| 1998 | |
| 1999 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 2000 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 2001 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2002 | case X86AVXABILevel::AVX512: |
| 2003 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2004 | case X86AVXABILevel::AVX: |
| 2005 | return 256; |
| 2006 | case X86AVXABILevel::None: |
| 2007 | return 128; |
| 2008 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 2009 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2012 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2013 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2014 | enum Class { |
| 2015 | Integer = 0, |
| 2016 | SSE, |
| 2017 | SSEUp, |
| 2018 | X87, |
| 2019 | X87Up, |
| 2020 | ComplexX87, |
| 2021 | NoClass, |
| 2022 | Memory |
| 2023 | }; |
| 2024 | |
| 2025 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2026 | /// |
| 2027 | /// Merge an accumulating classification \arg Accum with a field |
| 2028 | /// classification \arg Field. |
| 2029 | /// |
| 2030 | /// \param Accum - The accumulating classification. This should |
| 2031 | /// always be either NoClass or the result of a previous merge |
| 2032 | /// call. In addition, this should never be Memory (the caller |
| 2033 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2034 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2035 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2036 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2037 | /// |
| 2038 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2039 | /// final MEMORY or SSE classes when necessary. |
| 2040 | /// |
| 2041 | /// \param AggregateSize - The size of the current aggregate in |
| 2042 | /// the classification process. |
| 2043 | /// |
| 2044 | /// \param Lo - The classification for the parts of the type |
| 2045 | /// residing in the low word of the containing object. |
| 2046 | /// |
| 2047 | /// \param Hi - The classification for the parts of the type |
| 2048 | /// residing in the higher words of the containing object. |
| 2049 | /// |
| 2050 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2051 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2052 | /// classify - Determine the x86_64 register classes in which the |
| 2053 | /// given type T should be passed. |
| 2054 | /// |
| 2055 | /// \param Lo - The classification for the parts of the type |
| 2056 | /// residing in the low word of the containing object. |
| 2057 | /// |
| 2058 | /// \param Hi - The classification for the parts of the type |
| 2059 | /// residing in the high word of the containing object. |
| 2060 | /// |
| 2061 | /// \param OffsetBase - The bit offset of this type in the |
| 2062 | /// containing object. Some parameters are classified different |
| 2063 | /// depending on whether they straddle an eightbyte boundary. |
| 2064 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2065 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2066 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2067 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2068 | /// If a word is unused its result will be NoClass; if a type should |
| 2069 | /// be passed in Memory then at least the classification of \arg Lo |
| 2070 | /// will be Memory. |
| 2071 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2072 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2073 | /// |
| 2074 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2075 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2076 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2077 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2078 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2079 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2080 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2081 | unsigned IROffset, QualType SourceTy, |
| 2082 | unsigned SourceOffset) const; |
| 2083 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2084 | unsigned IROffset, QualType SourceTy, |
| 2085 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2086 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2087 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2088 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2089 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2090 | |
| 2091 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2092 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2093 | /// |
| 2094 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2095 | /// available. |
| 2096 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2097 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2098 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2099 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2100 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2101 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2102 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2103 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2104 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2105 | unsigned &NeededSSE) const; |
| 2106 | |
| 2107 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2108 | unsigned &NeededSSE) const; |
| 2109 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2110 | bool IsIllegalVectorType(QualType Ty) const; |
| 2111 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2112 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2113 | /// unfortunately in ways that were not always consistent with |
| 2114 | /// certain previous compilers. In particular, platforms which |
| 2115 | /// required strict binary compatibility with older versions of GCC |
| 2116 | /// may need to exempt themselves. |
| 2117 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2118 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2121 | /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to |
| 2122 | /// classify it as INTEGER (for compatibility with older clang compilers). |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2123 | bool classifyIntegerMMXAsSSE() const { |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2124 | // Clang <= 3.8 did not do this. |
| 2125 | if (getCodeGenOpts().getClangABICompat() <= |
| 2126 | CodeGenOptions::ClangABI::Ver3_8) |
| 2127 | return false; |
| 2128 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2129 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2130 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2131 | return false; |
| 2132 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2133 | return false; |
| 2134 | return true; |
| 2135 | } |
| 2136 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2137 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2138 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2139 | // 64-bit hardware. |
| 2140 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2141 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2142 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2143 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2144 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2145 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2146 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2147 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2148 | bool isPassedUsingAVXType(QualType type) const { |
| 2149 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2150 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2151 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2152 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2153 | if (info.isDirect()) { |
| 2154 | llvm::Type *ty = info.getCoerceToType(); |
| 2155 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2156 | return (vectorTy->getBitWidth() > 128); |
| 2157 | } |
| 2158 | return false; |
| 2159 | } |
| 2160 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2161 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2162 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2163 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2164 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2165 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2166 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2167 | |
| 2168 | bool has64BitPointers() const { |
| 2169 | return Has64BitPointers; |
| 2170 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2171 | |
| 2172 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 2173 | ArrayRef<llvm::Type*> scalars, |
| 2174 | bool asReturnValue) const override { |
| 2175 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2176 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2177 | bool isSwiftErrorInRegister() const override { |
| 2178 | return true; |
| 2179 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2180 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2181 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2182 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2183 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2184 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2185 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2186 | : SwiftABIInfo(CGT), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2187 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2188 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2189 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2190 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2191 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2192 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2193 | |
| 2194 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2195 | // FIXME: Assumes vectorcall is in use. |
| 2196 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2197 | } |
| 2198 | |
| 2199 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2200 | uint64_t NumMembers) const override { |
| 2201 | // FIXME: Assumes vectorcall is in use. |
| 2202 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2203 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2204 | |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2205 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 2206 | ArrayRef<llvm::Type *> scalars, |
| 2207 | bool asReturnValue) const override { |
| 2208 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2209 | } |
| 2210 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2211 | bool isSwiftErrorInRegister() const override { |
| 2212 | return true; |
| 2213 | } |
| 2214 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2215 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2216 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2217 | bool IsVectorCall, bool IsRegCall) const; |
| 2218 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2219 | const ABIArgInfo ¤t) const; |
| 2220 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2221 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2222 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2223 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2224 | }; |
| 2225 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2226 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2227 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2228 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2229 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2230 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2231 | const X86_64ABIInfo &getABIInfo() const { |
| 2232 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2233 | } |
| 2234 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2235 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2236 | return 7; |
| 2237 | } |
| 2238 | |
| 2239 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2240 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2241 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2242 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2243 | // 0-15 are the 16 integer registers. |
| 2244 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2245 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2246 | return false; |
| 2247 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2248 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2249 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2250 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2251 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2252 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2253 | } |
| 2254 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2255 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2256 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2257 | // The default CC on x86-64 sets %al to the number of SSA |
| 2258 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2259 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2260 | // that when AVX types are involved: the ABI explicitly states it is |
| 2261 | // undefined, and it doesn't work in practice because of how the ABI |
| 2262 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2263 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2264 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2265 | for (CallArgList::const_iterator |
| 2266 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2267 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2268 | HasAVXType = true; |
| 2269 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2270 | } |
| 2271 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2272 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2273 | if (!HasAVXType) |
| 2274 | return true; |
| 2275 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2276 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2277 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2280 | llvm::Constant * |
| 2281 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 2282 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 2283 | (0x06 << 8) | // .+0x08 |
| 2284 | ('v' << 16) | |
| 2285 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2286 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2287 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2288 | |
| 2289 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2290 | CodeGen::CodeGenModule &CGM, |
| 2291 | ForDefinition_t IsForDefinition) const override { |
| 2292 | if (!IsForDefinition) |
| 2293 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2294 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2295 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 2296 | // Get the LLVM function. |
| 2297 | auto *Fn = cast<llvm::Function>(GV); |
| 2298 | |
| 2299 | // Now add the 'alignstack' attribute with a value of 16. |
| 2300 | llvm::AttrBuilder B; |
| 2301 | B.addStackAlignmentAttr(16); |
| 2302 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 2303 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2304 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2305 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2306 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2307 | } |
| 2308 | } |
| 2309 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2310 | }; |
| 2311 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2312 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2313 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2314 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2315 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2316 | |
| 2317 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 2318 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2319 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 2320 | // If the argument contains a space, enclose it in quotes. |
| 2321 | if (Lib.find(" ") != StringRef::npos) |
| 2322 | Opt += "\"" + Lib.str() + "\""; |
| 2323 | else |
| 2324 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2325 | } |
| 2326 | }; |
| 2327 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2328 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2329 | // If the argument does not end in .lib, automatically add the suffix. |
| 2330 | // If the argument contains a space, enclose it in quotes. |
| 2331 | // This matches the behavior of MSVC. |
| 2332 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2333 | std::string ArgStr = Quote ? "\"" : ""; |
| 2334 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 2335 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2336 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2337 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2338 | return ArgStr; |
| 2339 | } |
| 2340 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2341 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2342 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2343 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2344 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2345 | unsigned NumRegisterParameters) |
| 2346 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2347 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2348 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2349 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2350 | CodeGen::CodeGenModule &CGM, |
| 2351 | ForDefinition_t IsForDefinition) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2352 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2353 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2354 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2355 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2356 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2357 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2358 | |
| 2359 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2360 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2361 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2362 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2363 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2364 | }; |
| 2365 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2366 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 2367 | llvm::GlobalValue *GV, |
| 2368 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 2369 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2370 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 2371 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2372 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2373 | Fn->addFnAttr("stack-probe-size", |
| 2374 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } |
| 2377 | } |
| 2378 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2379 | void WinX86_32TargetCodeGenInfo::setTargetAttributes( |
| 2380 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 2381 | ForDefinition_t IsForDefinition) const { |
| 2382 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition); |
| 2383 | if (!IsForDefinition) |
| 2384 | return; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2385 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2386 | } |
| 2387 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2388 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2389 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2390 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2391 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2392 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2393 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2394 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2395 | CodeGen::CodeGenModule &CGM, |
| 2396 | ForDefinition_t IsForDefinition) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2397 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2398 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2399 | return 7; |
| 2400 | } |
| 2401 | |
| 2402 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2403 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2404 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2405 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2406 | // 0-15 are the 16 integer registers. |
| 2407 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2408 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2409 | return false; |
| 2410 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2411 | |
| 2412 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2413 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2414 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2415 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2416 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2417 | |
| 2418 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2419 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2420 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2421 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2422 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2423 | }; |
| 2424 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2425 | void WinX86_64TargetCodeGenInfo::setTargetAttributes( |
| 2426 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 2427 | ForDefinition_t IsForDefinition) const { |
| 2428 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition); |
| 2429 | if (!IsForDefinition) |
| 2430 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2431 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2432 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 2433 | // Get the LLVM function. |
| 2434 | auto *Fn = cast<llvm::Function>(GV); |
| 2435 | |
| 2436 | // Now add the 'alignstack' attribute with a value of 16. |
| 2437 | llvm::AttrBuilder B; |
| 2438 | B.addStackAlignmentAttr(16); |
| 2439 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 2440 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2441 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2442 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2443 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2444 | } |
| 2445 | } |
| 2446 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2447 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2448 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2449 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2450 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2451 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2452 | Class &Hi) const { |
| 2453 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2454 | // |
| 2455 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2456 | // memory. |
| 2457 | // |
| 2458 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2459 | // memory. |
| 2460 | // |
| 2461 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2462 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2463 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2464 | // ABI working for processors that don't support the __m256 type. |
| 2465 | // |
| 2466 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2467 | // |
| 2468 | // Some of these are enforced by the merging logic. Others can arise |
| 2469 | // only with unions; for example: |
| 2470 | // union { _Complex double; unsigned; } |
| 2471 | // |
| 2472 | // Note that clauses (b) and (c) were added in 0.98. |
| 2473 | // |
| 2474 | if (Hi == Memory) |
| 2475 | Lo = Memory; |
| 2476 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2477 | Lo = Memory; |
| 2478 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2479 | Lo = Memory; |
| 2480 | if (Hi == SSEUp && Lo != SSE) |
| 2481 | Hi = SSE; |
| 2482 | } |
| 2483 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2484 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2485 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2486 | // classified recursively so that always two fields are |
| 2487 | // considered. The resulting class is calculated according to |
| 2488 | // the classes of the fields in the eightbyte: |
| 2489 | // |
| 2490 | // (a) If both classes are equal, this is the resulting class. |
| 2491 | // |
| 2492 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2493 | // the other class. |
| 2494 | // |
| 2495 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2496 | // class. |
| 2497 | // |
| 2498 | // (d) If one of the classes is INTEGER, the result is the |
| 2499 | // INTEGER. |
| 2500 | // |
| 2501 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2502 | // MEMORY is used as class. |
| 2503 | // |
| 2504 | // (f) Otherwise class SSE is used. |
| 2505 | |
| 2506 | // Accum should never be memory (we should have returned) or |
| 2507 | // ComplexX87 (because this cannot be passed in a structure). |
| 2508 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2509 | "Invalid accumulated classification during merge."); |
| 2510 | if (Accum == Field || Field == NoClass) |
| 2511 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2512 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2513 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2514 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2515 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2516 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2517 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2518 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2519 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2520 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2521 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2524 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2525 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2526 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2527 | // Class pairs with appropriate constructor methods for the various |
| 2528 | // situations. |
| 2529 | |
| 2530 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2531 | // shouldn't be passed in registers for example, so there is no chance they |
| 2532 | // can straddle an eightbyte. Verify & simplify. |
| 2533 | |
| 2534 | Lo = Hi = NoClass; |
| 2535 | |
| 2536 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2537 | Current = Memory; |
| 2538 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2539 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2540 | BuiltinType::Kind k = BT->getKind(); |
| 2541 | |
| 2542 | if (k == BuiltinType::Void) { |
| 2543 | Current = NoClass; |
| 2544 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2545 | Lo = Integer; |
| 2546 | Hi = Integer; |
| 2547 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2548 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2549 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2550 | Current = SSE; |
| 2551 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2552 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2553 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2554 | Lo = SSE; |
| 2555 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2556 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2557 | Lo = X87; |
| 2558 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2559 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2560 | Current = SSE; |
| 2561 | } else |
| 2562 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2563 | } |
| 2564 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2565 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2566 | return; |
| 2567 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2568 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2569 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2570 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2571 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2572 | return; |
| 2573 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2574 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2575 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2576 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2577 | return; |
| 2578 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2579 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2580 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2581 | if (Ty->isMemberFunctionPointerType()) { |
| 2582 | if (Has64BitPointers) { |
| 2583 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2584 | // Lo and Hi now. |
| 2585 | Lo = Hi = Integer; |
| 2586 | } else { |
| 2587 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2588 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2589 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2590 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2591 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2592 | Lo = Hi = Integer; |
| 2593 | } else { |
| 2594 | Current = Integer; |
| 2595 | } |
| 2596 | } |
| 2597 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2598 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2599 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2600 | return; |
| 2601 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2602 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2603 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2604 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2605 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2606 | // gcc passes the following as integer: |
| 2607 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2608 | // 2 bytes - <2 x char>, <1 x short> |
| 2609 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2610 | Current = Integer; |
| 2611 | |
| 2612 | // If this type crosses an eightbyte boundary, it should be |
| 2613 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2614 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2615 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2616 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2617 | Hi = Lo; |
| 2618 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2619 | QualType ElementType = VT->getElementType(); |
| 2620 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2621 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2622 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2623 | return; |
| 2624 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2625 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2626 | // pass them as integer. For platforms where clang is the de facto |
| 2627 | // platform compiler, we must continue to use integer. |
| 2628 | if (!classifyIntegerMMXAsSSE() && |
| 2629 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2630 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2631 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2632 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2633 | Current = Integer; |
| 2634 | else |
| 2635 | Current = SSE; |
| 2636 | |
| 2637 | // If this type crosses an eightbyte boundary, it should be |
| 2638 | // split. |
| 2639 | if (OffsetBase && OffsetBase != 64) |
| 2640 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2641 | } else if (Size == 128 || |
| 2642 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2643 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2644 | // least significant one belongs to class SSE and all the others to class |
| 2645 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2646 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2647 | // This design isn't correct for 256-bits, but since there're no cases |
| 2648 | // where the upper parts would need to be inspected, avoid adding |
| 2649 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2650 | // |
| 2651 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2652 | // registers if they are "named", i.e. not part of the "..." of a |
| 2653 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2654 | // |
| 2655 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2656 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2657 | Lo = SSE; |
| 2658 | Hi = SSEUp; |
| 2659 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2660 | return; |
| 2661 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2662 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2663 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2664 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2665 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2666 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2667 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2668 | if (Size <= 64) |
| 2669 | Current = Integer; |
| 2670 | else if (Size <= 128) |
| 2671 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2672 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2673 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2674 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2675 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2676 | } else if (ET == getContext().LongDoubleTy) { |
| 2677 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2678 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2679 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2680 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2681 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2682 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2683 | Lo = Hi = SSE; |
| 2684 | else |
| 2685 | llvm_unreachable("unexpected long double representation!"); |
| 2686 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2687 | |
| 2688 | // If this complex type crosses an eightbyte boundary then it |
| 2689 | // should be split. |
| 2690 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2691 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2692 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2693 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2694 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2695 | return; |
| 2696 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2697 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2698 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2699 | // Arrays are treated like structures. |
| 2700 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2701 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2702 | |
| 2703 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2704 | // than eight eightbytes, ..., it has class MEMORY. |
| 2705 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2706 | return; |
| 2707 | |
| 2708 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2709 | // fields, it has class MEMORY. |
| 2710 | // |
| 2711 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2712 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2713 | return; |
| 2714 | |
| 2715 | // Otherwise implement simplified merge. We could be smarter about |
| 2716 | // this, but it isn't worth it and would be harder to verify. |
| 2717 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2718 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2719 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2720 | |
| 2721 | // The only case a 256-bit wide vector could be used is when the array |
| 2722 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2723 | // to work for sizes wider than 128, early check and fallback to memory. |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2724 | // |
| 2725 | if (Size > 128 && |
| 2726 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2727 | return; |
| 2728 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2729 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2730 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2731 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2732 | Lo = merge(Lo, FieldLo); |
| 2733 | Hi = merge(Hi, FieldHi); |
| 2734 | if (Lo == Memory || Hi == Memory) |
| 2735 | break; |
| 2736 | } |
| 2737 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2738 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2739 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2740 | return; |
| 2741 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2742 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2743 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2744 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2745 | |
| 2746 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2747 | // than eight eightbytes, ..., it has class MEMORY. |
| 2748 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2749 | return; |
| 2750 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2751 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2752 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2753 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2754 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2755 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2756 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2757 | const RecordDecl *RD = RT->getDecl(); |
| 2758 | |
| 2759 | // Assume variable sized types are passed in memory. |
| 2760 | if (RD->hasFlexibleArrayMember()) |
| 2761 | return; |
| 2762 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2763 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2764 | |
| 2765 | // Reset Lo class, this will be recomputed. |
| 2766 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2767 | |
| 2768 | // If this is a C++ record, classify the bases first. |
| 2769 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2770 | for (const auto &I : CXXRD->bases()) { |
| 2771 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2772 | "Unexpected base class!"); |
| 2773 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2774 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2775 | |
| 2776 | // Classify this field. |
| 2777 | // |
| 2778 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2779 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2780 | // initialized to class NO_CLASS. |
| 2781 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2782 | uint64_t Offset = |
| 2783 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2784 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2785 | Lo = merge(Lo, FieldLo); |
| 2786 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2787 | if (Lo == Memory || Hi == Memory) { |
| 2788 | postMerge(Size, Lo, Hi); |
| 2789 | return; |
| 2790 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2795 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2796 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2797 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2798 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2799 | bool BitField = i->isBitField(); |
| 2800 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2801 | // Ignore padding bit-fields. |
| 2802 | if (BitField && i->isUnnamedBitfield()) |
| 2803 | continue; |
| 2804 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2805 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2806 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2807 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2808 | // The only case a 256-bit wide vector could be used is when the struct |
| 2809 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2810 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2811 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2812 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2813 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2814 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2815 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2816 | return; |
| 2817 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2818 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2819 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2820 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2821 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2822 | return; |
| 2823 | } |
| 2824 | |
| 2825 | // Classify this field. |
| 2826 | // |
| 2827 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2828 | // exceeds a single eightbyte, each is classified |
| 2829 | // separately. Each eightbyte gets initialized to class |
| 2830 | // NO_CLASS. |
| 2831 | Class FieldLo, FieldHi; |
| 2832 | |
| 2833 | // Bit-fields require special handling, they do not force the |
| 2834 | // structure to be passed in memory even if unaligned, and |
| 2835 | // therefore they can straddle an eightbyte. |
| 2836 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2837 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2838 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2839 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2840 | |
| 2841 | uint64_t EB_Lo = Offset / 64; |
| 2842 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2843 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2844 | if (EB_Lo) { |
| 2845 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2846 | FieldLo = NoClass; |
| 2847 | FieldHi = Integer; |
| 2848 | } else { |
| 2849 | FieldLo = Integer; |
| 2850 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2851 | } |
| 2852 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2853 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2854 | Lo = merge(Lo, FieldLo); |
| 2855 | Hi = merge(Hi, FieldHi); |
| 2856 | if (Lo == Memory || Hi == Memory) |
| 2857 | break; |
| 2858 | } |
| 2859 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2860 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2861 | } |
| 2862 | } |
| 2863 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2864 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2865 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2866 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2867 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2868 | // Treat an enum type as its underlying type. |
| 2869 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2870 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2871 | |
| 2872 | return (Ty->isPromotableIntegerType() ? |
| 2873 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2874 | } |
| 2875 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2876 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2879 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2880 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2881 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2882 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2883 | if (Size <= 64 || Size > LargestVector) |
| 2884 | return true; |
| 2885 | } |
| 2886 | |
| 2887 | return false; |
| 2888 | } |
| 2889 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2890 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2891 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2892 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2893 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2894 | // |
| 2895 | // This assumption is optimistic, as there could be free registers available |
| 2896 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2897 | // the argument in the free register. This does not seem to happen currently, |
| 2898 | // but this code would be much safer if we could mark the argument with |
| 2899 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2900 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2901 | // Treat an enum type as its underlying type. |
| 2902 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2903 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2904 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2905 | return (Ty->isPromotableIntegerType() ? |
| 2906 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2907 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2908 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2909 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2910 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2911 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2912 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2913 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2914 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2915 | |
| 2916 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2917 | // is important for good codegen. |
| 2918 | // |
| 2919 | // We do this by coercing the value into a scalar type which the backend can |
| 2920 | // handle naturally (i.e., without using byval). |
| 2921 | // |
| 2922 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2923 | // free integer registers. Doing this when there are free integer registers |
| 2924 | // would require more care, as we would have to ensure that the coerced value |
| 2925 | // did not claim the unused register. That would require either reording the |
| 2926 | // arguments to the function (so that any subsequent inreg values came first), |
| 2927 | // or only doing this optimization when there were no following arguments that |
| 2928 | // might be inreg. |
| 2929 | // |
| 2930 | // We currently expect it to be rare (particularly in well written code) for |
| 2931 | // arguments to be passed on the stack when there are still free integer |
| 2932 | // registers available (this would typically imply large structs being passed |
| 2933 | // by value), so this seems like a fair tradeoff for now. |
| 2934 | // |
| 2935 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2936 | // attributes. See PR12193. |
| 2937 | if (freeIntRegs == 0) { |
| 2938 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2939 | |
| 2940 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2941 | // type, which will end up on the stack (with alignment 8). |
| 2942 | if (Align == 8 && Size <= 64) |
| 2943 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2944 | Size)); |
| 2945 | } |
| 2946 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2947 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2950 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2951 | /// 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] | 2952 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2953 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2954 | // vectors; strip them off if present. |
| 2955 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2956 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2957 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2958 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2959 | if (isa<llvm::VectorType>(IRType) || |
| 2960 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2961 | return IRType; |
| 2962 | |
| 2963 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2964 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2965 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2966 | |
| 2967 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2968 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2969 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2972 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2973 | /// is known to either be off the end of the specified type or being in |
| 2974 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2975 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2976 | /// classification that put one of the two halves in the INTEGER class. |
| 2977 | /// |
| 2978 | /// It is conservatively correct to return false. |
| 2979 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2980 | unsigned EndBit, ASTContext &Context) { |
| 2981 | // If the bytes being queried are off the end of the type, there is no user |
| 2982 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2983 | // types that don't contain interesting padding. |
| 2984 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2985 | if (TySize <= StartBit) |
| 2986 | return true; |
| 2987 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2988 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2989 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2990 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2991 | |
| 2992 | // Check each element to see if the element overlaps with the queried range. |
| 2993 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2994 | // If the element is after the span we care about, then we're done.. |
| 2995 | unsigned EltOffset = i*EltSize; |
| 2996 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2997 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2998 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2999 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 3000 | EndBit-EltOffset, Context)) |
| 3001 | return false; |
| 3002 | } |
| 3003 | // If it overlaps no elements, then it is safe to process as padding. |
| 3004 | return true; |
| 3005 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3006 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3007 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3008 | const RecordDecl *RD = RT->getDecl(); |
| 3009 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3010 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3011 | // If this is a C++ record, check the bases first. |
| 3012 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3013 | for (const auto &I : CXXRD->bases()) { |
| 3014 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3015 | "Unexpected base class!"); |
| 3016 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3017 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3018 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3019 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 3020 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3021 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3022 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3023 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3024 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3025 | EndBit-BaseOffset, Context)) |
| 3026 | return false; |
| 3027 | } |
| 3028 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3029 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3030 | // Verify that no field has data that overlaps the region of interest. Yes |
| 3031 | // this could be sped up a lot by being smarter about queried fields, |
| 3032 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 3033 | // much. |
| 3034 | unsigned idx = 0; |
| 3035 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3036 | i != e; ++i, ++idx) { |
| 3037 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3038 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3039 | // If we found a field after the region we care about, then we're done. |
| 3040 | if (FieldOffset >= EndBit) break; |
| 3041 | |
| 3042 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 3043 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 3044 | Context)) |
| 3045 | return false; |
| 3046 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3047 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3048 | // If nothing in this record overlapped the area of interest, then we're |
| 3049 | // clean. |
| 3050 | return true; |
| 3051 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3052 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3053 | return false; |
| 3054 | } |
| 3055 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3056 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3057 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3058 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3059 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3060 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3061 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3062 | // Base case if we find a float. |
| 3063 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3064 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3065 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3066 | // 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] | 3067 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3068 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3069 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3070 | IROffset -= SL->getElementOffset(Elt); |
| 3071 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3072 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3073 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3074 | // 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] | 3075 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3076 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3077 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3078 | IROffset -= IROffset/EltSize*EltSize; |
| 3079 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3080 | } |
| 3081 | |
| 3082 | return false; |
| 3083 | } |
| 3084 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3085 | |
| 3086 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3087 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3088 | llvm::Type *X86_64ABIInfo:: |
| 3089 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3090 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3091 | // 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] | 3092 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3093 | // structs that contain 3 floats. |
| 3094 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3095 | SourceOffset*8+64, getContext())) |
| 3096 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3097 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3098 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3099 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3100 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3101 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3102 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3103 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3104 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3105 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3106 | } |
| 3107 | |
| 3108 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3109 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3110 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3111 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3112 | /// 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] | 3113 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3114 | /// etc). |
| 3115 | /// |
| 3116 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3117 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3118 | /// the 8-byte value references. PrefType may be null. |
| 3119 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3120 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3121 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3122 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3123 | llvm::Type *X86_64ABIInfo:: |
| 3124 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3125 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3126 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3127 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3128 | if (IROffset == 0) { |
| 3129 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3130 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3131 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3132 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3133 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3134 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3135 | // goodness in the source type is just tail padding. This is allowed to |
| 3136 | // kick in for struct {double,int} on the int, but not on |
| 3137 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3138 | // have to do this analysis on the source type because we can't depend on |
| 3139 | // unions being lowered a specific way etc. |
| 3140 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3141 | IRType->isIntegerTy(32) || |
| 3142 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3143 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3144 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3145 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3146 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3147 | SourceOffset*8+64, getContext())) |
| 3148 | return IRType; |
| 3149 | } |
| 3150 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3151 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3152 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3153 | // 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] | 3154 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3155 | if (IROffset < SL->getSizeInBytes()) { |
| 3156 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3157 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3158 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3159 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3160 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3161 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3162 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3163 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3164 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3165 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3166 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3167 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3168 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3169 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3170 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3171 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3172 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3173 | // 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] | 3174 | unsigned TySizeInBytes = |
| 3175 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3176 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3177 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3178 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3179 | // It is always safe to classify this as an integer type up to i64 that |
| 3180 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3181 | return llvm::IntegerType::get(getVMContext(), |
| 3182 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3185 | |
| 3186 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3187 | /// be used as elements of a two register pair to pass or return, return a |
| 3188 | /// first class aggregate to represent them. For example, if the low part of |
| 3189 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3190 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3191 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3192 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3193 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3194 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3195 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3196 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3197 | // the second element at offset 8. Check for this: |
| 3198 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3199 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3200 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3201 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3202 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3203 | // To handle this, we have to increase the size of the low part so that the |
| 3204 | // second element will start at an 8 byte offset. We can't increase the size |
| 3205 | // of the second element because it might make us access off the end of the |
| 3206 | // struct. |
| 3207 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3208 | // There are usually two sorts of types the ABI generation code can produce |
| 3209 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3210 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3211 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3212 | // Promote these to a larger type. |
| 3213 | if (Lo->isFloatTy()) |
| 3214 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3215 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3216 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3217 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3218 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3219 | } |
| 3220 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3221 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3222 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3223 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3224 | // Verify that the second element is at an 8-byte offset. |
| 3225 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3226 | "Invalid x86-64 argument pair!"); |
| 3227 | return Result; |
| 3228 | } |
| 3229 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3230 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3231 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3232 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3233 | // classification algorithm. |
| 3234 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3235 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3236 | |
| 3237 | // Check some invariants. |
| 3238 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3239 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3240 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3241 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3242 | switch (Lo) { |
| 3243 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3244 | if (Hi == NoClass) |
| 3245 | return ABIArgInfo::getIgnore(); |
| 3246 | // If the low part is just padding, it takes no register, leave ResType |
| 3247 | // null. |
| 3248 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3249 | "Unknown missing lo part"); |
| 3250 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3251 | |
| 3252 | case SSEUp: |
| 3253 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3254 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3255 | |
| 3256 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3257 | // hidden argument. |
| 3258 | case Memory: |
| 3259 | return getIndirectReturnResult(RetTy); |
| 3260 | |
| 3261 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3262 | // available register of the sequence %rax, %rdx is used. |
| 3263 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3264 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3265 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3266 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3267 | // so that the parameter gets the right LLVM IR attributes. |
| 3268 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3269 | // Treat an enum type as its underlying type. |
| 3270 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3271 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3272 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3273 | if (RetTy->isIntegralOrEnumerationType() && |
| 3274 | RetTy->isPromotableIntegerType()) |
| 3275 | return ABIArgInfo::getExtend(); |
| 3276 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3277 | break; |
| 3278 | |
| 3279 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3280 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3281 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3282 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3283 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3284 | |
| 3285 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3286 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3287 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3288 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3289 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3290 | |
| 3291 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3292 | // part of the value is returned in %st0 and the imaginary part in |
| 3293 | // %st1. |
| 3294 | case ComplexX87: |
| 3295 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3296 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3297 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3298 | break; |
| 3299 | } |
| 3300 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3301 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3302 | switch (Hi) { |
| 3303 | // Memory was handled previously and X87 should |
| 3304 | // never occur as a hi class. |
| 3305 | case Memory: |
| 3306 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3307 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3308 | |
| 3309 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3310 | case NoClass: |
| 3311 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3312 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3313 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3314 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3315 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3316 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3317 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3318 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3319 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3320 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3321 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3322 | break; |
| 3323 | |
| 3324 | // 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] | 3325 | // is passed in the next available eightbyte chunk if the last used |
| 3326 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3327 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3328 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3329 | case SSEUp: |
| 3330 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3331 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3332 | break; |
| 3333 | |
| 3334 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3335 | // returned together with the previous X87 value in %st0. |
| 3336 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3337 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3338 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3339 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3340 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3341 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3342 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3343 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3344 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3345 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3346 | break; |
| 3347 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3348 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3349 | // 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] | 3350 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3351 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3352 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3353 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3354 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3355 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3358 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3359 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3360 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3361 | const |
| 3362 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3363 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3364 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3365 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3366 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3367 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3368 | // Check some invariants. |
| 3369 | // FIXME: Enforce these by construction. |
| 3370 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3371 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3372 | |
| 3373 | neededInt = 0; |
| 3374 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3375 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3376 | switch (Lo) { |
| 3377 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3378 | if (Hi == NoClass) |
| 3379 | return ABIArgInfo::getIgnore(); |
| 3380 | // If the low part is just padding, it takes no register, leave ResType |
| 3381 | // null. |
| 3382 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3383 | "Unknown missing lo part"); |
| 3384 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3385 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3386 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3387 | // on the stack. |
| 3388 | case Memory: |
| 3389 | |
| 3390 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3391 | // COMPLEX_X87, it is passed in memory. |
| 3392 | case X87: |
| 3393 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3394 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3395 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3396 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3397 | |
| 3398 | case SSEUp: |
| 3399 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3400 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3401 | |
| 3402 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3403 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3404 | // and %r9 is used. |
| 3405 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3406 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3407 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3408 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3409 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3410 | |
| 3411 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3412 | // so that the parameter gets the right LLVM IR attributes. |
| 3413 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3414 | // Treat an enum type as its underlying type. |
| 3415 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3416 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3417 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3418 | if (Ty->isIntegralOrEnumerationType() && |
| 3419 | Ty->isPromotableIntegerType()) |
| 3420 | return ABIArgInfo::getExtend(); |
| 3421 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3422 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3423 | break; |
| 3424 | |
| 3425 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3426 | // available SSE register is used, the registers are taken in the |
| 3427 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3428 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3429 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3430 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3431 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3432 | break; |
| 3433 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3434 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3435 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3436 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3437 | switch (Hi) { |
| 3438 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3439 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3440 | // which is passed in memory. |
| 3441 | case Memory: |
| 3442 | case X87: |
| 3443 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3444 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3445 | |
| 3446 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3447 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3448 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3449 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3450 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3451 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3452 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3453 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3454 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3455 | break; |
| 3456 | |
| 3457 | // X87Up generally doesn't occur here (long double is passed in |
| 3458 | // memory), except in situations involving unions. |
| 3459 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3460 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3461 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3462 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3463 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3464 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3465 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3466 | ++neededSSE; |
| 3467 | break; |
| 3468 | |
| 3469 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3470 | // 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] | 3471 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3472 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3473 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3474 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3475 | break; |
| 3476 | } |
| 3477 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3478 | // If a high part was specified, merge it together with the low part. It is |
| 3479 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3480 | // first class struct aggregate with the high and low part: {low, high} |
| 3481 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3482 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3483 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3484 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3487 | ABIArgInfo |
| 3488 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3489 | unsigned &NeededSSE) const { |
| 3490 | auto RT = Ty->getAs<RecordType>(); |
| 3491 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3492 | |
| 3493 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3494 | return getIndirectReturnResult(Ty); |
| 3495 | |
| 3496 | // Sum up bases |
| 3497 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3498 | if (CXXRD->isDynamicClass()) { |
| 3499 | NeededInt = NeededSSE = 0; |
| 3500 | return getIndirectReturnResult(Ty); |
| 3501 | } |
| 3502 | |
| 3503 | for (const auto &I : CXXRD->bases()) |
| 3504 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3505 | .isIndirect()) { |
| 3506 | NeededInt = NeededSSE = 0; |
| 3507 | return getIndirectReturnResult(Ty); |
| 3508 | } |
| 3509 | } |
| 3510 | |
| 3511 | // Sum up members |
| 3512 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3513 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3514 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3515 | .isIndirect()) { |
| 3516 | NeededInt = NeededSSE = 0; |
| 3517 | return getIndirectReturnResult(Ty); |
| 3518 | } |
| 3519 | } else { |
| 3520 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3521 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3522 | LocalNeededSSE, true) |
| 3523 | .isIndirect()) { |
| 3524 | NeededInt = NeededSSE = 0; |
| 3525 | return getIndirectReturnResult(Ty); |
| 3526 | } |
| 3527 | NeededInt += LocalNeededInt; |
| 3528 | NeededSSE += LocalNeededSSE; |
| 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | return ABIArgInfo::getDirect(); |
| 3533 | } |
| 3534 | |
| 3535 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3536 | unsigned &NeededInt, |
| 3537 | unsigned &NeededSSE) const { |
| 3538 | |
| 3539 | NeededInt = 0; |
| 3540 | NeededSSE = 0; |
| 3541 | |
| 3542 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3543 | } |
| 3544 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3545 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3546 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3547 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3548 | |
| 3549 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3550 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3551 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3552 | unsigned NeededInt, NeededSSE; |
| 3553 | |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 3554 | if (!getCXXABI().classifyReturnType(FI)) { |
| 3555 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3556 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3557 | FI.getReturnInfo() = |
| 3558 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3559 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3560 | FreeIntRegs -= NeededInt; |
| 3561 | FreeSSERegs -= NeededSSE; |
| 3562 | } else { |
| 3563 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3564 | } |
| 3565 | } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) { |
| 3566 | // Complex Long Double Type is passed in Memory when Regcall |
| 3567 | // calling convention is used. |
| 3568 | const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>(); |
| 3569 | if (getContext().getCanonicalType(CT->getElementType()) == |
| 3570 | getContext().LongDoubleTy) |
| 3571 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3572 | } else |
| 3573 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3574 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3575 | |
| 3576 | // If the return value is indirect, then the hidden argument is consuming one |
| 3577 | // integer register. |
| 3578 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3579 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3580 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3581 | // The chain argument effectively gives us another free register. |
| 3582 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3583 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3584 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3585 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3586 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3587 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3588 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3589 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3590 | it != ie; ++it, ++ArgNo) { |
| 3591 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3592 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3593 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3594 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3595 | else |
| 3596 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3597 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3598 | |
| 3599 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3600 | // eightbyte of an argument, the whole argument is passed on the |
| 3601 | // stack. If registers have already been assigned for some |
| 3602 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3603 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3604 | FreeIntRegs -= NeededInt; |
| 3605 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3606 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3607 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3608 | } |
| 3609 | } |
| 3610 | } |
| 3611 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3612 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3613 | Address VAListAddr, QualType Ty) { |
| 3614 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3615 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3616 | llvm::Value *overflow_arg_area = |
| 3617 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3618 | |
| 3619 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3620 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3621 | // It isn't stated explicitly in the standard, but in practice we use |
| 3622 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3623 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3624 | if (Align > CharUnits::fromQuantity(8)) { |
| 3625 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3626 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | // 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] | 3630 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3631 | llvm::Value *Res = |
| 3632 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3633 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3634 | |
| 3635 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3636 | // l->overflow_arg_area + sizeof(type). |
| 3637 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3638 | // an 8 byte boundary. |
| 3639 | |
| 3640 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3641 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3642 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3643 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3644 | "overflow_arg_area.next"); |
| 3645 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3646 | |
| 3647 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3648 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3651 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3652 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3653 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3654 | // struct { |
| 3655 | // i32 gp_offset; |
| 3656 | // i32 fp_offset; |
| 3657 | // i8* overflow_arg_area; |
| 3658 | // i8* reg_save_area; |
| 3659 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3660 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3661 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3662 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3663 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3664 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3665 | |
| 3666 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3667 | // in the registers. If not go to step 7. |
| 3668 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3669 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3670 | |
| 3671 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3672 | // general purpose registers needed to pass type and num_fp to hold |
| 3673 | // the number of floating point registers needed. |
| 3674 | |
| 3675 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3676 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3677 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3678 | // |
| 3679 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3680 | // register save space). |
| 3681 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3682 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3683 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3684 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3685 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3686 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3687 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3688 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3689 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3690 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3691 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3695 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3696 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3697 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3698 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3699 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3700 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3701 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3702 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3703 | } |
| 3704 | |
| 3705 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3706 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3707 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3708 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3709 | |
| 3710 | // Emit code to load the value if it was passed in registers. |
| 3711 | |
| 3712 | CGF.EmitBlock(InRegBlock); |
| 3713 | |
| 3714 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3715 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3716 | // copying to a temporary location in case the parameter is passed |
| 3717 | // in different register classes or requires an alignment greater |
| 3718 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3719 | // |
| 3720 | // FIXME: This really results in shameful code when we end up needing to |
| 3721 | // collect arguments from different places; often what should result in a |
| 3722 | // simple assembling of a structure from scattered addresses has many more |
| 3723 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3724 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3725 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3726 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3727 | "reg_save_area"); |
| 3728 | |
| 3729 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3730 | if (neededInt && neededSSE) { |
| 3731 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3732 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3733 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3734 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3735 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3736 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3737 | llvm::Type *TyLo = ST->getElementType(0); |
| 3738 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3739 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3740 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3741 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3742 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3743 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3744 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3745 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3746 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3747 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3748 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3749 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3750 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3751 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3752 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3753 | CGF.Builder.CreateStore(V, |
| 3754 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3755 | |
| 3756 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3757 | V = CGF.Builder.CreateAlignedLoad( |
| 3758 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3759 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3760 | CharUnits Offset = CharUnits::fromQuantity( |
| 3761 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3762 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3763 | |
| 3764 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3765 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3766 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3767 | CharUnits::fromQuantity(8)); |
| 3768 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3769 | |
| 3770 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3771 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3772 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3773 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3774 | CharUnits TyAlign = SizeAlign.second; |
| 3775 | |
| 3776 | // Copy into a temporary if the type is more aligned than the |
| 3777 | // register save area. |
| 3778 | if (TyAlign.getQuantity() > 8) { |
| 3779 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3780 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3781 | RegAddr = Tmp; |
| 3782 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3783 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3784 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3785 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3786 | CharUnits::fromQuantity(16)); |
| 3787 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3788 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3789 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3790 | // SSE registers are spaced 16 bytes apart in the register save |
| 3791 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3792 | // The ABI isn't explicit about this, but it seems reasonable |
| 3793 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3794 | // naturally 16-byte aligned and the prologue is expected to store |
| 3795 | // all the SSE registers to the RSA. |
| 3796 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3797 | CharUnits::fromQuantity(16)); |
| 3798 | Address RegAddrHi = |
| 3799 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3800 | CharUnits::fromQuantity(16)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3801 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3802 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3803 | llvm::Value *V; |
| 3804 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3805 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3806 | V = CGF.Builder.CreateLoad( |
| 3807 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3808 | CGF.Builder.CreateStore(V, |
| 3809 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3810 | V = CGF.Builder.CreateLoad( |
| 3811 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3812 | CGF.Builder.CreateStore(V, |
| 3813 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3814 | |
| 3815 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3819 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3820 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3821 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3822 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3823 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3824 | gp_offset_p); |
| 3825 | } |
| 3826 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3827 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3828 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3829 | fp_offset_p); |
| 3830 | } |
| 3831 | CGF.EmitBranch(ContBlock); |
| 3832 | |
| 3833 | // Emit code to load the value if it was passed in memory. |
| 3834 | |
| 3835 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3836 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3837 | |
| 3838 | // Return the appropriate result. |
| 3839 | |
| 3840 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3841 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3842 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3843 | return ResAddr; |
| 3844 | } |
| 3845 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3846 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3847 | QualType Ty) const { |
| 3848 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3849 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3850 | CharUnits::fromQuantity(8), |
| 3851 | /*allowHigherAlign*/ false); |
| 3852 | } |
| 3853 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3854 | ABIArgInfo |
| 3855 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3856 | const ABIArgInfo ¤t) const { |
| 3857 | // Assumes vectorCall calling convention. |
| 3858 | const Type *Base = nullptr; |
| 3859 | uint64_t NumElts = 0; |
| 3860 | |
| 3861 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3862 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3863 | FreeSSERegs -= NumElts; |
| 3864 | return getDirectX86Hva(); |
| 3865 | } |
| 3866 | return current; |
| 3867 | } |
| 3868 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3869 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3870 | bool IsReturnType, bool IsVectorCall, |
| 3871 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3872 | |
| 3873 | if (Ty->isVoidType()) |
| 3874 | return ABIArgInfo::getIgnore(); |
| 3875 | |
| 3876 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3877 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3878 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3879 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3880 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3881 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3882 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3883 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3884 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3885 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3886 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3887 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3891 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3892 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3893 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3894 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3895 | const Type *Base = nullptr; |
| 3896 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3897 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3898 | // other targets. |
| 3899 | if ((IsVectorCall || IsRegCall) && |
| 3900 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3901 | if (IsRegCall) { |
| 3902 | if (FreeSSERegs >= NumElts) { |
| 3903 | FreeSSERegs -= NumElts; |
| 3904 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3905 | return ABIArgInfo::getDirect(); |
| 3906 | return ABIArgInfo::getExpand(); |
| 3907 | } |
| 3908 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3909 | } else if (IsVectorCall) { |
| 3910 | if (FreeSSERegs >= NumElts && |
| 3911 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3912 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3913 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3914 | } else if (IsReturnType) { |
| 3915 | return ABIArgInfo::getExpand(); |
| 3916 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3917 | // HVAs are delayed and reclassified in the 2nd step. |
| 3918 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3919 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3920 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3923 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3924 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3925 | // directly. |
| 3926 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3927 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3928 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3931 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3932 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3933 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3934 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3935 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3936 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3937 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3938 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3941 | // Bool type is always extended to the ABI, other builtin types are not |
| 3942 | // extended. |
| 3943 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3944 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3945 | return ABIArgInfo::getExtend(); |
| 3946 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3947 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3948 | // passes them indirectly through memory. |
| 3949 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3950 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3951 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3952 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3953 | } |
| 3954 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3955 | return ABIArgInfo::getDirect(); |
| 3956 | } |
| 3957 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3958 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 3959 | unsigned FreeSSERegs, |
| 3960 | bool IsVectorCall, |
| 3961 | bool IsRegCall) const { |
| 3962 | unsigned Count = 0; |
| 3963 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 3964 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 3965 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3966 | if (Count < VectorcallMaxParamNumAsReg) |
| 3967 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 3968 | else { |
| 3969 | // Since these cannot be passed in registers, pretend no registers |
| 3970 | // are left. |
| 3971 | unsigned ZeroSSERegsAvail = 0; |
| 3972 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 3973 | IsVectorCall, IsRegCall); |
| 3974 | } |
| 3975 | ++Count; |
| 3976 | } |
| 3977 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3978 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 3979 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3980 | } |
| 3981 | } |
| 3982 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3983 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3984 | bool IsVectorCall = |
| 3985 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3986 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3987 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3988 | unsigned FreeSSERegs = 0; |
| 3989 | if (IsVectorCall) { |
| 3990 | // We can use up to 4 SSE return registers with vectorcall. |
| 3991 | FreeSSERegs = 4; |
| 3992 | } else if (IsRegCall) { |
| 3993 | // RegCall gives us 16 SSE registers. |
| 3994 | FreeSSERegs = 16; |
| 3995 | } |
| 3996 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3997 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3998 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 3999 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4000 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4001 | if (IsVectorCall) { |
| 4002 | // We can use up to 6 SSE register parameters with vectorcall. |
| 4003 | FreeSSERegs = 6; |
| 4004 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4005 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4006 | FreeSSERegs = 16; |
| 4007 | } |
| 4008 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4009 | if (IsVectorCall) { |
| 4010 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 4011 | } else { |
| 4012 | for (auto &I : FI.arguments()) |
| 4013 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4014 | } |
| 4015 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4018 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4019 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 4020 | |
| 4021 | bool IsIndirect = false; |
| 4022 | |
| 4023 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 4024 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 4025 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 4026 | uint64_t Width = getContext().getTypeSize(Ty); |
| 4027 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 4028 | } |
| 4029 | |
| 4030 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4031 | CGF.getContext().getTypeInfoInChars(Ty), |
| 4032 | CharUnits::fromQuantity(8), |
| 4033 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4034 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4035 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4036 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4037 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4038 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 4039 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4040 | bool IsSoftFloatABI; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4041 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4042 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 4043 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4044 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4045 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4046 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4047 | }; |
| 4048 | |
| 4049 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4050 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4051 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 4052 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4053 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4054 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4055 | // This is recovered from gcc output. |
| 4056 | return 1; // r1 is the dedicated stack pointer |
| 4057 | } |
| 4058 | |
| 4059 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4060 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4061 | }; |
| 4062 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4063 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4064 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4065 | // TODO: this implementation is now likely redundant with |
| 4066 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4067 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4068 | QualType Ty) const { |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4069 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4070 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4071 | // TODO: Implement this. For now ignore. |
| 4072 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4073 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4076 | // struct __va_list_tag { |
| 4077 | // unsigned char gpr; |
| 4078 | // unsigned char fpr; |
| 4079 | // unsigned short reserved; |
| 4080 | // void *overflow_arg_area; |
| 4081 | // void *reg_save_area; |
| 4082 | // }; |
| 4083 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4084 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4085 | bool isInt = |
| 4086 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4087 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4088 | |
| 4089 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4090 | // with the argument-lowering code. |
| 4091 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4092 | |
| 4093 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4094 | |
| 4095 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4096 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4097 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4098 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 4099 | } else { |
| 4100 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4101 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4102 | |
| 4103 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4104 | |
| 4105 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4106 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4107 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4108 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4109 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4110 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4111 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4112 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4113 | |
| 4114 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4115 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4116 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4117 | |
| 4118 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4119 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4120 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4121 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4122 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4123 | // Case 1: consume registers. |
| 4124 | Address RegAddr = Address::invalid(); |
| 4125 | { |
| 4126 | CGF.EmitBlock(UsingRegs); |
| 4127 | |
| 4128 | Address RegSaveAreaPtr = |
| 4129 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 4130 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4131 | CharUnits::fromQuantity(8)); |
| 4132 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4133 | |
| 4134 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4135 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4136 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4137 | CharUnits::fromQuantity(32)); |
| 4138 | } |
| 4139 | |
| 4140 | // Get the address of the saved value by scaling the number of |
| 4141 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4142 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4143 | llvm::Value *RegOffset = |
| 4144 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4145 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4146 | RegAddr.getPointer(), RegOffset), |
| 4147 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4148 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4149 | |
| 4150 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4151 | NumRegs = |
| 4152 | Builder.CreateAdd(NumRegs, |
| 4153 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4154 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4155 | |
| 4156 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4157 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4158 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4159 | // Case 2: consume space in the overflow area. |
| 4160 | Address MemAddr = Address::invalid(); |
| 4161 | { |
| 4162 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4163 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4164 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4165 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4166 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4167 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4168 | |
| 4169 | CharUnits Size; |
| 4170 | if (!isIndirect) { |
| 4171 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4172 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4173 | } else { |
| 4174 | Size = CGF.getPointerSize(); |
| 4175 | } |
| 4176 | |
| 4177 | Address OverflowAreaAddr = |
| 4178 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4179 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4180 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4181 | // Round up address of argument to alignment |
| 4182 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4183 | if (Align > OverflowAreaAlign) { |
| 4184 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4185 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4186 | Align); |
| 4187 | } |
| 4188 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4189 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4190 | |
| 4191 | // Increase the overflow area. |
| 4192 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4193 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4194 | CGF.EmitBranch(Cont); |
| 4195 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4196 | |
| 4197 | CGF.EmitBlock(Cont); |
| 4198 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4199 | // Merge the cases with a phi. |
| 4200 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4201 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4202 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4203 | // Load the pointer if the argument was passed indirectly. |
| 4204 | if (isIndirect) { |
| 4205 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4206 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4207 | } |
| 4208 | |
| 4209 | return Result; |
| 4210 | } |
| 4211 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4212 | bool |
| 4213 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4214 | llvm::Value *Address) const { |
| 4215 | // This is calculated from the LLVM and GCC tables and verified |
| 4216 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4217 | |
| 4218 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4219 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4220 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4221 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4222 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4223 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4224 | |
| 4225 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4226 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4227 | |
| 4228 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4229 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4230 | |
| 4231 | // 64-76 are various 4-byte special-purpose registers: |
| 4232 | // 64: mq |
| 4233 | // 65: lr |
| 4234 | // 66: ctr |
| 4235 | // 67: ap |
| 4236 | // 68-75 cr0-7 |
| 4237 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4238 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4239 | |
| 4240 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4241 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4242 | |
| 4243 | // 109: vrsave |
| 4244 | // 110: vscr |
| 4245 | // 111: spe_acc |
| 4246 | // 112: spefscr |
| 4247 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4248 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4249 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4250 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4253 | // PowerPC-64 |
| 4254 | |
| 4255 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4256 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4257 | class PPC64_SVR4_ABIInfo : public ABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4258 | public: |
| 4259 | enum ABIKind { |
| 4260 | ELFv1 = 0, |
| 4261 | ELFv2 |
| 4262 | }; |
| 4263 | |
| 4264 | private: |
| 4265 | static const unsigned GPRBits = 64; |
| 4266 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4267 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4268 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4269 | |
| 4270 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4271 | // will be passed in a QPX register. |
| 4272 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4273 | if (!HasQPX) |
| 4274 | return false; |
| 4275 | |
| 4276 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4277 | unsigned NumElements = VT->getNumElements(); |
| 4278 | if (NumElements == 1) |
| 4279 | return false; |
| 4280 | |
| 4281 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4282 | if (getContext().getTypeSize(Ty) <= 256) |
| 4283 | return true; |
| 4284 | } else if (VT->getElementType()-> |
| 4285 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4286 | if (getContext().getTypeSize(Ty) <= 128) |
| 4287 | return true; |
| 4288 | } |
| 4289 | } |
| 4290 | |
| 4291 | return false; |
| 4292 | } |
| 4293 | |
| 4294 | bool IsQPXVectorTy(QualType Ty) const { |
| 4295 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4296 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4297 | |
| 4298 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4299 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4300 | bool SoftFloatABI) |
| 4301 | : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
| 4302 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4303 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4304 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4305 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4306 | |
| 4307 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4308 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4309 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4310 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4311 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4312 | uint64_t Members) const override; |
| 4313 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4314 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4315 | // Example: For aggregate arguments that fit in a register, we could |
| 4316 | // use getDirectInReg (as is done below for structs containing a single |
| 4317 | // floating-point value) to avoid pushing them to memory on function |
| 4318 | // entry. This would require changing the logic in PPCISelLowering |
| 4319 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4320 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4321 | if (!getCXXABI().classifyReturnType(FI)) |
| 4322 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4323 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4324 | // We rely on the default argument classification for the most part. |
| 4325 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4326 | // 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] | 4327 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4328 | if (T) { |
| 4329 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4330 | if (IsQPXVectorTy(T) || |
| 4331 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4332 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4333 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4334 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4335 | continue; |
| 4336 | } |
| 4337 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4338 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4339 | } |
| 4340 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4341 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4342 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4343 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4344 | }; |
| 4345 | |
| 4346 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4347 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4348 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4349 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4350 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4351 | bool SoftFloatABI) |
| 4352 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4353 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4354 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4355 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4356 | // This is recovered from gcc output. |
| 4357 | return 1; // r1 is the dedicated stack pointer |
| 4358 | } |
| 4359 | |
| 4360 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4361 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4362 | }; |
| 4363 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4364 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4365 | public: |
| 4366 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4367 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4368 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4369 | // This is recovered from gcc output. |
| 4370 | return 1; // r1 is the dedicated stack pointer |
| 4371 | } |
| 4372 | |
| 4373 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4374 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4375 | }; |
| 4376 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4377 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4378 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4379 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4380 | // extended to 64 bits. |
| 4381 | bool |
| 4382 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4383 | // Treat an enum type as its underlying type. |
| 4384 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4385 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4386 | |
| 4387 | // Promotable integer types are required to be promoted by the ABI. |
| 4388 | if (Ty->isPromotableIntegerType()) |
| 4389 | return true; |
| 4390 | |
| 4391 | // In addition to the usual promotable integer types, we also need to |
| 4392 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4393 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4394 | switch (BT->getKind()) { |
| 4395 | case BuiltinType::Int: |
| 4396 | case BuiltinType::UInt: |
| 4397 | return true; |
| 4398 | default: |
| 4399 | break; |
| 4400 | } |
| 4401 | |
| 4402 | return false; |
| 4403 | } |
| 4404 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4405 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4406 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4407 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4408 | // Complex types are passed just like their elements. |
| 4409 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4410 | Ty = CTy->getElementType(); |
| 4411 | |
| 4412 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4413 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4414 | if (IsQPXVectorTy(Ty)) { |
| 4415 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4416 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4417 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4418 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4419 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4420 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4421 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4422 | |
| 4423 | // For single-element float/vector structs, we consider the whole type |
| 4424 | // to have the same alignment requirements as its single element. |
| 4425 | const Type *AlignAsType = nullptr; |
| 4426 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4427 | if (EltType) { |
| 4428 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4429 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4430 | getContext().getTypeSize(EltType) == 128) || |
| 4431 | (BT && BT->isFloatingPoint())) |
| 4432 | AlignAsType = EltType; |
| 4433 | } |
| 4434 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4435 | // Likewise for ELFv2 homogeneous aggregates. |
| 4436 | const Type *Base = nullptr; |
| 4437 | uint64_t Members = 0; |
| 4438 | if (!AlignAsType && Kind == ELFv2 && |
| 4439 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4440 | AlignAsType = Base; |
| 4441 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4442 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4443 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4444 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4445 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4446 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4447 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4448 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4449 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4450 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4451 | |
| 4452 | // Otherwise, we only need alignment for any aggregate type that |
| 4453 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4454 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4455 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4456 | return CharUnits::fromQuantity(32); |
| 4457 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4458 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4459 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4460 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4463 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4464 | /// aggregate. Base is set to the base element type, and Members is set |
| 4465 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4466 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4467 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4468 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4469 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4470 | if (NElements == 0) |
| 4471 | return false; |
| 4472 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4473 | return false; |
| 4474 | Members *= NElements; |
| 4475 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4476 | const RecordDecl *RD = RT->getDecl(); |
| 4477 | if (RD->hasFlexibleArrayMember()) |
| 4478 | return false; |
| 4479 | |
| 4480 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4481 | |
| 4482 | // If this is a C++ record, check the bases first. |
| 4483 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4484 | for (const auto &I : CXXRD->bases()) { |
| 4485 | // Ignore empty records. |
| 4486 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4487 | continue; |
| 4488 | |
| 4489 | uint64_t FldMembers; |
| 4490 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4491 | return false; |
| 4492 | |
| 4493 | Members += FldMembers; |
| 4494 | } |
| 4495 | } |
| 4496 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4497 | for (const auto *FD : RD->fields()) { |
| 4498 | // Ignore (non-zero arrays of) empty records. |
| 4499 | QualType FT = FD->getType(); |
| 4500 | while (const ConstantArrayType *AT = |
| 4501 | getContext().getAsConstantArrayType(FT)) { |
| 4502 | if (AT->getSize().getZExtValue() == 0) |
| 4503 | return false; |
| 4504 | FT = AT->getElementType(); |
| 4505 | } |
| 4506 | if (isEmptyRecord(getContext(), FT, true)) |
| 4507 | continue; |
| 4508 | |
| 4509 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4510 | if (getContext().getLangOpts().CPlusPlus && |
| 4511 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4512 | continue; |
| 4513 | |
| 4514 | uint64_t FldMembers; |
| 4515 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4516 | return false; |
| 4517 | |
| 4518 | Members = (RD->isUnion() ? |
| 4519 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4520 | } |
| 4521 | |
| 4522 | if (!Base) |
| 4523 | return false; |
| 4524 | |
| 4525 | // Ensure there is no padding. |
| 4526 | if (getContext().getTypeSize(Base) * Members != |
| 4527 | getContext().getTypeSize(Ty)) |
| 4528 | return false; |
| 4529 | } else { |
| 4530 | Members = 1; |
| 4531 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4532 | Members = 2; |
| 4533 | Ty = CT->getElementType(); |
| 4534 | } |
| 4535 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4536 | // Most ABIs only support float, double, and some vector type widths. |
| 4537 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4538 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4539 | |
| 4540 | // The base type must be the same for all members. Types that |
| 4541 | // agree in both total size and mode (float vs. vector) are |
| 4542 | // treated as being equivalent here. |
| 4543 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4544 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4545 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4546 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4547 | // so make sure to widen it explicitly. |
| 4548 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4549 | QualType EltTy = VT->getElementType(); |
| 4550 | unsigned NumElements = |
| 4551 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4552 | Base = getContext() |
| 4553 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4554 | .getTypePtr(); |
| 4555 | } |
| 4556 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4557 | |
| 4558 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4559 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4560 | return false; |
| 4561 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4562 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4563 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4564 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4565 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4566 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4567 | // double, long double, or 128-bit vectors. |
| 4568 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4569 | if (BT->getKind() == BuiltinType::Float || |
| 4570 | BT->getKind() == BuiltinType::Double || |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4571 | BT->getKind() == BuiltinType::LongDouble) { |
| 4572 | if (IsSoftFloatABI) |
| 4573 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4574 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4575 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4576 | } |
| 4577 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4578 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4579 | return true; |
| 4580 | } |
| 4581 | return false; |
| 4582 | } |
| 4583 | |
| 4584 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4585 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4586 | // Vector types require one register, floating point types require one |
| 4587 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4588 | uint32_t NumRegs = |
| 4589 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4590 | |
| 4591 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4592 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4595 | ABIArgInfo |
| 4596 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4597 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4598 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4599 | if (Ty->isAnyComplexType()) |
| 4600 | return ABIArgInfo::getDirect(); |
| 4601 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4602 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4603 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4604 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4605 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4606 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4607 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4608 | else if (Size < 128) { |
| 4609 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4610 | return ABIArgInfo::getDirect(CoerceTy); |
| 4611 | } |
| 4612 | } |
| 4613 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4614 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4615 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4616 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4617 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4618 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4619 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4620 | |
| 4621 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4622 | const Type *Base = nullptr; |
| 4623 | uint64_t Members = 0; |
| 4624 | if (Kind == ELFv2 && |
| 4625 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4626 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4627 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4628 | return ABIArgInfo::getDirect(CoerceTy); |
| 4629 | } |
| 4630 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4631 | // If an aggregate may end up fully in registers, we do not |
| 4632 | // use the ByVal method, but pass the aggregate as array. |
| 4633 | // This is usually beneficial since we avoid forcing the |
| 4634 | // back-end to store the argument to memory. |
| 4635 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4636 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4637 | llvm::Type *CoerceTy; |
| 4638 | |
| 4639 | // Types up to 8 bytes are passed as integer type (which will be |
| 4640 | // properly aligned in the argument save area doubleword). |
| 4641 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4642 | CoerceTy = |
| 4643 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4644 | // Larger types are passed as arrays, with the base type selected |
| 4645 | // according to the required alignment in the save area. |
| 4646 | else { |
| 4647 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4648 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4649 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4650 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4651 | } |
| 4652 | |
| 4653 | return ABIArgInfo::getDirect(CoerceTy); |
| 4654 | } |
| 4655 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4656 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4657 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4658 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4659 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | return (isPromotableTypeForABI(Ty) ? |
| 4663 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4664 | } |
| 4665 | |
| 4666 | ABIArgInfo |
| 4667 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4668 | if (RetTy->isVoidType()) |
| 4669 | return ABIArgInfo::getIgnore(); |
| 4670 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4671 | if (RetTy->isAnyComplexType()) |
| 4672 | return ABIArgInfo::getDirect(); |
| 4673 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4674 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4675 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4676 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4677 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4678 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4679 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4680 | else if (Size < 128) { |
| 4681 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4682 | return ABIArgInfo::getDirect(CoerceTy); |
| 4683 | } |
| 4684 | } |
| 4685 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4686 | if (isAggregateTypeForABI(RetTy)) { |
| 4687 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4688 | const Type *Base = nullptr; |
| 4689 | uint64_t Members = 0; |
| 4690 | if (Kind == ELFv2 && |
| 4691 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4692 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4693 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4694 | return ABIArgInfo::getDirect(CoerceTy); |
| 4695 | } |
| 4696 | |
| 4697 | // ELFv2 small aggregates are returned in up to two registers. |
| 4698 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4699 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4700 | if (Bits == 0) |
| 4701 | return ABIArgInfo::getIgnore(); |
| 4702 | |
| 4703 | llvm::Type *CoerceTy; |
| 4704 | if (Bits > GPRBits) { |
| 4705 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4706 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4707 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4708 | CoerceTy = |
| 4709 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4710 | return ABIArgInfo::getDirect(CoerceTy); |
| 4711 | } |
| 4712 | |
| 4713 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4714 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4715 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4716 | |
| 4717 | return (isPromotableTypeForABI(RetTy) ? |
| 4718 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4719 | } |
| 4720 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4721 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4722 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4723 | QualType Ty) const { |
| 4724 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4725 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4726 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4727 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4728 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4729 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4730 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4731 | // in separate doublewords. However, Clang expects us to produce a |
| 4732 | // pointer to a structure with the two parts packed tightly. So generate |
| 4733 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4734 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4735 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4736 | CharUnits EltSize = TypeInfo.first / 2; |
| 4737 | if (EltSize < SlotSize) { |
| 4738 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4739 | SlotSize * 2, SlotSize, |
| 4740 | SlotSize, /*AllowHigher*/ true); |
| 4741 | |
| 4742 | Address RealAddr = Addr; |
| 4743 | Address ImagAddr = RealAddr; |
| 4744 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4745 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4746 | SlotSize - EltSize); |
| 4747 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4748 | 2 * SlotSize - EltSize); |
| 4749 | } else { |
| 4750 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4751 | } |
| 4752 | |
| 4753 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4754 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4755 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4756 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4757 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4758 | |
| 4759 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4760 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4761 | /*init*/ true); |
| 4762 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4763 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4764 | } |
| 4765 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4766 | // Otherwise, just use the general rule. |
| 4767 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4768 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | static bool |
| 4772 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4773 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4774 | // This is calculated from the LLVM and GCC tables and verified |
| 4775 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4776 | |
| 4777 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4778 | |
| 4779 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4780 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4781 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4782 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4783 | |
| 4784 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4785 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4786 | |
| 4787 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4788 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4789 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4790 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4791 | // 64: mq |
| 4792 | // 65: lr |
| 4793 | // 66: ctr |
| 4794 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4795 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4796 | |
| 4797 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4798 | // 68-75 cr0-7 |
| 4799 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4800 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4801 | |
| 4802 | // 77-108: v0-31, the 16-byte vector registers |
| 4803 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4804 | |
| 4805 | // 109: vrsave |
| 4806 | // 110: vscr |
| 4807 | // 111: spe_acc |
| 4808 | // 112: spefscr |
| 4809 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4810 | // 114: tfhar |
| 4811 | // 115: tfiar |
| 4812 | // 116: texasr |
| 4813 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4814 | |
| 4815 | return false; |
| 4816 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4817 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4818 | bool |
| 4819 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4820 | CodeGen::CodeGenFunction &CGF, |
| 4821 | llvm::Value *Address) const { |
| 4822 | |
| 4823 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4824 | } |
| 4825 | |
| 4826 | bool |
| 4827 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4828 | llvm::Value *Address) const { |
| 4829 | |
| 4830 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4831 | } |
| 4832 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4833 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4834 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4835 | //===----------------------------------------------------------------------===// |
| 4836 | |
| 4837 | namespace { |
| 4838 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4839 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4840 | public: |
| 4841 | enum ABIKind { |
| 4842 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4843 | DarwinPCS, |
| 4844 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4845 | }; |
| 4846 | |
| 4847 | private: |
| 4848 | ABIKind Kind; |
| 4849 | |
| 4850 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4851 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4852 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4853 | |
| 4854 | private: |
| 4855 | ABIKind getABIKind() const { return Kind; } |
| 4856 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4857 | |
| 4858 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4859 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4860 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4861 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4862 | uint64_t Members) const override; |
| 4863 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4864 | bool isIllegalVectorType(QualType Ty) const; |
| 4865 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4866 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4867 | if (!getCXXABI().classifyReturnType(FI)) |
| 4868 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4869 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4870 | for (auto &it : FI.arguments()) |
| 4871 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4874 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4875 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4876 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4877 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4878 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4879 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4880 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4881 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4882 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 4883 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4884 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4885 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4886 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4887 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4888 | QualType Ty) const override; |
| 4889 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4890 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 4891 | ArrayRef<llvm::Type*> scalars, |
| 4892 | bool asReturnValue) const override { |
| 4893 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4894 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 4895 | bool isSwiftErrorInRegister() const override { |
| 4896 | return true; |
| 4897 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 4898 | |
| 4899 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 4900 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4901 | }; |
| 4902 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4903 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4904 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4905 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4906 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4907 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4908 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 4909 | return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4912 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4913 | return 31; |
| 4914 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4915 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4916 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4917 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 4918 | |
| 4919 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 4920 | public: |
| 4921 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 4922 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 4923 | |
| 4924 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 4925 | llvm::SmallString<24> &Opt) const override { |
| 4926 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 4927 | } |
| 4928 | |
| 4929 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 4930 | llvm::SmallString<32> &Opt) const override { |
| 4931 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 4932 | } |
| 4933 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4934 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4935 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4936 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4937 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4938 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4939 | // Handle illegal vector types here. |
| 4940 | if (isIllegalVectorType(Ty)) { |
| 4941 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4942 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 4943 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4944 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 4945 | return ABIArgInfo::getDirect(ResType); |
| 4946 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4947 | if (Size <= 32) { |
| 4948 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4949 | return ABIArgInfo::getDirect(ResType); |
| 4950 | } |
| 4951 | if (Size == 64) { |
| 4952 | llvm::Type *ResType = |
| 4953 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4954 | return ABIArgInfo::getDirect(ResType); |
| 4955 | } |
| 4956 | if (Size == 128) { |
| 4957 | llvm::Type *ResType = |
| 4958 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4959 | return ABIArgInfo::getDirect(ResType); |
| 4960 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4961 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4962 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4963 | |
| 4964 | if (!isAggregateTypeForABI(Ty)) { |
| 4965 | // Treat an enum type as its underlying type. |
| 4966 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4967 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4968 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4969 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4970 | ? ABIArgInfo::getExtend() |
| 4971 | : ABIArgInfo::getDirect()); |
| 4972 | } |
| 4973 | |
| 4974 | // Structures with either a non-trivial destructor or a non-trivial |
| 4975 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4976 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4977 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4978 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
| 4981 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4982 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4983 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4984 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 4985 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4986 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4987 | return ABIArgInfo::getIgnore(); |
| 4988 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 4989 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 4990 | // 0. |
| 4991 | if (IsEmpty && Size == 0) |
| 4992 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4993 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4994 | } |
| 4995 | |
| 4996 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4997 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4998 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4999 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5000 | return ABIArgInfo::getDirect( |
| 5001 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5002 | } |
| 5003 | |
| 5004 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5005 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5006 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5007 | // same size and alignment. |
| 5008 | if (getTarget().isRenderScriptTarget()) { |
| 5009 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5010 | } |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 5011 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5012 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5013 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5014 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5015 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 5016 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5017 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5018 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5019 | } |
| 5020 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5021 | } |
| 5022 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5023 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5026 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5027 | if (RetTy->isVoidType()) |
| 5028 | return ABIArgInfo::getIgnore(); |
| 5029 | |
| 5030 | // Large vector types should be returned via memory. |
| 5031 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5032 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5033 | |
| 5034 | if (!isAggregateTypeForABI(RetTy)) { |
| 5035 | // Treat an enum type as its underlying type. |
| 5036 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5037 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5038 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5039 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 5040 | ? ABIArgInfo::getExtend() |
| 5041 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5042 | } |
| 5043 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5044 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5045 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5046 | return ABIArgInfo::getIgnore(); |
| 5047 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5048 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5049 | uint64_t Members = 0; |
| 5050 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5051 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 5052 | return ABIArgInfo::getDirect(); |
| 5053 | |
| 5054 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5055 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5056 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5057 | // same size and alignment. |
| 5058 | if (getTarget().isRenderScriptTarget()) { |
| 5059 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5060 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5061 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5062 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5063 | |
| 5064 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5065 | // For aggregates with 16-byte alignment, we use i128. |
| 5066 | if (Alignment < 128 && Size == 128) { |
| 5067 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5068 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5069 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5070 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5071 | } |
| 5072 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5073 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5076 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5077 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5078 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5079 | // Check whether VT is legal. |
| 5080 | unsigned NumElements = VT->getNumElements(); |
| 5081 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5082 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5083 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5084 | return true; |
| 5085 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5086 | } |
| 5087 | return false; |
| 5088 | } |
| 5089 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5090 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5091 | llvm::Type *eltTy, |
| 5092 | unsigned elts) const { |
| 5093 | if (!llvm::isPowerOf2_32(elts)) |
| 5094 | return false; |
| 5095 | if (totalSize.getQuantity() != 8 && |
| 5096 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5097 | return false; |
| 5098 | return true; |
| 5099 | } |
| 5100 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5101 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5102 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5103 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5104 | // but with the difference that any floating-point type is allowed, |
| 5105 | // including __fp16. |
| 5106 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5107 | if (BT->isFloatingPoint()) |
| 5108 | return true; |
| 5109 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5110 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5111 | if (VecSize == 64 || VecSize == 128) |
| 5112 | return true; |
| 5113 | } |
| 5114 | return false; |
| 5115 | } |
| 5116 | |
| 5117 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5118 | uint64_t Members) const { |
| 5119 | return Members <= 4; |
| 5120 | } |
| 5121 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5122 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5123 | QualType Ty, |
| 5124 | CodeGenFunction &CGF) const { |
| 5125 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5126 | bool IsIndirect = AI.isIndirect(); |
| 5127 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5128 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5129 | if (IsIndirect) |
| 5130 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5131 | else if (AI.getCoerceToType()) |
| 5132 | BaseTy = AI.getCoerceToType(); |
| 5133 | |
| 5134 | unsigned NumRegs = 1; |
| 5135 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5136 | BaseTy = ArrTy->getElementType(); |
| 5137 | NumRegs = ArrTy->getNumElements(); |
| 5138 | } |
| 5139 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5140 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5141 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5142 | // Standard, section B.4: |
| 5143 | // |
| 5144 | // struct { |
| 5145 | // void *__stack; |
| 5146 | // void *__gr_top; |
| 5147 | // void *__vr_top; |
| 5148 | // int __gr_offs; |
| 5149 | // int __vr_offs; |
| 5150 | // }; |
| 5151 | |
| 5152 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5153 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5154 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5155 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5156 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5157 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5158 | CharUnits TyAlign = TyInfo.second; |
| 5159 | |
| 5160 | Address reg_offs_p = Address::invalid(); |
| 5161 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5162 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5163 | CharUnits reg_top_offset; |
| 5164 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5165 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5166 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5167 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5168 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5169 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5170 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5171 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5172 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5173 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5174 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5175 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5176 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5177 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 5178 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5179 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5180 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5181 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5182 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | //======================================= |
| 5186 | // Find out where argument was passed |
| 5187 | //======================================= |
| 5188 | |
| 5189 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5190 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5191 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5192 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5193 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5194 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5195 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5196 | |
| 5197 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5198 | |
| 5199 | // 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] | 5200 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5201 | CGF.EmitBlock(MaybeRegBlock); |
| 5202 | |
| 5203 | // Integer arguments may need to correct register alignment (for example a |
| 5204 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5205 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5206 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5207 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5208 | |
| 5209 | reg_offs = CGF.Builder.CreateAdd( |
| 5210 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5211 | "align_regoffs"); |
| 5212 | reg_offs = CGF.Builder.CreateAnd( |
| 5213 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5214 | "aligned_regoffs"); |
| 5215 | } |
| 5216 | |
| 5217 | // 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] | 5218 | // The fact that this is done unconditionally reflects the fact that |
| 5219 | // allocating an argument to the stack also uses up all the remaining |
| 5220 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5221 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5222 | NewOffset = CGF.Builder.CreateAdd( |
| 5223 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5224 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5225 | |
| 5226 | // Now we're in a position to decide whether this argument really was in |
| 5227 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5228 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5229 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5230 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5231 | |
| 5232 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5233 | |
| 5234 | //======================================= |
| 5235 | // Argument was in registers |
| 5236 | //======================================= |
| 5237 | |
| 5238 | // Now we emit the code for if the argument was originally passed in |
| 5239 | // registers. First start the appropriate block: |
| 5240 | CGF.EmitBlock(InRegBlock); |
| 5241 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5242 | llvm::Value *reg_top = nullptr; |
| 5243 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 5244 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5245 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5246 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5247 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5248 | Address RegAddr = Address::invalid(); |
| 5249 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5250 | |
| 5251 | if (IsIndirect) { |
| 5252 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5253 | // stored registers or on the stack will actually be a struct **. |
| 5254 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5255 | } |
| 5256 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5257 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5258 | uint64_t NumMembers = 0; |
| 5259 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5260 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5261 | // Homogeneous aggregates passed in registers will have their elements split |
| 5262 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5263 | // qN+1, ...). We reload and store into a temporary local variable |
| 5264 | // contiguously. |
| 5265 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5266 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5267 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5268 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5269 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5270 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5271 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5272 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5273 | int Offset = 0; |
| 5274 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5275 | BaseTyInfo.first.getQuantity() < 16) |
| 5276 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5277 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5278 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5279 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5280 | Address LoadAddr = |
| 5281 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5282 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5283 | |
| 5284 | Address StoreAddr = |
| 5285 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5286 | |
| 5287 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5288 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5289 | } |
| 5290 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5291 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5292 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5293 | // Otherwise the object is contiguous in memory. |
| 5294 | |
| 5295 | // It might be right-aligned in its slot. |
| 5296 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5297 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5298 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5299 | TyInfo.first < SlotSize) { |
| 5300 | CharUnits Offset = SlotSize - TyInfo.first; |
| 5301 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5302 | } |
| 5303 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5304 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5305 | } |
| 5306 | |
| 5307 | CGF.EmitBranch(ContBlock); |
| 5308 | |
| 5309 | //======================================= |
| 5310 | // Argument was on the stack |
| 5311 | //======================================= |
| 5312 | CGF.EmitBlock(OnStackBlock); |
| 5313 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5314 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 5315 | CharUnits::Zero(), "stack_p"); |
| 5316 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5317 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5318 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5319 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5320 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5321 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5322 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5323 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5324 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5325 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5326 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5327 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5328 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5329 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5330 | "align_stack"); |
| 5331 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5332 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5333 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5334 | Address OnStackAddr(OnStackPtr, |
| 5335 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5336 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5337 | // All stack slots are multiples of 8 bytes. |
| 5338 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5339 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5340 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5341 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5342 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5343 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5344 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5345 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5346 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5347 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5348 | |
| 5349 | // Write the new value of __stack for the next call to va_arg |
| 5350 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5351 | |
| 5352 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5353 | TyInfo.first < StackSlotSize) { |
| 5354 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 5355 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5358 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5359 | |
| 5360 | CGF.EmitBranch(ContBlock); |
| 5361 | |
| 5362 | //======================================= |
| 5363 | // Tidy up |
| 5364 | //======================================= |
| 5365 | CGF.EmitBlock(ContBlock); |
| 5366 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5367 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5368 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5369 | |
| 5370 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5371 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 5372 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5373 | |
| 5374 | return ResAddr; |
| 5375 | } |
| 5376 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5377 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5378 | CodeGenFunction &CGF) const { |
| 5379 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5380 | // illegal vector types. Lower VAArg here for these cases and use |
| 5381 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5382 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5383 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5384 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5385 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5386 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5387 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5388 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5389 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5390 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5391 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5394 | // The size of the actual thing passed, which might end up just |
| 5395 | // being a pointer for indirect types. |
| 5396 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5397 | |
| 5398 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5399 | // aggregates should be passed indirectly. |
| 5400 | bool IsIndirect = false; |
| 5401 | if (TyInfo.first.getQuantity() > 16) { |
| 5402 | const Type *Base = nullptr; |
| 5403 | uint64_t Members = 0; |
| 5404 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5405 | } |
| 5406 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5407 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5408 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5409 | } |
| 5410 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5411 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5412 | QualType Ty) const { |
| 5413 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5414 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5415 | CharUnits::fromQuantity(8), |
| 5416 | /*allowHigherAlign*/ false); |
| 5417 | } |
| 5418 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5419 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5420 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5421 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5422 | |
| 5423 | namespace { |
| 5424 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5425 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5426 | public: |
| 5427 | enum ABIKind { |
| 5428 | APCS = 0, |
| 5429 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5430 | AAPCS_VFP = 2, |
| 5431 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5432 | }; |
| 5433 | |
| 5434 | private: |
| 5435 | ABIKind Kind; |
| 5436 | |
| 5437 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5438 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5439 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5440 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5441 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5442 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5443 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5444 | switch (getTarget().getTriple().getEnvironment()) { |
| 5445 | case llvm::Triple::Android: |
| 5446 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5447 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5448 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5449 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5450 | case llvm::Triple::MuslEABI: |
| 5451 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5452 | return true; |
| 5453 | default: |
| 5454 | return false; |
| 5455 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5456 | } |
| 5457 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5458 | bool isEABIHF() const { |
| 5459 | switch (getTarget().getTriple().getEnvironment()) { |
| 5460 | case llvm::Triple::EABIHF: |
| 5461 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5462 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5463 | return true; |
| 5464 | default: |
| 5465 | return false; |
| 5466 | } |
| 5467 | } |
| 5468 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5469 | ABIKind getABIKind() const { return Kind; } |
| 5470 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5471 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 5472 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5473 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5474 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5475 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5476 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5477 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5478 | uint64_t Members) const override; |
| 5479 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5480 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5481 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5482 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5483 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5484 | |
| 5485 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5486 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5487 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5488 | |
| 5489 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 5490 | ArrayRef<llvm::Type*> scalars, |
| 5491 | bool asReturnValue) const override { |
| 5492 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5493 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5494 | bool isSwiftErrorInRegister() const override { |
| 5495 | return true; |
| 5496 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5497 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5498 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5499 | }; |
| 5500 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5501 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5502 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5503 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5504 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5505 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5506 | const ARMABIInfo &getABIInfo() const { |
| 5507 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5508 | } |
| 5509 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5510 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5511 | return 13; |
| 5512 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5513 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5514 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5515 | return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5516 | } |
| 5517 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5518 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5519 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5520 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5521 | |
| 5522 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5523 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5524 | return false; |
| 5525 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5526 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5527 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5528 | if (getABIInfo().isEABI()) return 88; |
| 5529 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5530 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5531 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5532 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5533 | CodeGen::CodeGenModule &CGM, |
| 5534 | ForDefinition_t IsForDefinition) const override { |
| 5535 | if (!IsForDefinition) |
| 5536 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5537 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5538 | if (!FD) |
| 5539 | return; |
| 5540 | |
| 5541 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5542 | if (!Attr) |
| 5543 | return; |
| 5544 | |
| 5545 | const char *Kind; |
| 5546 | switch (Attr->getInterrupt()) { |
| 5547 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5548 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5549 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5550 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5551 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5552 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5553 | } |
| 5554 | |
| 5555 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5556 | |
| 5557 | Fn->addFnAttr("interrupt", Kind); |
| 5558 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5559 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5560 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5561 | return; |
| 5562 | |
| 5563 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5564 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5565 | // the backend to perform a realignment as part of the function prologue. |
| 5566 | llvm::AttrBuilder B; |
| 5567 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5568 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5569 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5570 | }; |
| 5571 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5572 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5573 | public: |
| 5574 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5575 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5576 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5577 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5578 | CodeGen::CodeGenModule &CGM, |
| 5579 | ForDefinition_t IsForDefinition) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5580 | |
| 5581 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5582 | llvm::SmallString<24> &Opt) const override { |
| 5583 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5584 | } |
| 5585 | |
| 5586 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5587 | llvm::SmallString<32> &Opt) const override { |
| 5588 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5589 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5590 | }; |
| 5591 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5592 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5593 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, |
| 5594 | ForDefinition_t IsForDefinition) const { |
| 5595 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition); |
| 5596 | if (!IsForDefinition) |
| 5597 | return; |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5598 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 5599 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5600 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5601 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5602 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5603 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5604 | FI.getReturnInfo() = |
| 5605 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5606 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5607 | for (auto &I : FI.arguments()) |
| 5608 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5609 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5610 | // Always honor user-specified calling convention. |
| 5611 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5612 | return; |
| 5613 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5614 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5615 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5616 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5617 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5618 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5619 | /// Return the default calling convention that LLVM will use. |
| 5620 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5621 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5622 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5623 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5624 | else if (isEABI()) |
| 5625 | return llvm::CallingConv::ARM_AAPCS; |
| 5626 | else |
| 5627 | return llvm::CallingConv::ARM_APCS; |
| 5628 | } |
| 5629 | |
| 5630 | /// Return the calling convention that our ABI would like us to use |
| 5631 | /// as the C calling convention. |
| 5632 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5633 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5634 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5635 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5636 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5637 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5638 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5639 | llvm_unreachable("bad ABI kind"); |
| 5640 | } |
| 5641 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5642 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5643 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5644 | |
| 5645 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5646 | // they'd just match what LLVM will infer from the triple. |
| 5647 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5648 | if (abiCC != getLLVMDefaultCC()) |
| 5649 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5650 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5651 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 5652 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 5653 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
Peter Smith | 32e2675 | 2017-07-27 10:43:53 +0000 | [diff] [blame] | 5654 | |
| 5655 | // The Run-time ABI for the ARM Architecture section 4.1.2 requires |
| 5656 | // AEABI-complying FP helper functions to use the base AAPCS. |
| 5657 | // These AEABI functions are expanded in the ARM llvm backend, all the builtin |
| 5658 | // support functions emitted by clang such as the _Complex helpers follow the |
| 5659 | // abiCC. |
| 5660 | if (abiCC != getLLVMDefaultCC()) |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5661 | BuiltinCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5662 | } |
| 5663 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5664 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5665 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5666 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5667 | // A single-precision floating-point type (including promoted |
| 5668 | // half-precision types); A double-precision floating-point type; |
| 5669 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5670 | // with a Base Type of a single- or double-precision floating-point type, |
| 5671 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5672 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5673 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5674 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5675 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5676 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5677 | // Handle illegal vector types here. |
| 5678 | if (isIllegalVectorType(Ty)) { |
| 5679 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5680 | if (Size <= 32) { |
| 5681 | llvm::Type *ResType = |
| 5682 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5683 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5684 | } |
| 5685 | if (Size == 64) { |
| 5686 | llvm::Type *ResType = llvm::VectorType::get( |
| 5687 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5688 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5689 | } |
| 5690 | if (Size == 128) { |
| 5691 | llvm::Type *ResType = llvm::VectorType::get( |
| 5692 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5693 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5694 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5695 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5696 | } |
| 5697 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5698 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 5699 | // unspecified. This is not done for OpenCL as it handles the half type |
| 5700 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5701 | if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5702 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5703 | llvm::Type::getFloatTy(getVMContext()) : |
| 5704 | llvm::Type::getInt32Ty(getVMContext()); |
| 5705 | return ABIArgInfo::getDirect(ResType); |
| 5706 | } |
| 5707 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5708 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5709 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5710 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5711 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5712 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5713 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5714 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5715 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5716 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5717 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5718 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5719 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5720 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5721 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5722 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5723 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5724 | return ABIArgInfo::getIgnore(); |
| 5725 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5726 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5727 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5728 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5729 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5730 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5731 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5732 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5733 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5734 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5735 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5736 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5737 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5738 | // this convention even for a variadic function: the backend will use GPRs |
| 5739 | // if needed. |
| 5740 | const Type *Base = nullptr; |
| 5741 | uint64_t Members = 0; |
| 5742 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5743 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5744 | llvm::Type *Ty = |
| 5745 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5746 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5747 | } |
| 5748 | } |
| 5749 | |
| 5750 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5751 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5752 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5753 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5754 | // and a pointer is passed. |
| 5755 | return ABIArgInfo::getIndirect( |
| 5756 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5759 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5760 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5761 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5762 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5763 | uint64_t ABIAlign = 4; |
| 5764 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5765 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5766 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5767 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5768 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5769 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5770 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5771 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5772 | /*ByVal=*/true, |
| 5773 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5774 | } |
| 5775 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5776 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5777 | // same size and alignment. |
| 5778 | if (getTarget().isRenderScriptTarget()) { |
| 5779 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5780 | } |
| 5781 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5782 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5783 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5784 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5785 | // FIXME: Try to match the types of the arguments more accurately where |
| 5786 | // we can. |
| 5787 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5788 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5789 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5790 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5791 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5792 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5793 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5794 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5795 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5798 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5799 | llvm::LLVMContext &VMContext) { |
| 5800 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5801 | // is called integer-like if its size is less than or equal to one word, and |
| 5802 | // the offset of each of its addressable sub-fields is zero. |
| 5803 | |
| 5804 | uint64_t Size = Context.getTypeSize(Ty); |
| 5805 | |
| 5806 | // Check that the type fits in a word. |
| 5807 | if (Size > 32) |
| 5808 | return false; |
| 5809 | |
| 5810 | // FIXME: Handle vector types! |
| 5811 | if (Ty->isVectorType()) |
| 5812 | return false; |
| 5813 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5814 | // Float types are never treated as "integer like". |
| 5815 | if (Ty->isRealFloatingType()) |
| 5816 | return false; |
| 5817 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5818 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5819 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5820 | return true; |
| 5821 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5822 | // Small complex integer types are "integer like". |
| 5823 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5824 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5825 | |
| 5826 | // Single element and zero sized arrays should be allowed, by the definition |
| 5827 | // above, but they are not. |
| 5828 | |
| 5829 | // Otherwise, it must be a record type. |
| 5830 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5831 | if (!RT) return false; |
| 5832 | |
| 5833 | // Ignore records with flexible arrays. |
| 5834 | const RecordDecl *RD = RT->getDecl(); |
| 5835 | if (RD->hasFlexibleArrayMember()) |
| 5836 | return false; |
| 5837 | |
| 5838 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5839 | // like". |
| 5840 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5841 | |
| 5842 | bool HadField = false; |
| 5843 | unsigned idx = 0; |
| 5844 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5845 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5846 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5847 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5848 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5849 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5850 | // struct { int : 0; int x } |
| 5851 | // is non-integer like according to gcc. |
| 5852 | if (FD->isBitField()) { |
| 5853 | if (!RD->isUnion()) |
| 5854 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5855 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5856 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5857 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5858 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5859 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5860 | } |
| 5861 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5862 | // Check if this field is at offset 0. |
| 5863 | if (Layout.getFieldOffset(idx) != 0) |
| 5864 | return false; |
| 5865 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5866 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5867 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5868 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5869 | // Only allow at most one field in a structure. This doesn't match the |
| 5870 | // wording above, but follows gcc in situations with a field following an |
| 5871 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5872 | if (!RD->isUnion()) { |
| 5873 | if (HadField) |
| 5874 | return false; |
| 5875 | |
| 5876 | HadField = true; |
| 5877 | } |
| 5878 | } |
| 5879 | |
| 5880 | return true; |
| 5881 | } |
| 5882 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5883 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5884 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5885 | bool IsEffectivelyAAPCS_VFP = |
| 5886 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5887 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5888 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5889 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5890 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5891 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5892 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5893 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5894 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5895 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5896 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5897 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5898 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 8e2e9d6 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5899 | if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5900 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5901 | llvm::Type::getFloatTy(getVMContext()) : |
| 5902 | llvm::Type::getInt32Ty(getVMContext()); |
| 5903 | return ABIArgInfo::getDirect(ResType); |
| 5904 | } |
| 5905 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5906 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5907 | // Treat an enum type as its underlying type. |
| 5908 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5909 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5910 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5911 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5912 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5913 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5914 | |
| 5915 | // Are we following APCS? |
| 5916 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5917 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5918 | return ABIArgInfo::getIgnore(); |
| 5919 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5920 | // Complex types are all returned as packed integers. |
| 5921 | // |
| 5922 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5923 | // correctly. |
| 5924 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5925 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5926 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5927 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5928 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5929 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5930 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5931 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5932 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5933 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5934 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5935 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5936 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5937 | } |
| 5938 | |
| 5939 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5940 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5941 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5942 | |
| 5943 | // Otherwise this is an AAPCS variant. |
| 5944 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5945 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5946 | return ABIArgInfo::getIgnore(); |
| 5947 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5948 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5949 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5950 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5951 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5952 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5953 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5954 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5955 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5956 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5957 | } |
| 5958 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5959 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5960 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5961 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5962 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5963 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 5964 | // same size and alignment. |
| 5965 | if (getTarget().isRenderScriptTarget()) { |
| 5966 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5967 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5968 | if (getDataLayout().isBigEndian()) |
| 5969 | // 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] | 5970 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5971 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5972 | // Return in the smallest viable integer type. |
| 5973 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5974 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5975 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5976 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5977 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5978 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5979 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5980 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5981 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5982 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5985 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5986 | } |
| 5987 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5988 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5989 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 5990 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 5991 | if (isAndroid()) { |
| 5992 | // Android shipped using Clang 3.1, which supported a slightly different |
| 5993 | // vector ABI. The primary differences were that 3-element vector types |
| 5994 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 5995 | // accepts that legacy behavior for Android only. |
| 5996 | // Check whether VT is legal. |
| 5997 | unsigned NumElements = VT->getNumElements(); |
| 5998 | // NumElements should be power of 2 or equal to 3. |
| 5999 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 6000 | return true; |
| 6001 | } else { |
| 6002 | // Check whether VT is legal. |
| 6003 | unsigned NumElements = VT->getNumElements(); |
| 6004 | uint64_t Size = getContext().getTypeSize(VT); |
| 6005 | // NumElements should be power of 2. |
| 6006 | if (!llvm::isPowerOf2_32(NumElements)) |
| 6007 | return true; |
| 6008 | // Size should be greater than 32 bits. |
| 6009 | return Size <= 32; |
| 6010 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6011 | } |
| 6012 | return false; |
| 6013 | } |
| 6014 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 6015 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 6016 | llvm::Type *eltTy, |
| 6017 | unsigned numElts) const { |
| 6018 | if (!llvm::isPowerOf2_32(numElts)) |
| 6019 | return false; |
| 6020 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 6021 | if (size > 64) |
| 6022 | return false; |
| 6023 | if (vectorSize.getQuantity() != 8 && |
| 6024 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 6025 | return false; |
| 6026 | return true; |
| 6027 | } |
| 6028 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 6029 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 6030 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 6031 | // double, or 64-bit or 128-bit vectors. |
| 6032 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 6033 | if (BT->getKind() == BuiltinType::Float || |
| 6034 | BT->getKind() == BuiltinType::Double || |
| 6035 | BT->getKind() == BuiltinType::LongDouble) |
| 6036 | return true; |
| 6037 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 6038 | unsigned VecSize = getContext().getTypeSize(VT); |
| 6039 | if (VecSize == 64 || VecSize == 128) |
| 6040 | return true; |
| 6041 | } |
| 6042 | return false; |
| 6043 | } |
| 6044 | |
| 6045 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 6046 | uint64_t Members) const { |
| 6047 | return Members <= 4; |
| 6048 | } |
| 6049 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6050 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6051 | QualType Ty) const { |
| 6052 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6053 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6054 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6055 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6056 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 6057 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 6058 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6061 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 6062 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6063 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6064 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6065 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6066 | const Type *Base = nullptr; |
| 6067 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6068 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 6069 | IsIndirect = true; |
| 6070 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6071 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6072 | // allocated by the caller. |
| 6073 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 6074 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6075 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6076 | IsIndirect = true; |
| 6077 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6078 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6079 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6080 | // 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] | 6081 | // Our callers should be prepared to handle an under-aligned address. |
| 6082 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6083 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6084 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6085 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6086 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6087 | // ARMv7k allows type alignment up to 16 bytes. |
| 6088 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6089 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6090 | } else { |
| 6091 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6092 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6093 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6094 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6095 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6096 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6097 | } |
| 6098 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6099 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6100 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6101 | //===----------------------------------------------------------------------===// |
| 6102 | |
| 6103 | namespace { |
| 6104 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6105 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6106 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6107 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6108 | |
| 6109 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6110 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6111 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6112 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6113 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6114 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6115 | }; |
| 6116 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6117 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6118 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6119 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6120 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6121 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6122 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6123 | CodeGen::CodeGenModule &M, |
| 6124 | ForDefinition_t IsForDefinition) const override; |
| 6125 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6126 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6127 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6128 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6129 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6130 | }; |
| 6131 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6132 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6133 | if (RetTy->isVoidType()) |
| 6134 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6135 | |
| 6136 | // note: this is different from default ABI |
| 6137 | if (!RetTy->isScalarType()) |
| 6138 | return ABIArgInfo::getDirect(); |
| 6139 | |
| 6140 | // Treat an enum type as its underlying type. |
| 6141 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6142 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6143 | |
| 6144 | return (RetTy->isPromotableIntegerType() ? |
| 6145 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6146 | } |
| 6147 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6148 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6149 | // Treat an enum type as its underlying type. |
| 6150 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6151 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6152 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6153 | // Return aggregates type as indirect by value |
| 6154 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6155 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6156 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6157 | return (Ty->isPromotableIntegerType() ? |
| 6158 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6159 | } |
| 6160 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6161 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6162 | if (!getCXXABI().classifyReturnType(FI)) |
| 6163 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6164 | for (auto &I : FI.arguments()) |
| 6165 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6166 | |
| 6167 | // Always honor user-specified calling convention. |
| 6168 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6169 | return; |
| 6170 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6171 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6172 | } |
| 6173 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6174 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6175 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6176 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6177 | } |
| 6178 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6179 | void NVPTXTargetCodeGenInfo::setTargetAttributes( |
| 6180 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 6181 | ForDefinition_t IsForDefinition) const { |
| 6182 | if (!IsForDefinition) |
| 6183 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6184 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6185 | if (!FD) return; |
| 6186 | |
| 6187 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6188 | |
| 6189 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6190 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6191 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6192 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6193 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6194 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6195 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6196 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6197 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6198 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6199 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6200 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6201 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6202 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6203 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6204 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6205 | // __global__ functions cannot be called from the device, we do not |
| 6206 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6207 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6208 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6209 | addNVVMMetadata(F, "kernel", 1); |
| 6210 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6211 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6212 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6213 | llvm::APSInt MaxThreads(32); |
| 6214 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6215 | if (MaxThreads > 0) |
| 6216 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6217 | |
| 6218 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6219 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6220 | // we don't have to add a PTX directive. |
| 6221 | if (Attr->getMinBlocks()) { |
| 6222 | llvm::APSInt MinBlocks(32); |
| 6223 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6224 | if (MinBlocks > 0) |
| 6225 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6226 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6227 | } |
| 6228 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6229 | } |
| 6230 | } |
| 6231 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6232 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6233 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6234 | llvm::Module *M = F->getParent(); |
| 6235 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6236 | |
| 6237 | // Get "nvvm.annotations" metadata node |
| 6238 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6239 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6240 | llvm::Metadata *MDVals[] = { |
| 6241 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6242 | llvm::ConstantAsMetadata::get( |
| 6243 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6244 | // Append metadata to nvvm.annotations |
| 6245 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6246 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6247 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6248 | |
| 6249 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6250 | // SystemZ ABI Implementation |
| 6251 | //===----------------------------------------------------------------------===// |
| 6252 | |
| 6253 | namespace { |
| 6254 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6255 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6256 | bool HasVector; |
| 6257 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6258 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6259 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6260 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6261 | |
| 6262 | bool isPromotableIntegerType(QualType Ty) const; |
| 6263 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6264 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6265 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6266 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6267 | |
| 6268 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6269 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6270 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6271 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6272 | if (!getCXXABI().classifyReturnType(FI)) |
| 6273 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6274 | for (auto &I : FI.arguments()) |
| 6275 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6276 | } |
| 6277 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6278 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6279 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6280 | |
| 6281 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 6282 | ArrayRef<llvm::Type*> scalars, |
| 6283 | bool asReturnValue) const override { |
| 6284 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6285 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6286 | bool isSwiftErrorInRegister() const override { |
| 6287 | return true; |
| 6288 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6289 | }; |
| 6290 | |
| 6291 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6292 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6293 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6294 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6295 | }; |
| 6296 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6297 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6298 | |
| 6299 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6300 | // Treat an enum type as its underlying type. |
| 6301 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6302 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6303 | |
| 6304 | // Promotable integer types are required to be promoted by the ABI. |
| 6305 | if (Ty->isPromotableIntegerType()) |
| 6306 | return true; |
| 6307 | |
| 6308 | // 32-bit values must also be promoted. |
| 6309 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6310 | switch (BT->getKind()) { |
| 6311 | case BuiltinType::Int: |
| 6312 | case BuiltinType::UInt: |
| 6313 | return true; |
| 6314 | default: |
| 6315 | return false; |
| 6316 | } |
| 6317 | return false; |
| 6318 | } |
| 6319 | |
| 6320 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6321 | return (Ty->isAnyComplexType() || |
| 6322 | Ty->isVectorType() || |
| 6323 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6324 | } |
| 6325 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6326 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6327 | return (HasVector && |
| 6328 | Ty->isVectorType() && |
| 6329 | getContext().getTypeSize(Ty) <= 128); |
| 6330 | } |
| 6331 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6332 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6333 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6334 | switch (BT->getKind()) { |
| 6335 | case BuiltinType::Float: |
| 6336 | case BuiltinType::Double: |
| 6337 | return true; |
| 6338 | default: |
| 6339 | return false; |
| 6340 | } |
| 6341 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6342 | return false; |
| 6343 | } |
| 6344 | |
| 6345 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6346 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6347 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6348 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6349 | |
| 6350 | // If this is a C++ record, check the bases first. |
| 6351 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6352 | for (const auto &I : CXXRD->bases()) { |
| 6353 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6354 | |
| 6355 | // Empty bases don't affect things either way. |
| 6356 | if (isEmptyRecord(getContext(), Base, true)) |
| 6357 | continue; |
| 6358 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6359 | if (!Found.isNull()) |
| 6360 | return Ty; |
| 6361 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6362 | } |
| 6363 | |
| 6364 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6365 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6366 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6367 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6368 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6369 | if (getContext().getLangOpts().CPlusPlus && |
| 6370 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 6371 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6372 | |
| 6373 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6374 | // Nested structures still do though. |
| 6375 | if (!Found.isNull()) |
| 6376 | return Ty; |
| 6377 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6378 | } |
| 6379 | |
| 6380 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6381 | // 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] | 6382 | if (!Found.isNull()) |
| 6383 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6384 | } |
| 6385 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6386 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6387 | } |
| 6388 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6389 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6390 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6391 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6392 | // struct { |
| 6393 | // i64 __gpr; |
| 6394 | // i64 __fpr; |
| 6395 | // i8 *__overflow_arg_area; |
| 6396 | // i8 *__reg_save_area; |
| 6397 | // }; |
| 6398 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6399 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6400 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6401 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6402 | Ty = getContext().getCanonicalType(Ty); |
| 6403 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6404 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6405 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6406 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6407 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6408 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6409 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6410 | CharUnits UnpaddedSize; |
| 6411 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6412 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6413 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6414 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6415 | } else { |
| 6416 | if (AI.getCoerceToType()) |
| 6417 | ArgTy = AI.getCoerceToType(); |
| 6418 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6419 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6420 | UnpaddedSize = TyInfo.first; |
| 6421 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6422 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6423 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6424 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6425 | PaddedSize = CharUnits::fromQuantity(16); |
| 6426 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6427 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6428 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6429 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6430 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6431 | llvm::Value *PaddedSizeV = |
| 6432 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6433 | |
| 6434 | if (IsVector) { |
| 6435 | // Work out the address of a vector argument on the stack. |
| 6436 | // Vector arguments are always passed in the high bits of a |
| 6437 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6438 | Address OverflowArgAreaPtr = |
| 6439 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6440 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6441 | Address OverflowArgArea = |
| 6442 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6443 | TyInfo.second); |
| 6444 | Address MemAddr = |
| 6445 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6446 | |
| 6447 | // Update overflow_arg_area_ptr pointer |
| 6448 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6449 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6450 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6451 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6452 | |
| 6453 | return MemAddr; |
| 6454 | } |
| 6455 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6456 | assert(PaddedSize.getQuantity() == 8); |
| 6457 | |
| 6458 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6459 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6460 | if (InFPRs) { |
| 6461 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6462 | RegCountField = 1; // __fpr |
| 6463 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6464 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6465 | } else { |
| 6466 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6467 | RegCountField = 0; // __gpr |
| 6468 | RegSaveIndex = 2; // save offset for r2 |
| 6469 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6470 | } |
| 6471 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6472 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 6473 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 6474 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6475 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6476 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6477 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6478 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6479 | |
| 6480 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6481 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6482 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6483 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6484 | |
| 6485 | // Emit code to load the value if it was passed in registers. |
| 6486 | CGF.EmitBlock(InRegBlock); |
| 6487 | |
| 6488 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6489 | llvm::Value *ScaledRegCount = |
| 6490 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6491 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6492 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6493 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6494 | llvm::Value *RegOffset = |
| 6495 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6496 | Address RegSaveAreaPtr = |
| 6497 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 6498 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6499 | llvm::Value *RegSaveArea = |
| 6500 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6501 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6502 | "raw_reg_addr"), |
| 6503 | PaddedSize); |
| 6504 | Address RegAddr = |
| 6505 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6506 | |
| 6507 | // Update the register count |
| 6508 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6509 | llvm::Value *NewRegCount = |
| 6510 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6511 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6512 | CGF.EmitBranch(ContBlock); |
| 6513 | |
| 6514 | // Emit code to load the value if it was passed in memory. |
| 6515 | CGF.EmitBlock(InMemBlock); |
| 6516 | |
| 6517 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6518 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 6519 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 6520 | Address OverflowArgArea = |
| 6521 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6522 | PaddedSize); |
| 6523 | Address RawMemAddr = |
| 6524 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6525 | Address MemAddr = |
| 6526 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6527 | |
| 6528 | // Update overflow_arg_area_ptr pointer |
| 6529 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6530 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6531 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6532 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6533 | CGF.EmitBranch(ContBlock); |
| 6534 | |
| 6535 | // Return the appropriate result. |
| 6536 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6537 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6538 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6539 | |
| 6540 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6541 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6542 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6543 | |
| 6544 | return ResAddr; |
| 6545 | } |
| 6546 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6547 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6548 | if (RetTy->isVoidType()) |
| 6549 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6550 | if (isVectorArgumentType(RetTy)) |
| 6551 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6552 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6553 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6554 | return (isPromotableIntegerType(RetTy) ? |
| 6555 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6556 | } |
| 6557 | |
| 6558 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6559 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6560 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6561 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6562 | |
| 6563 | // Integers and enums are extended to full register width. |
| 6564 | if (isPromotableIntegerType(Ty)) |
| 6565 | return ABIArgInfo::getExtend(); |
| 6566 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6567 | // Handle vector types and vector-like structure types. Note that |
| 6568 | // as opposed to float-like structure types, we do not allow any |
| 6569 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6570 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6571 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6572 | if (isVectorArgumentType(SingleElementTy) && |
| 6573 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6574 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6575 | |
| 6576 | // 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] | 6577 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6578 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6579 | |
| 6580 | // Handle small structures. |
| 6581 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6582 | // Structures with flexible arrays have variable length, so really |
| 6583 | // fail the size test above. |
| 6584 | const RecordDecl *RD = RT->getDecl(); |
| 6585 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6586 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6587 | |
| 6588 | // The structure is passed as an unextended integer, a float, or a double. |
| 6589 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6590 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6591 | assert(Size == 32 || Size == 64); |
| 6592 | if (Size == 32) |
| 6593 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6594 | else |
| 6595 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6596 | } else |
| 6597 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6598 | return ABIArgInfo::getDirect(PassTy); |
| 6599 | } |
| 6600 | |
| 6601 | // Non-structure compounds are passed indirectly. |
| 6602 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6603 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6604 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6605 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6606 | } |
| 6607 | |
| 6608 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6609 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6610 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6611 | |
| 6612 | namespace { |
| 6613 | |
| 6614 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6615 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6616 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6617 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6618 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6619 | CodeGen::CodeGenModule &M, |
| 6620 | ForDefinition_t IsForDefinition) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6621 | }; |
| 6622 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6623 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6624 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6625 | void MSP430TargetCodeGenInfo::setTargetAttributes( |
| 6626 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 6627 | ForDefinition_t IsForDefinition) const { |
| 6628 | if (!IsForDefinition) |
| 6629 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6630 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6631 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 6632 | // Handle 'interrupt' attribute: |
| 6633 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6634 | |
| 6635 | // Step 1: Set ISR calling convention. |
| 6636 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 6637 | |
| 6638 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6639 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6640 | |
| 6641 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 6642 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 6643 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 6644 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6645 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6646 | } |
| 6647 | } |
| 6648 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6649 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6650 | // MIPS ABI Implementation. This works for both little-endian and |
| 6651 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6652 | //===----------------------------------------------------------------------===// |
| 6653 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6654 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6655 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6656 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6657 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6658 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6659 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6660 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6661 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6662 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6663 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6664 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6665 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6666 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6667 | |
| 6668 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6669 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6670 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6671 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6672 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6673 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6674 | }; |
| 6675 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6676 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6677 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6678 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6679 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6680 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6681 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6682 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6683 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6684 | return 29; |
| 6685 | } |
| 6686 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6687 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6688 | CodeGen::CodeGenModule &CGM, |
| 6689 | ForDefinition_t IsForDefinition) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6690 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6691 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6692 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6693 | |
| 6694 | if (FD->hasAttr<MipsLongCallAttr>()) |
| 6695 | Fn->addFnAttr("long-call"); |
| 6696 | else if (FD->hasAttr<MipsShortCallAttr>()) |
| 6697 | Fn->addFnAttr("short-call"); |
| 6698 | |
| 6699 | // Other attributes do not have a meaning for declarations. |
| 6700 | if (!IsForDefinition) |
| 6701 | return; |
| 6702 | |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6703 | if (FD->hasAttr<Mips16Attr>()) { |
| 6704 | Fn->addFnAttr("mips16"); |
| 6705 | } |
| 6706 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6707 | Fn->addFnAttr("nomips16"); |
| 6708 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6709 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6710 | if (FD->hasAttr<MicroMipsAttr>()) |
| 6711 | Fn->addFnAttr("micromips"); |
| 6712 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 6713 | Fn->addFnAttr("nomicromips"); |
| 6714 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6715 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6716 | if (!Attr) |
| 6717 | return; |
| 6718 | |
| 6719 | const char *Kind; |
| 6720 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6721 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6722 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6723 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6724 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6725 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6726 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6727 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6728 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6729 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6730 | } |
| 6731 | |
| 6732 | Fn->addFnAttr("interrupt", Kind); |
| 6733 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6734 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6735 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6736 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6737 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6738 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6739 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6740 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6741 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6742 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6743 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6744 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6745 | void MipsABIInfo::CoerceToIntArgs( |
| 6746 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6747 | llvm::IntegerType *IntTy = |
| 6748 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6749 | |
| 6750 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6751 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6752 | ArgList.push_back(IntTy); |
| 6753 | |
| 6754 | // If necessary, add one more integer type to ArgList. |
| 6755 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6756 | |
| 6757 | if (R) |
| 6758 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6759 | } |
| 6760 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6761 | // In N32/64, an aligned double precision floating point field is passed in |
| 6762 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6763 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6764 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6765 | |
| 6766 | if (IsO32) { |
| 6767 | CoerceToIntArgs(TySize, ArgList); |
| 6768 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6769 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6770 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6771 | if (Ty->isComplexType()) |
| 6772 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6773 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6774 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6775 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6776 | // Unions/vectors are passed in integer registers. |
| 6777 | if (!RT || !RT->isStructureOrClassType()) { |
| 6778 | CoerceToIntArgs(TySize, ArgList); |
| 6779 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6780 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6781 | |
| 6782 | const RecordDecl *RD = RT->getDecl(); |
| 6783 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6784 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6785 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6786 | uint64_t LastOffset = 0; |
| 6787 | unsigned idx = 0; |
| 6788 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6789 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6790 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6791 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6792 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6793 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6794 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6795 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6796 | |
| 6797 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6798 | continue; |
| 6799 | |
| 6800 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6801 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6802 | continue; |
| 6803 | |
| 6804 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6805 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6806 | ArgList.push_back(I64); |
| 6807 | |
| 6808 | // Add double type. |
| 6809 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6810 | LastOffset = Offset + 64; |
| 6811 | } |
| 6812 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6813 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6814 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6815 | |
| 6816 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6817 | } |
| 6818 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6819 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6820 | uint64_t Offset) const { |
| 6821 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6822 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6823 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6824 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6825 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6826 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6827 | ABIArgInfo |
| 6828 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6829 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6830 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6831 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6832 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6833 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6834 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6835 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6836 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6837 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6838 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6839 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6840 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6841 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6842 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6843 | return ABIArgInfo::getIgnore(); |
| 6844 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6845 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6846 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6847 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6848 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6849 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6850 | // If we have reached here, aggregates are passed directly by coercing to |
| 6851 | // another structure type. Padding is inserted if the offset of the |
| 6852 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6853 | ABIArgInfo ArgInfo = |
| 6854 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6855 | getPaddingType(OrigOffset, CurrOffset)); |
| 6856 | ArgInfo.setInReg(true); |
| 6857 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6858 | } |
| 6859 | |
| 6860 | // Treat an enum type as its underlying type. |
| 6861 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6862 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6863 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6864 | // All integral types are promoted to the GPR width. |
| 6865 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6866 | return ABIArgInfo::getExtend(); |
| 6867 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6868 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6869 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6870 | } |
| 6871 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6872 | llvm::Type* |
| 6873 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6874 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6875 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6876 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6877 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6878 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6879 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6880 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6881 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6882 | // N32/64 returns struct/classes in floating point registers if the |
| 6883 | // following conditions are met: |
| 6884 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6885 | // 2. The struct/class has one or two fields all of which are floating |
| 6886 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6887 | // 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] | 6888 | // |
| 6889 | // Any other composite results are returned in integer registers. |
| 6890 | // |
| 6891 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6892 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6893 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6894 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6895 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6896 | if (!BT || !BT->isFloatingPoint()) |
| 6897 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6898 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6899 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6900 | } |
| 6901 | |
| 6902 | if (b == e) |
| 6903 | return llvm::StructType::get(getVMContext(), RTList, |
| 6904 | RD->hasAttr<PackedAttr>()); |
| 6905 | |
| 6906 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6907 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6908 | } |
| 6909 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6910 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6911 | return llvm::StructType::get(getVMContext(), RTList); |
| 6912 | } |
| 6913 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6914 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6915 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6916 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6917 | if (RetTy->isVoidType()) |
| 6918 | return ABIArgInfo::getIgnore(); |
| 6919 | |
| 6920 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6921 | // However, N32/N64 ignores zero sized return values. |
| 6922 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6923 | return ABIArgInfo::getIgnore(); |
| 6924 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6925 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6926 | if (Size <= 128) { |
| 6927 | if (RetTy->isAnyComplexType()) |
| 6928 | return ABIArgInfo::getDirect(); |
| 6929 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6930 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6931 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6932 | if (!IsO32 || |
| 6933 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6934 | ABIArgInfo ArgInfo = |
| 6935 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6936 | ArgInfo.setInReg(true); |
| 6937 | return ArgInfo; |
| 6938 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6939 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6940 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6941 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6942 | } |
| 6943 | |
| 6944 | // Treat an enum type as its underlying type. |
| 6945 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6946 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6947 | |
| 6948 | return (RetTy->isPromotableIntegerType() ? |
| 6949 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6950 | } |
| 6951 | |
| 6952 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6953 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6954 | if (!getCXXABI().classifyReturnType(FI)) |
| 6955 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6956 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6957 | // 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] | 6958 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6959 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6960 | for (auto &I : FI.arguments()) |
| 6961 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6962 | } |
| 6963 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6964 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6965 | QualType OrigTy) const { |
| 6966 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6967 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6968 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6969 | // 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] | 6970 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6971 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6972 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6973 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6974 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6975 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6976 | DidPromote = true; |
| 6977 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6978 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6979 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6980 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6981 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6982 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6983 | // The alignment of things in the argument area is never larger than |
| 6984 | // StackAlignInBytes. |
| 6985 | TyInfo.second = |
| 6986 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6987 | |
| 6988 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6989 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6990 | |
| 6991 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6992 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6993 | |
| 6994 | |
| 6995 | // If there was a promotion, "unpromote" into a temporary. |
| 6996 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6997 | if (DidPromote) { |
| 6998 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6999 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 7000 | |
| 7001 | // Truncate down to the right width. |
| 7002 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 7003 | : CGF.IntPtrTy); |
| 7004 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 7005 | if (OrigTy->isPointerType()) |
| 7006 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 7007 | |
| 7008 | CGF.Builder.CreateStore(V, Temp); |
| 7009 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7010 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7011 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7012 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7013 | } |
| 7014 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7015 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 7016 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7017 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7018 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 7019 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 7020 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7021 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7022 | return false; |
| 7023 | } |
| 7024 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7025 | bool |
| 7026 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7027 | llvm::Value *Address) const { |
| 7028 | // This information comes from gcc's implementation, which seems to |
| 7029 | // as canonical as it gets. |
| 7030 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7031 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 7032 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7033 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7034 | |
| 7035 | // 0-31 are the general purpose registers, $0 - $31. |
| 7036 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 7037 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 7038 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7039 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7040 | |
| 7041 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 7042 | // They are one bit wide and ignored here. |
| 7043 | |
| 7044 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 7045 | // (coprocessor 1 is the FP unit) |
| 7046 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 7047 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 7048 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7049 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7050 | return false; |
| 7051 | } |
| 7052 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7053 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7054 | // AVR ABI Implementation. |
| 7055 | //===----------------------------------------------------------------------===// |
| 7056 | |
| 7057 | namespace { |
| 7058 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7059 | public: |
| 7060 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7061 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 7062 | |
| 7063 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7064 | CodeGen::CodeGenModule &CGM, |
| 7065 | ForDefinition_t IsForDefinition) const override { |
| 7066 | if (!IsForDefinition) |
| 7067 | return; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7068 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 7069 | if (!FD) return; |
| 7070 | auto *Fn = cast<llvm::Function>(GV); |
| 7071 | |
| 7072 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7073 | Fn->addFnAttr("interrupt"); |
| 7074 | |
| 7075 | if (FD->getAttr<AVRSignalAttr>()) |
| 7076 | Fn->addFnAttr("signal"); |
| 7077 | } |
| 7078 | }; |
| 7079 | } |
| 7080 | |
| 7081 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7082 | // 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] | 7083 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7084 | // handling. |
| 7085 | //===----------------------------------------------------------------------===// |
| 7086 | |
| 7087 | namespace { |
| 7088 | |
| 7089 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7090 | public: |
| 7091 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7092 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7093 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7094 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7095 | CodeGen::CodeGenModule &M, |
| 7096 | ForDefinition_t IsForDefinition) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7097 | }; |
| 7098 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7099 | void TCETargetCodeGenInfo::setTargetAttributes( |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7100 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 7101 | ForDefinition_t IsForDefinition) const { |
| 7102 | if (!IsForDefinition) |
| 7103 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7104 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7105 | if (!FD) return; |
| 7106 | |
| 7107 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7108 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7109 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7110 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7111 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7112 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7113 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7114 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7115 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7116 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7117 | llvm::NamedMDNode *OpenCLMetadata = |
| 7118 | M.getModule().getOrInsertNamedMetadata( |
| 7119 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7120 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7121 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7122 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7123 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7124 | Operands.push_back( |
| 7125 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7126 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7127 | Operands.push_back( |
| 7128 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7129 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7130 | Operands.push_back( |
| 7131 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7132 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7133 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7134 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7135 | // (false) for implementing the work_group_size_hint attr later. |
| 7136 | // 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] | 7137 | Operands.push_back( |
| 7138 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7139 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7140 | } |
| 7141 | } |
| 7142 | } |
| 7143 | } |
| 7144 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7145 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7146 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7147 | //===----------------------------------------------------------------------===// |
| 7148 | // Hexagon ABI Implementation |
| 7149 | //===----------------------------------------------------------------------===// |
| 7150 | |
| 7151 | namespace { |
| 7152 | |
| 7153 | class HexagonABIInfo : public ABIInfo { |
| 7154 | |
| 7155 | |
| 7156 | public: |
| 7157 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7158 | |
| 7159 | private: |
| 7160 | |
| 7161 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7162 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7163 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7164 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7165 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7166 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7167 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7168 | }; |
| 7169 | |
| 7170 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7171 | public: |
| 7172 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7173 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7174 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7175 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7176 | return 29; |
| 7177 | } |
| 7178 | }; |
| 7179 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7180 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7181 | |
| 7182 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7183 | if (!getCXXABI().classifyReturnType(FI)) |
| 7184 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7185 | for (auto &I : FI.arguments()) |
| 7186 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7187 | } |
| 7188 | |
| 7189 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7190 | if (!isAggregateTypeForABI(Ty)) { |
| 7191 | // Treat an enum type as its underlying type. |
| 7192 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7193 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7194 | |
| 7195 | return (Ty->isPromotableIntegerType() ? |
| 7196 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 7197 | } |
| 7198 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7199 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7200 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7201 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7202 | // Ignore empty records. |
| 7203 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7204 | return ABIArgInfo::getIgnore(); |
| 7205 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7206 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7207 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7208 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7209 | // Pass in the smallest viable integer type. |
| 7210 | else if (Size > 32) |
| 7211 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7212 | else if (Size > 16) |
| 7213 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7214 | else if (Size > 8) |
| 7215 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7216 | else |
| 7217 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7218 | } |
| 7219 | |
| 7220 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7221 | if (RetTy->isVoidType()) |
| 7222 | return ABIArgInfo::getIgnore(); |
| 7223 | |
| 7224 | // Large vector types should be returned via memory. |
| 7225 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7226 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7227 | |
| 7228 | if (!isAggregateTypeForABI(RetTy)) { |
| 7229 | // Treat an enum type as its underlying type. |
| 7230 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7231 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7232 | |
| 7233 | return (RetTy->isPromotableIntegerType() ? |
| 7234 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 7235 | } |
| 7236 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7237 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7238 | return ABIArgInfo::getIgnore(); |
| 7239 | |
| 7240 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7241 | // are returned indirectly. |
| 7242 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7243 | if (Size <= 64) { |
| 7244 | // Return in the smallest viable integer type. |
| 7245 | if (Size <= 8) |
| 7246 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7247 | if (Size <= 16) |
| 7248 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7249 | if (Size <= 32) |
| 7250 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7251 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7252 | } |
| 7253 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7254 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7255 | } |
| 7256 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7257 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7258 | QualType Ty) const { |
| 7259 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7260 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7261 | getContext().getTypeInfoInChars(Ty), |
| 7262 | CharUnits::fromQuantity(4), |
| 7263 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7264 | } |
| 7265 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7266 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7267 | // Lanai ABI Implementation |
| 7268 | //===----------------------------------------------------------------------===// |
| 7269 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7270 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7271 | class LanaiABIInfo : public DefaultABIInfo { |
| 7272 | public: |
| 7273 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7274 | |
| 7275 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7276 | |
| 7277 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7278 | CCState State(FI.getCallingConvention()); |
| 7279 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7280 | // regparm attribute set. |
| 7281 | if (FI.getHasRegParm()) { |
| 7282 | State.FreeRegs = FI.getRegParm(); |
| 7283 | } else { |
| 7284 | State.FreeRegs = 4; |
| 7285 | } |
| 7286 | |
| 7287 | if (!getCXXABI().classifyReturnType(FI)) |
| 7288 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7289 | for (auto &I : FI.arguments()) |
| 7290 | I.info = classifyArgumentType(I.type, State); |
| 7291 | } |
| 7292 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7293 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7294 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7295 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7296 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7297 | |
| 7298 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7299 | unsigned Size = getContext().getTypeSize(Ty); |
| 7300 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7301 | |
| 7302 | if (SizeInRegs == 0) |
| 7303 | return false; |
| 7304 | |
| 7305 | if (SizeInRegs > State.FreeRegs) { |
| 7306 | State.FreeRegs = 0; |
| 7307 | return false; |
| 7308 | } |
| 7309 | |
| 7310 | State.FreeRegs -= SizeInRegs; |
| 7311 | |
| 7312 | return true; |
| 7313 | } |
| 7314 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7315 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7316 | CCState &State) const { |
| 7317 | if (!ByVal) { |
| 7318 | if (State.FreeRegs) { |
| 7319 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7320 | return getNaturalAlignIndirectInReg(Ty); |
| 7321 | } |
| 7322 | return getNaturalAlignIndirect(Ty, false); |
| 7323 | } |
| 7324 | |
| 7325 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7326 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7327 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7328 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7329 | /*Realign=*/TypeAlign > |
| 7330 | MinABIStackAlignInBytes); |
| 7331 | } |
| 7332 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7333 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7334 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7335 | // Check with the C++ ABI first. |
| 7336 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7337 | if (RT) { |
| 7338 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7339 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7340 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7341 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7342 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7343 | } |
| 7344 | } |
| 7345 | |
| 7346 | if (isAggregateTypeForABI(Ty)) { |
| 7347 | // Structures with flexible arrays are always indirect. |
| 7348 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7349 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7350 | |
| 7351 | // Ignore empty structs/unions. |
| 7352 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7353 | return ABIArgInfo::getIgnore(); |
| 7354 | |
| 7355 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7356 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7357 | if (SizeInRegs <= State.FreeRegs) { |
| 7358 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7359 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7360 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7361 | State.FreeRegs -= SizeInRegs; |
| 7362 | return ABIArgInfo::getDirectInReg(Result); |
| 7363 | } else { |
| 7364 | State.FreeRegs = 0; |
| 7365 | } |
| 7366 | return getIndirectResult(Ty, true, State); |
| 7367 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7368 | |
| 7369 | // Treat an enum type as its underlying type. |
| 7370 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7371 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7372 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7373 | bool InReg = shouldUseInReg(Ty, State); |
| 7374 | if (Ty->isPromotableIntegerType()) { |
| 7375 | if (InReg) |
| 7376 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7377 | return ABIArgInfo::getExtend(); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7378 | } |
| 7379 | if (InReg) |
| 7380 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7381 | return ABIArgInfo::getDirect(); |
| 7382 | } |
| 7383 | |
| 7384 | namespace { |
| 7385 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7386 | public: |
| 7387 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7388 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7389 | }; |
| 7390 | } |
| 7391 | |
| 7392 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7393 | // AMDGPU ABI Implementation |
| 7394 | //===----------------------------------------------------------------------===// |
| 7395 | |
| 7396 | namespace { |
| 7397 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7398 | class AMDGPUABIInfo final : public DefaultABIInfo { |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7399 | private: |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7400 | static const unsigned MaxNumRegsForArgsRet = 16; |
| 7401 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7402 | unsigned numRegsForType(QualType Ty) const; |
| 7403 | |
| 7404 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 7405 | bool isHomogeneousAggregateSmallEnough(const Type *Base, |
| 7406 | uint64_t Members) const override; |
| 7407 | |
| 7408 | public: |
| 7409 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : |
| 7410 | DefaultABIInfo(CGT) {} |
| 7411 | |
| 7412 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7413 | ABIArgInfo classifyKernelArgumentType(QualType Ty) const; |
| 7414 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7415 | |
| 7416 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7417 | }; |
| 7418 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7419 | bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 7420 | return true; |
| 7421 | } |
| 7422 | |
| 7423 | bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( |
| 7424 | const Type *Base, uint64_t Members) const { |
| 7425 | uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; |
| 7426 | |
| 7427 | // Homogeneous Aggregates may occupy at most 16 registers. |
| 7428 | return Members * NumRegs <= MaxNumRegsForArgsRet; |
| 7429 | } |
| 7430 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7431 | /// Estimate number of registers the type will use when passed in registers. |
| 7432 | unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { |
| 7433 | unsigned NumRegs = 0; |
| 7434 | |
| 7435 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 7436 | // Compute from the number of elements. The reported size is based on the |
| 7437 | // in-memory size, which includes the padding 4th element for 3-vectors. |
| 7438 | QualType EltTy = VT->getElementType(); |
| 7439 | unsigned EltSize = getContext().getTypeSize(EltTy); |
| 7440 | |
| 7441 | // 16-bit element vectors should be passed as packed. |
| 7442 | if (EltSize == 16) |
| 7443 | return (VT->getNumElements() + 1) / 2; |
| 7444 | |
| 7445 | unsigned EltNumRegs = (EltSize + 31) / 32; |
| 7446 | return EltNumRegs * VT->getNumElements(); |
| 7447 | } |
| 7448 | |
| 7449 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7450 | const RecordDecl *RD = RT->getDecl(); |
| 7451 | assert(!RD->hasFlexibleArrayMember()); |
| 7452 | |
| 7453 | for (const FieldDecl *Field : RD->fields()) { |
| 7454 | QualType FieldTy = Field->getType(); |
| 7455 | NumRegs += numRegsForType(FieldTy); |
| 7456 | } |
| 7457 | |
| 7458 | return NumRegs; |
| 7459 | } |
| 7460 | |
| 7461 | return (getContext().getTypeSize(Ty) + 31) / 32; |
| 7462 | } |
| 7463 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7464 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7465 | llvm::CallingConv::ID CC = FI.getCallingConvention(); |
| 7466 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7467 | if (!getCXXABI().classifyReturnType(FI)) |
| 7468 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7469 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7470 | unsigned NumRegsLeft = MaxNumRegsForArgsRet; |
| 7471 | for (auto &Arg : FI.arguments()) { |
| 7472 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) { |
| 7473 | Arg.info = classifyKernelArgumentType(Arg.type); |
| 7474 | } else { |
| 7475 | Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); |
| 7476 | } |
| 7477 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7478 | } |
| 7479 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7480 | ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { |
| 7481 | if (isAggregateTypeForABI(RetTy)) { |
| 7482 | // Records with non-trivial destructors/copy-constructors should not be |
| 7483 | // returned by value. |
| 7484 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 7485 | // Ignore empty structs/unions. |
| 7486 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7487 | return ABIArgInfo::getIgnore(); |
| 7488 | |
| 7489 | // Lower single-element structs to just return a regular value. |
| 7490 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 7491 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7492 | |
| 7493 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
| 7494 | const RecordDecl *RD = RT->getDecl(); |
| 7495 | if (RD->hasFlexibleArrayMember()) |
| 7496 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7497 | } |
| 7498 | |
| 7499 | // Pack aggregates <= 4 bytes into single VGPR or pair. |
| 7500 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7501 | if (Size <= 16) |
| 7502 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7503 | |
| 7504 | if (Size <= 32) |
| 7505 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7506 | |
| 7507 | if (Size <= 64) { |
| 7508 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7509 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7510 | } |
| 7511 | |
| 7512 | if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) |
| 7513 | return ABIArgInfo::getDirect(); |
| 7514 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7515 | } |
| 7516 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7517 | // Otherwise just do the default thing. |
| 7518 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7519 | } |
| 7520 | |
| 7521 | /// For kernels all parameters are really passed in a special buffer. It doesn't |
| 7522 | /// make sense to pass anything byval, so everything must be direct. |
| 7523 | ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { |
| 7524 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7525 | |
| 7526 | // TODO: Can we omit empty structs? |
| 7527 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7528 | // Coerce single element structs to its element. |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7529 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7530 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7531 | |
| 7532 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7533 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7534 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 7535 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 7536 | } |
| 7537 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7538 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, |
| 7539 | unsigned &NumRegsLeft) const { |
| 7540 | assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); |
| 7541 | |
| 7542 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7543 | |
| 7544 | if (isAggregateTypeForABI(Ty)) { |
| 7545 | // Records with non-trivial destructors/copy-constructors should not be |
| 7546 | // passed by value. |
| 7547 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7548 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7549 | |
| 7550 | // Ignore empty structs/unions. |
| 7551 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7552 | return ABIArgInfo::getIgnore(); |
| 7553 | |
| 7554 | // Lower single-element structs to just pass a regular value. TODO: We |
| 7555 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 7556 | // though watch out for things like bitfields. |
| 7557 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7558 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7559 | |
| 7560 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7561 | const RecordDecl *RD = RT->getDecl(); |
| 7562 | if (RD->hasFlexibleArrayMember()) |
| 7563 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7564 | } |
| 7565 | |
| 7566 | // Pack aggregates <= 8 bytes into single VGPR or pair. |
| 7567 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7568 | if (Size <= 64) { |
| 7569 | unsigned NumRegs = (Size + 31) / 32; |
| 7570 | NumRegsLeft -= std::min(NumRegsLeft, NumRegs); |
| 7571 | |
| 7572 | if (Size <= 16) |
| 7573 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7574 | |
| 7575 | if (Size <= 32) |
| 7576 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7577 | |
| 7578 | // XXX: Should this be i64 instead, and should the limit increase? |
| 7579 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7580 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7581 | } |
| 7582 | |
| 7583 | if (NumRegsLeft > 0) { |
| 7584 | unsigned NumRegs = numRegsForType(Ty); |
| 7585 | if (NumRegsLeft >= NumRegs) { |
| 7586 | NumRegsLeft -= NumRegs; |
| 7587 | return ABIArgInfo::getDirect(); |
| 7588 | } |
| 7589 | } |
| 7590 | } |
| 7591 | |
| 7592 | // Otherwise just do the default thing. |
| 7593 | ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); |
| 7594 | if (!ArgInfo.isIndirect()) { |
| 7595 | unsigned NumRegs = numRegsForType(Ty); |
| 7596 | NumRegsLeft -= std::min(NumRegs, NumRegsLeft); |
| 7597 | } |
| 7598 | |
| 7599 | return ArgInfo; |
| 7600 | } |
| 7601 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7602 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7603 | public: |
| 7604 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7605 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7606 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7607 | CodeGen::CodeGenModule &M, |
| 7608 | ForDefinition_t IsForDefinition) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7609 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7610 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7611 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7612 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7613 | |
| 7614 | unsigned getASTAllocaAddressSpace() const override { |
| 7615 | return LangAS::FirstTargetAddressSpace + |
| 7616 | getABIInfo().getDataLayout().getAllocaAddrSpace(); |
| 7617 | } |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7618 | unsigned getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7619 | const VarDecl *D) const override; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7620 | llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S, |
| 7621 | llvm::LLVMContext &C) const override; |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 7622 | llvm::Function * |
| 7623 | createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 7624 | llvm::Function *BlockInvokeFunc, |
| 7625 | llvm::Value *BlockLiteral) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7626 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7627 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7628 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7629 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7630 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, |
| 7631 | ForDefinition_t IsForDefinition) const { |
| 7632 | if (!IsForDefinition) |
| 7633 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7634 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7635 | if (!FD) |
| 7636 | return; |
| 7637 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7638 | llvm::Function *F = cast<llvm::Function>(GV); |
| 7639 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7640 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 7641 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
| 7642 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 7643 | if (ReqdWGS || FlatWGS) { |
| 7644 | unsigned Min = FlatWGS ? FlatWGS->getMin() : 0; |
| 7645 | unsigned Max = FlatWGS ? FlatWGS->getMax() : 0; |
| 7646 | if (ReqdWGS && Min == 0 && Max == 0) |
| 7647 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7648 | |
| 7649 | if (Min != 0) { |
| 7650 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 7651 | |
| 7652 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 7653 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 7654 | } else |
| 7655 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7656 | } |
| 7657 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7658 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 7659 | unsigned Min = Attr->getMin(); |
| 7660 | unsigned Max = Attr->getMax(); |
| 7661 | |
| 7662 | if (Min != 0) { |
| 7663 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 7664 | |
| 7665 | std::string AttrVal = llvm::utostr(Min); |
| 7666 | if (Max != 0) |
| 7667 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 7668 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 7669 | } else |
| 7670 | assert(Max == 0 && "Max must be zero"); |
| 7671 | } |
| 7672 | |
| 7673 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7674 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7675 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7676 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7677 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 7678 | } |
| 7679 | |
| 7680 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7681 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 7682 | |
| 7683 | if (NumVGPR != 0) |
| 7684 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7685 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7686 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7687 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7688 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7689 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7690 | } |
| 7691 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7692 | // Currently LLVM assumes null pointers always have value 0, |
| 7693 | // which results in incorrectly transformed IR. Therefore, instead of |
| 7694 | // emitting null pointers in private and local address spaces, a null |
| 7695 | // pointer in generic address space is emitted which is casted to a |
| 7696 | // pointer in local or private address space. |
| 7697 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 7698 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 7699 | QualType QT) const { |
| 7700 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 7701 | return llvm::ConstantPointerNull::get(PT); |
| 7702 | |
| 7703 | auto &Ctx = CGM.getContext(); |
| 7704 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 7705 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 7706 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 7707 | llvm::ConstantPointerNull::get(NPT), PT); |
| 7708 | } |
| 7709 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7710 | unsigned |
| 7711 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7712 | const VarDecl *D) const { |
| 7713 | assert(!CGM.getLangOpts().OpenCL && |
| 7714 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 7715 | "Address space agnostic languages only"); |
| 7716 | unsigned DefaultGlobalAS = |
| 7717 | LangAS::FirstTargetAddressSpace + |
| 7718 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global); |
| 7719 | if (!D) |
| 7720 | return DefaultGlobalAS; |
| 7721 | |
| 7722 | unsigned AddrSpace = D->getType().getAddressSpace(); |
| 7723 | assert(AddrSpace == LangAS::Default || |
| 7724 | AddrSpace >= LangAS::FirstTargetAddressSpace); |
| 7725 | if (AddrSpace != LangAS::Default) |
| 7726 | return AddrSpace; |
| 7727 | |
| 7728 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 7729 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 7730 | return ConstAS.getValue(); |
| 7731 | } |
| 7732 | return DefaultGlobalAS; |
| 7733 | } |
| 7734 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7735 | llvm::SyncScope::ID |
| 7736 | AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, |
| 7737 | llvm::LLVMContext &C) const { |
| 7738 | StringRef Name; |
| 7739 | switch (S) { |
| 7740 | case SyncScope::OpenCLWorkGroup: |
| 7741 | Name = "workgroup"; |
| 7742 | break; |
| 7743 | case SyncScope::OpenCLDevice: |
| 7744 | Name = "agent"; |
| 7745 | break; |
| 7746 | case SyncScope::OpenCLAllSVMDevices: |
| 7747 | Name = ""; |
| 7748 | break; |
| 7749 | case SyncScope::OpenCLSubGroup: |
| 7750 | Name = "subgroup"; |
| 7751 | } |
| 7752 | return C.getOrInsertSyncScopeID(Name); |
| 7753 | } |
| 7754 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7755 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 7756 | // SPARC v8 ABI Implementation. |
| 7757 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7758 | // |
| 7759 | // Ensures that complex values are passed in registers. |
| 7760 | // |
| 7761 | namespace { |
| 7762 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 7763 | public: |
| 7764 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7765 | |
| 7766 | private: |
| 7767 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7768 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7769 | }; |
| 7770 | } // end anonymous namespace |
| 7771 | |
| 7772 | |
| 7773 | ABIArgInfo |
| 7774 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 7775 | if (Ty->isAnyComplexType()) { |
| 7776 | return ABIArgInfo::getDirect(); |
| 7777 | } |
| 7778 | else { |
| 7779 | return DefaultABIInfo::classifyReturnType(Ty); |
| 7780 | } |
| 7781 | } |
| 7782 | |
| 7783 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7784 | |
| 7785 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7786 | for (auto &Arg : FI.arguments()) |
| 7787 | Arg.info = classifyArgumentType(Arg.type); |
| 7788 | } |
| 7789 | |
| 7790 | namespace { |
| 7791 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7792 | public: |
| 7793 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7794 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 7795 | }; |
| 7796 | } // end anonymous namespace |
| 7797 | |
| 7798 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7799 | // SPARC v9 ABI Implementation. |
| 7800 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7801 | // |
| 7802 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 7803 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 7804 | // the array, structs larger than 16 bytes are passed indirectly. |
| 7805 | // |
| 7806 | // One case requires special care: |
| 7807 | // |
| 7808 | // struct mixed { |
| 7809 | // int i; |
| 7810 | // float f; |
| 7811 | // }; |
| 7812 | // |
| 7813 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 7814 | // parameter array, but the int is passed in an integer register, and the float |
| 7815 | // is passed in a floating point register. This is represented as two arguments |
| 7816 | // with the LLVM IR inreg attribute: |
| 7817 | // |
| 7818 | // declare void f(i32 inreg %i, float inreg %f) |
| 7819 | // |
| 7820 | // The code generator will only allocate 4 bytes from the parameter array for |
| 7821 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 7822 | // bytes. |
| 7823 | // |
| 7824 | namespace { |
| 7825 | class SparcV9ABIInfo : public ABIInfo { |
| 7826 | public: |
| 7827 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7828 | |
| 7829 | private: |
| 7830 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7831 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7832 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7833 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7834 | |
| 7835 | // Coercion type builder for structs passed in registers. The coercion type |
| 7836 | // serves two purposes: |
| 7837 | // |
| 7838 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 7839 | // in registers. |
| 7840 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 7841 | // code generator knows to pass them in floating point registers. |
| 7842 | // |
| 7843 | // We also compute the InReg flag which indicates that the struct contains |
| 7844 | // aligned 32-bit floats. |
| 7845 | // |
| 7846 | struct CoerceBuilder { |
| 7847 | llvm::LLVMContext &Context; |
| 7848 | const llvm::DataLayout &DL; |
| 7849 | SmallVector<llvm::Type*, 8> Elems; |
| 7850 | uint64_t Size; |
| 7851 | bool InReg; |
| 7852 | |
| 7853 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 7854 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 7855 | |
| 7856 | // Pad Elems with integers until Size is ToSize. |
| 7857 | void pad(uint64_t ToSize) { |
| 7858 | assert(ToSize >= Size && "Cannot remove elements"); |
| 7859 | if (ToSize == Size) |
| 7860 | return; |
| 7861 | |
| 7862 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7863 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7864 | if (Aligned > Size && Aligned <= ToSize) { |
| 7865 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 7866 | Size = Aligned; |
| 7867 | } |
| 7868 | |
| 7869 | // Add whole 64-bit words. |
| 7870 | while (Size + 64 <= ToSize) { |
| 7871 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 7872 | Size += 64; |
| 7873 | } |
| 7874 | |
| 7875 | // Final in-word padding. |
| 7876 | if (Size < ToSize) { |
| 7877 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 7878 | Size = ToSize; |
| 7879 | } |
| 7880 | } |
| 7881 | |
| 7882 | // Add a floating point element at Offset. |
| 7883 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 7884 | // Unaligned floats are treated as integers. |
| 7885 | if (Offset % Bits) |
| 7886 | return; |
| 7887 | // The InReg flag is only required if there are any floats < 64 bits. |
| 7888 | if (Bits < 64) |
| 7889 | InReg = true; |
| 7890 | pad(Offset); |
| 7891 | Elems.push_back(Ty); |
| 7892 | Size = Offset + Bits; |
| 7893 | } |
| 7894 | |
| 7895 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 7896 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 7897 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 7898 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 7899 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 7900 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 7901 | switch (ElemTy->getTypeID()) { |
| 7902 | case llvm::Type::StructTyID: |
| 7903 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 7904 | break; |
| 7905 | case llvm::Type::FloatTyID: |
| 7906 | addFloat(ElemOffset, ElemTy, 32); |
| 7907 | break; |
| 7908 | case llvm::Type::DoubleTyID: |
| 7909 | addFloat(ElemOffset, ElemTy, 64); |
| 7910 | break; |
| 7911 | case llvm::Type::FP128TyID: |
| 7912 | addFloat(ElemOffset, ElemTy, 128); |
| 7913 | break; |
| 7914 | case llvm::Type::PointerTyID: |
| 7915 | if (ElemOffset % 64 == 0) { |
| 7916 | pad(ElemOffset); |
| 7917 | Elems.push_back(ElemTy); |
| 7918 | Size += 64; |
| 7919 | } |
| 7920 | break; |
| 7921 | default: |
| 7922 | break; |
| 7923 | } |
| 7924 | } |
| 7925 | } |
| 7926 | |
| 7927 | // Check if Ty is a usable substitute for the coercion type. |
| 7928 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 7929 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7930 | } |
| 7931 | |
| 7932 | // Get the coercion type as a literal struct type. |
| 7933 | llvm::Type *getType() const { |
| 7934 | if (Elems.size() == 1) |
| 7935 | return Elems.front(); |
| 7936 | else |
| 7937 | return llvm::StructType::get(Context, Elems); |
| 7938 | } |
| 7939 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7940 | }; |
| 7941 | } // end anonymous namespace |
| 7942 | |
| 7943 | ABIArgInfo |
| 7944 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 7945 | if (Ty->isVoidType()) |
| 7946 | return ABIArgInfo::getIgnore(); |
| 7947 | |
| 7948 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7949 | |
| 7950 | // Anything too big to fit in registers is passed with an explicit indirect |
| 7951 | // pointer / sret pointer. |
| 7952 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7953 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7954 | |
| 7955 | // Treat an enum type as its underlying type. |
| 7956 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7957 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7958 | |
| 7959 | // Integer types smaller than a register are extended. |
| 7960 | if (Size < 64 && Ty->isIntegerType()) |
| 7961 | return ABIArgInfo::getExtend(); |
| 7962 | |
| 7963 | // Other non-aggregates go in registers. |
| 7964 | if (!isAggregateTypeForABI(Ty)) |
| 7965 | return ABIArgInfo::getDirect(); |
| 7966 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7967 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 7968 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 7969 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7970 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7971 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7972 | // 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] | 7973 | // Build a coercion type from the LLVM struct type. |
| 7974 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7975 | if (!StrTy) |
| 7976 | return ABIArgInfo::getDirect(); |
| 7977 | |
| 7978 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 7979 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7980 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7981 | |
| 7982 | // Try to use the original type for coercion. |
| 7983 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 7984 | |
| 7985 | if (CB.InReg) |
| 7986 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 7987 | else |
| 7988 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7989 | } |
| 7990 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7991 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7992 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7993 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 7994 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7995 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7996 | AI.setCoerceToType(ArgTy); |
| 7997 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7998 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7999 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8000 | CGBuilderTy &Builder = CGF.Builder; |
| 8001 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 8002 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 8003 | |
| 8004 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 8005 | |
| 8006 | Address ArgAddr = Address::invalid(); |
| 8007 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8008 | switch (AI.getKind()) { |
| 8009 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8010 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8011 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8012 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8013 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8014 | case ABIArgInfo::Extend: { |
| 8015 | Stride = SlotSize; |
| 8016 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 8017 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8018 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8019 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8020 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8021 | case ABIArgInfo::Direct: { |
| 8022 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8023 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8024 | ArgAddr = Addr; |
| 8025 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8026 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8027 | |
| 8028 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8029 | Stride = SlotSize; |
| 8030 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 8031 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 8032 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8033 | break; |
| 8034 | |
| 8035 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8036 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8037 | } |
| 8038 | |
| 8039 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8040 | llvm::Value *NextPtr = |
| 8041 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 8042 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8043 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8044 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8045 | } |
| 8046 | |
| 8047 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8048 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 8049 | for (auto &I : FI.arguments()) |
| 8050 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8051 | } |
| 8052 | |
| 8053 | namespace { |
| 8054 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8055 | public: |
| 8056 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8057 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8058 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8059 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8060 | return 14; |
| 8061 | } |
| 8062 | |
| 8063 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8064 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8065 | }; |
| 8066 | } // end anonymous namespace |
| 8067 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8068 | bool |
| 8069 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 8070 | llvm::Value *Address) const { |
| 8071 | // This is calculated from the LLVM and GCC tables and verified |
| 8072 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 8073 | |
| 8074 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 8075 | |
| 8076 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 8077 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 8078 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 8079 | |
| 8080 | // 0-31: the 8-byte general-purpose registers |
| 8081 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 8082 | |
| 8083 | // 32-63: f0-31, the 4-byte floating-point registers |
| 8084 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 8085 | |
| 8086 | // Y = 64 |
| 8087 | // PSR = 65 |
| 8088 | // WIM = 66 |
| 8089 | // TBR = 67 |
| 8090 | // PC = 68 |
| 8091 | // NPC = 69 |
| 8092 | // FSR = 70 |
| 8093 | // CSR = 71 |
| 8094 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8095 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8096 | // 72-87: d0-15, the 8-byte floating-point registers |
| 8097 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 8098 | |
| 8099 | return false; |
| 8100 | } |
| 8101 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8102 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8103 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8104 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8105 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8106 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8107 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8108 | |
| 8109 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 8110 | /// it by reference between functions that append to it. |
| 8111 | typedef llvm::SmallString<128> SmallStringEnc; |
| 8112 | |
| 8113 | /// TypeStringCache caches the meta encodings of Types. |
| 8114 | /// |
| 8115 | /// The reason for caching TypeStrings is two fold: |
| 8116 | /// 1. To cache a type's encoding for later uses; |
| 8117 | /// 2. As a means to break recursive member type inclusion. |
| 8118 | /// |
| 8119 | /// A cache Entry can have a Status of: |
| 8120 | /// NonRecursive: The type encoding is not recursive; |
| 8121 | /// Recursive: The type encoding is recursive; |
| 8122 | /// Incomplete: An incomplete TypeString; |
| 8123 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 8124 | /// Recursive type encoding. |
| 8125 | /// |
| 8126 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 8127 | /// as possible. Whilst it may contain types which are recursive, the type |
| 8128 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 8129 | /// the type is encountered. |
| 8130 | /// |
| 8131 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 8132 | /// possible. The type itself is recursive and it may contain other types which |
| 8133 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 8134 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 8135 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 8136 | /// |
| 8137 | /// An Incomplete entry is always a RecordType and only encodes its |
| 8138 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 8139 | /// are placed into the cache during type expansion as a means to identify and |
| 8140 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 8141 | /// the entry becomes IncompleteUsed. |
| 8142 | /// |
| 8143 | /// During the expansion of a RecordType's members: |
| 8144 | /// |
| 8145 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 8146 | /// cached encoding is used; |
| 8147 | /// |
| 8148 | /// If the cache contains a Recursive encoding for the member type, the |
| 8149 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 8150 | /// |
| 8151 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 8152 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 8153 | /// |
| 8154 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 8155 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 8156 | /// it is swapped back in; |
| 8157 | /// |
| 8158 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 8159 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 8160 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 8161 | /// |
| 8162 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 8163 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 8164 | /// Else the member is part of a recursive type and thus the recursion has |
| 8165 | /// been exited too soon for the encoding to be correct for the member. |
| 8166 | /// |
| 8167 | class TypeStringCache { |
| 8168 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 8169 | struct Entry { |
| 8170 | std::string Str; // The encoded TypeString for the type. |
| 8171 | enum Status State; // Information about the encoding in 'Str'. |
| 8172 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 8173 | // during the expansion of RecordType's members. |
| 8174 | }; |
| 8175 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 8176 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 8177 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 8178 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8179 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8180 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 8181 | bool removeIncomplete(const IdentifierInfo *ID); |
| 8182 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8183 | bool IsRecursive); |
| 8184 | StringRef lookupStr(const IdentifierInfo *ID); |
| 8185 | }; |
| 8186 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8187 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8188 | /// FieldEncoding is a helper for this ordering process. |
| 8189 | class FieldEncoding { |
| 8190 | bool HasName; |
| 8191 | std::string Enc; |
| 8192 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8193 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8194 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8195 | bool operator<(const FieldEncoding &rhs) const { |
| 8196 | if (HasName != rhs.HasName) return HasName; |
| 8197 | return Enc < rhs.Enc; |
| 8198 | } |
| 8199 | }; |
| 8200 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8201 | class XCoreABIInfo : public DefaultABIInfo { |
| 8202 | public: |
| 8203 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8204 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8205 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8206 | }; |
| 8207 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8208 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8209 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8210 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8211 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8212 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 8213 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8214 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8215 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8216 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8217 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8218 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 8219 | // TODO: this implementation is likely now redundant with the default |
| 8220 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8221 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8222 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8223 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8224 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8225 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8226 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 8227 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8228 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8229 | // Handle the argument. |
| 8230 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8231 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8232 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8233 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8234 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8235 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8236 | |
| 8237 | Address Val = Address::invalid(); |
| 8238 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8239 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8240 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8241 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8242 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8243 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8244 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8245 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 8246 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8247 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8248 | case ABIArgInfo::Extend: |
| 8249 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8250 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 8251 | ArgSize = CharUnits::fromQuantity( |
| 8252 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8253 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8254 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8255 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8256 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 8257 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 8258 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8259 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8260 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8261 | |
| 8262 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8263 | if (!ArgSize.isZero()) { |
| 8264 | llvm::Value *APN = |
| 8265 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 8266 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8267 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8268 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8269 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8270 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8271 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8272 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8273 | /// into the cache as a means to identify and break recursion. |
| 8274 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8275 | /// be reinserted by removeIncomplete(). |
| 8276 | /// All other types of encoding should have been used rather than arriving here. |
| 8277 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8278 | std::string StubEnc) { |
| 8279 | if (!ID) |
| 8280 | return; |
| 8281 | Entry &E = Map[ID]; |
| 8282 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8283 | "Incorrectly use of addIncomplete"); |
| 8284 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8285 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8286 | E.Str.swap(StubEnc); |
| 8287 | E.State = Incomplete; |
| 8288 | ++IncompleteCount; |
| 8289 | } |
| 8290 | |
| 8291 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8292 | /// must be removed from the cache. |
| 8293 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8294 | /// Returns true if the RecordType was defined recursively. |
| 8295 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8296 | if (!ID) |
| 8297 | return false; |
| 8298 | auto I = Map.find(ID); |
| 8299 | assert(I != Map.end() && "Entry not present"); |
| 8300 | Entry &E = I->second; |
| 8301 | assert( (E.State == Incomplete || |
| 8302 | E.State == IncompleteUsed) && |
| 8303 | "Entry must be an incomplete type"); |
| 8304 | bool IsRecursive = false; |
| 8305 | if (E.State == IncompleteUsed) { |
| 8306 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8307 | IsRecursive = true; |
| 8308 | --IncompleteUsedCount; |
| 8309 | } |
| 8310 | if (E.Swapped.empty()) |
| 8311 | Map.erase(I); |
| 8312 | else { |
| 8313 | // Swap the Recursive back. |
| 8314 | E.Swapped.swap(E.Str); |
| 8315 | E.Swapped.clear(); |
| 8316 | E.State = Recursive; |
| 8317 | } |
| 8318 | --IncompleteCount; |
| 8319 | return IsRecursive; |
| 8320 | } |
| 8321 | |
| 8322 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8323 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8324 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8325 | bool IsRecursive) { |
| 8326 | if (!ID || IncompleteUsedCount) |
| 8327 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8328 | Entry &E = Map[ID]; |
| 8329 | if (IsRecursive && !E.Str.empty()) { |
| 8330 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8331 | "This is not the same Recursive entry"); |
| 8332 | // The parent container was not recursive after all, so we could have used |
| 8333 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8334 | // we started viz: IncompleteCount!=0. |
| 8335 | return; |
| 8336 | } |
| 8337 | assert(E.Str.empty() && "Entry already present"); |
| 8338 | E.Str = Str.str(); |
| 8339 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8340 | } |
| 8341 | |
| 8342 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8343 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8344 | /// encoding is Recursive, return an empty StringRef. |
| 8345 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8346 | if (!ID) |
| 8347 | return StringRef(); // We have no key. |
| 8348 | auto I = Map.find(ID); |
| 8349 | if (I == Map.end()) |
| 8350 | return StringRef(); // We have no encoding. |
| 8351 | Entry &E = I->second; |
| 8352 | if (E.State == Recursive && IncompleteCount) |
| 8353 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8354 | |
| 8355 | if (E.State == Incomplete) { |
| 8356 | // The incomplete type is being used to break out of recursion. |
| 8357 | E.State = IncompleteUsed; |
| 8358 | ++IncompleteUsedCount; |
| 8359 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8360 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8361 | } |
| 8362 | |
| 8363 | /// The XCore ABI includes a type information section that communicates symbol |
| 8364 | /// type information to the linker. The linker uses this information to verify |
| 8365 | /// safety/correctness of things such as array bound and pointers et al. |
| 8366 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8367 | /// This type information (TypeString) is emitted into meta data for all global |
| 8368 | /// symbols: definitions, declarations, functions & variables. |
| 8369 | /// |
| 8370 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8371 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8372 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8373 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8374 | /// |
| 8375 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8376 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8377 | |
| 8378 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8379 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8380 | CodeGen::CodeGenModule &CGM) const { |
| 8381 | SmallStringEnc Enc; |
| 8382 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8383 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8384 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8385 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8386 | llvm::NamedMDNode *MD = |
| 8387 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8388 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8389 | } |
| 8390 | } |
| 8391 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8392 | //===----------------------------------------------------------------------===// |
| 8393 | // SPIR ABI Implementation |
| 8394 | //===----------------------------------------------------------------------===// |
| 8395 | |
| 8396 | namespace { |
| 8397 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8398 | public: |
| 8399 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8400 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8401 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8402 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8403 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8404 | } // End anonymous namespace. |
| 8405 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8406 | namespace clang { |
| 8407 | namespace CodeGen { |
| 8408 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8409 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8410 | SPIRABI.computeInfo(FI); |
| 8411 | } |
| 8412 | } |
| 8413 | } |
| 8414 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8415 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8416 | return llvm::CallingConv::SPIR_KERNEL; |
| 8417 | } |
| 8418 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8419 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8420 | const CodeGen::CodeGenModule &CGM, |
| 8421 | TypeStringCache &TSC); |
| 8422 | |
| 8423 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8424 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8425 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8426 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8427 | const RecordDecl *RD, |
| 8428 | const CodeGen::CodeGenModule &CGM, |
| 8429 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8430 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8431 | SmallStringEnc Enc; |
| 8432 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8433 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8434 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8435 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8436 | Enc += "b("; |
| 8437 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8438 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8439 | Enc += ':'; |
| 8440 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8441 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8442 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8443 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8444 | Enc += ')'; |
| 8445 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 8446 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8447 | } |
| 8448 | return true; |
| 8449 | } |
| 8450 | |
| 8451 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 8452 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 8453 | /// Union types have their fields ordered according to the ABI. |
| 8454 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 8455 | const CodeGen::CodeGenModule &CGM, |
| 8456 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 8457 | // Append the cached TypeString if we have one. |
| 8458 | StringRef TypeString = TSC.lookupStr(ID); |
| 8459 | if (!TypeString.empty()) { |
| 8460 | Enc += TypeString; |
| 8461 | return true; |
| 8462 | } |
| 8463 | |
| 8464 | // Start to emit an incomplete TypeString. |
| 8465 | size_t Start = Enc.size(); |
| 8466 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 8467 | Enc += '('; |
| 8468 | if (ID) |
| 8469 | Enc += ID->getName(); |
| 8470 | Enc += "){"; |
| 8471 | |
| 8472 | // We collect all encoded fields and order as necessary. |
| 8473 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8474 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 8475 | if (RD && !RD->field_empty()) { |
| 8476 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 8477 | // so that recursive calls to this RecordType will use it whilst building a |
| 8478 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8479 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8480 | std::string StubEnc(Enc.substr(Start).str()); |
| 8481 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 8482 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 8483 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 8484 | (void) TSC.removeIncomplete(ID); |
| 8485 | return false; |
| 8486 | } |
| 8487 | IsRecursive = TSC.removeIncomplete(ID); |
| 8488 | // The ABI requires unions to be sorted but not structures. |
| 8489 | // See FieldEncoding::operator< for sort algorithm. |
| 8490 | if (RT->isUnionType()) |
| 8491 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8492 | // We can now complete the TypeString. |
| 8493 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8494 | for (unsigned I = 0; I != E; ++I) { |
| 8495 | if (I) |
| 8496 | Enc += ','; |
| 8497 | Enc += FE[I].str(); |
| 8498 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8499 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8500 | Enc += '}'; |
| 8501 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 8502 | return true; |
| 8503 | } |
| 8504 | |
| 8505 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 8506 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 8507 | TypeStringCache &TSC, |
| 8508 | const IdentifierInfo *ID) { |
| 8509 | // Append the cached TypeString if we have one. |
| 8510 | StringRef TypeString = TSC.lookupStr(ID); |
| 8511 | if (!TypeString.empty()) { |
| 8512 | Enc += TypeString; |
| 8513 | return true; |
| 8514 | } |
| 8515 | |
| 8516 | size_t Start = Enc.size(); |
| 8517 | Enc += "e("; |
| 8518 | if (ID) |
| 8519 | Enc += ID->getName(); |
| 8520 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8521 | |
| 8522 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8523 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8524 | SmallVector<FieldEncoding, 16> FE; |
| 8525 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 8526 | ++I) { |
| 8527 | SmallStringEnc EnumEnc; |
| 8528 | EnumEnc += "m("; |
| 8529 | EnumEnc += I->getName(); |
| 8530 | EnumEnc += "){"; |
| 8531 | I->getInitVal().toString(EnumEnc); |
| 8532 | EnumEnc += '}'; |
| 8533 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 8534 | } |
| 8535 | std::sort(FE.begin(), FE.end()); |
| 8536 | unsigned E = FE.size(); |
| 8537 | for (unsigned I = 0; I != E; ++I) { |
| 8538 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8539 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8540 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8541 | } |
| 8542 | } |
| 8543 | Enc += '}'; |
| 8544 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 8545 | return true; |
| 8546 | } |
| 8547 | |
| 8548 | /// Appends type's qualifier to Enc. |
| 8549 | /// This is done prior to appending the type's encoding. |
| 8550 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 8551 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 8552 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8553 | int Lookup = 0; |
| 8554 | if (QT.isConstQualified()) |
| 8555 | Lookup += 1<<0; |
| 8556 | if (QT.isRestrictQualified()) |
| 8557 | Lookup += 1<<1; |
| 8558 | if (QT.isVolatileQualified()) |
| 8559 | Lookup += 1<<2; |
| 8560 | Enc += Table[Lookup]; |
| 8561 | } |
| 8562 | |
| 8563 | /// Appends built-in types to Enc. |
| 8564 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 8565 | const char *EncType; |
| 8566 | switch (BT->getKind()) { |
| 8567 | case BuiltinType::Void: |
| 8568 | EncType = "0"; |
| 8569 | break; |
| 8570 | case BuiltinType::Bool: |
| 8571 | EncType = "b"; |
| 8572 | break; |
| 8573 | case BuiltinType::Char_U: |
| 8574 | EncType = "uc"; |
| 8575 | break; |
| 8576 | case BuiltinType::UChar: |
| 8577 | EncType = "uc"; |
| 8578 | break; |
| 8579 | case BuiltinType::SChar: |
| 8580 | EncType = "sc"; |
| 8581 | break; |
| 8582 | case BuiltinType::UShort: |
| 8583 | EncType = "us"; |
| 8584 | break; |
| 8585 | case BuiltinType::Short: |
| 8586 | EncType = "ss"; |
| 8587 | break; |
| 8588 | case BuiltinType::UInt: |
| 8589 | EncType = "ui"; |
| 8590 | break; |
| 8591 | case BuiltinType::Int: |
| 8592 | EncType = "si"; |
| 8593 | break; |
| 8594 | case BuiltinType::ULong: |
| 8595 | EncType = "ul"; |
| 8596 | break; |
| 8597 | case BuiltinType::Long: |
| 8598 | EncType = "sl"; |
| 8599 | break; |
| 8600 | case BuiltinType::ULongLong: |
| 8601 | EncType = "ull"; |
| 8602 | break; |
| 8603 | case BuiltinType::LongLong: |
| 8604 | EncType = "sll"; |
| 8605 | break; |
| 8606 | case BuiltinType::Float: |
| 8607 | EncType = "ft"; |
| 8608 | break; |
| 8609 | case BuiltinType::Double: |
| 8610 | EncType = "d"; |
| 8611 | break; |
| 8612 | case BuiltinType::LongDouble: |
| 8613 | EncType = "ld"; |
| 8614 | break; |
| 8615 | default: |
| 8616 | return false; |
| 8617 | } |
| 8618 | Enc += EncType; |
| 8619 | return true; |
| 8620 | } |
| 8621 | |
| 8622 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 8623 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 8624 | const CodeGen::CodeGenModule &CGM, |
| 8625 | TypeStringCache &TSC) { |
| 8626 | Enc += "p("; |
| 8627 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 8628 | return false; |
| 8629 | Enc += ')'; |
| 8630 | return true; |
| 8631 | } |
| 8632 | |
| 8633 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8634 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 8635 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8636 | const CodeGen::CodeGenModule &CGM, |
| 8637 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 8638 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 8639 | return false; |
| 8640 | Enc += "a("; |
| 8641 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 8642 | CAT->getSize().toStringUnsigned(Enc); |
| 8643 | else |
| 8644 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 8645 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8646 | // The Qualifiers should be attached to the type rather than the array. |
| 8647 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8648 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 8649 | return false; |
| 8650 | Enc += ')'; |
| 8651 | return true; |
| 8652 | } |
| 8653 | |
| 8654 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 8655 | /// and the arguments. |
| 8656 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 8657 | const CodeGen::CodeGenModule &CGM, |
| 8658 | TypeStringCache &TSC) { |
| 8659 | Enc += "f{"; |
| 8660 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 8661 | return false; |
| 8662 | Enc += "}("; |
| 8663 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 8664 | // N.B. we are only interested in the adjusted param types. |
| 8665 | auto I = FPT->param_type_begin(); |
| 8666 | auto E = FPT->param_type_end(); |
| 8667 | if (I != E) { |
| 8668 | do { |
| 8669 | if (!appendType(Enc, *I, CGM, TSC)) |
| 8670 | return false; |
| 8671 | ++I; |
| 8672 | if (I != E) |
| 8673 | Enc += ','; |
| 8674 | } while (I != E); |
| 8675 | if (FPT->isVariadic()) |
| 8676 | Enc += ",va"; |
| 8677 | } else { |
| 8678 | if (FPT->isVariadic()) |
| 8679 | Enc += "va"; |
| 8680 | else |
| 8681 | Enc += '0'; |
| 8682 | } |
| 8683 | } |
| 8684 | Enc += ')'; |
| 8685 | return true; |
| 8686 | } |
| 8687 | |
| 8688 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 8689 | /// type encodings. |
| 8690 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8691 | const CodeGen::CodeGenModule &CGM, |
| 8692 | TypeStringCache &TSC) { |
| 8693 | |
| 8694 | QualType QT = QType.getCanonicalType(); |
| 8695 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8696 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 8697 | // The Qualifiers should be attached to the type rather than the array. |
| 8698 | // Thus we don't call appendQualifier() here. |
| 8699 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 8700 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8701 | appendQualifier(Enc, QT); |
| 8702 | |
| 8703 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 8704 | return appendBuiltinType(Enc, BT); |
| 8705 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8706 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 8707 | return appendPointerType(Enc, PT, CGM, TSC); |
| 8708 | |
| 8709 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 8710 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 8711 | |
| 8712 | if (const RecordType *RT = QT->getAsStructureType()) |
| 8713 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8714 | |
| 8715 | if (const RecordType *RT = QT->getAsUnionType()) |
| 8716 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8717 | |
| 8718 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 8719 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 8720 | |
| 8721 | return false; |
| 8722 | } |
| 8723 | |
| 8724 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8725 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 8726 | if (!D) |
| 8727 | return false; |
| 8728 | |
| 8729 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8730 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 8731 | return false; |
| 8732 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 8733 | } |
| 8734 | |
| 8735 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8736 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 8737 | return false; |
| 8738 | QualType QT = VD->getType().getCanonicalType(); |
| 8739 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 8740 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8741 | // The Qualifiers should be attached to the type rather than the array. |
| 8742 | // Thus we don't call appendQualifier() here. |
| 8743 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8744 | } |
| 8745 | return appendType(Enc, QT, CGM, TSC); |
| 8746 | } |
| 8747 | return false; |
| 8748 | } |
| 8749 | |
| 8750 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8751 | //===----------------------------------------------------------------------===// |
| 8752 | // Driver code |
| 8753 | //===----------------------------------------------------------------------===// |
| 8754 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8755 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 8756 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8757 | } |
| 8758 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 8759 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8760 | if (TheTargetCodeGenInfo) |
| 8761 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8762 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8763 | // Helper to set the unique_ptr while still keeping the return value. |
| 8764 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 8765 | this->TheTargetCodeGenInfo.reset(P); |
| 8766 | return *P; |
| 8767 | }; |
| 8768 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 8769 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 8770 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8771 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8772 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8773 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 8774 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8775 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 8776 | case llvm::Triple::mips: |
| 8777 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 8778 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8779 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 8780 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8781 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 8782 | case llvm::Triple::mips64: |
| 8783 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8784 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8785 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 8786 | case llvm::Triple::avr: |
| 8787 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 8788 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 8789 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 8790 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8791 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 8792 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8793 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 8794 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 8795 | return SetCGInfo( |
| 8796 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8797 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8798 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8799 | } |
| 8800 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8801 | case llvm::Triple::wasm32: |
| 8802 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8803 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8804 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8805 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 8806 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8807 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8808 | case llvm::Triple::thumbeb: { |
| 8809 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 8810 | return SetCGInfo( |
| 8811 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 8812 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8813 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8814 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 8815 | StringRef ABIStr = getTarget().getABI(); |
| 8816 | if (ABIStr == "apcs-gnu") |
| 8817 | Kind = ARMABIInfo::APCS; |
| 8818 | else if (ABIStr == "aapcs16") |
| 8819 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 8820 | else if (CodeGenOpts.FloatABI == "hard" || |
| 8821 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8822 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 8823 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 8824 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8825 | Kind = ARMABIInfo::AAPCS_VFP; |
| 8826 | |
| 8827 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 8828 | } |
| 8829 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8830 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8831 | return SetCGInfo( |
| 8832 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 8833 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8834 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8835 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8836 | if (getTarget().getABI() == "elfv2") |
| 8837 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8838 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8839 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8840 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8841 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 8842 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8843 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8844 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8845 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 8846 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8847 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8848 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8849 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 8850 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8851 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 8852 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 8853 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 8854 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 8855 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8856 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 8857 | case llvm::Triple::nvptx: |
| 8858 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8859 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 8860 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8861 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8862 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8863 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8864 | case llvm::Triple::systemz: { |
| 8865 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8866 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 8867 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 8868 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8869 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 8870 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8871 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8872 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8873 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8874 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 8875 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8876 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 8877 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 8878 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8879 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8880 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 8881 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8882 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8883 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8884 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 8885 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8886 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 8887 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8888 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8889 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8890 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8891 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8892 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8893 | X86AVXABILevel AVXLevel = |
| 8894 | (ABI == "avx512" |
| 8895 | ? X86AVXABILevel::AVX512 |
| 8896 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 8897 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8898 | switch (Triple.getOS()) { |
| 8899 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8900 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 8901 | case llvm::Triple::PS4: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8902 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8903 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8904 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8905 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8906 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 8907 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8908 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 8909 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8910 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8911 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8912 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 8913 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8914 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 8915 | case llvm::Triple::sparc: |
| 8916 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8917 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8918 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8919 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8920 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8921 | case llvm::Triple::spir: |
| 8922 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8923 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8924 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8925 | } |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 8926 | |
| 8927 | /// Create an OpenCL kernel for an enqueued block. |
| 8928 | /// |
| 8929 | /// The kernel has the same function type as the block invoke function. Its |
| 8930 | /// name is the name of the block invoke function postfixed with "_kernel". |
| 8931 | /// It simply calls the block invoke function then returns. |
| 8932 | llvm::Function * |
| 8933 | TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 8934 | llvm::Function *Invoke, |
| 8935 | llvm::Value *BlockLiteral) const { |
| 8936 | auto *InvokeFT = Invoke->getFunctionType(); |
| 8937 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 8938 | for (auto &P : InvokeFT->params()) |
| 8939 | ArgTys.push_back(P); |
| 8940 | auto &C = CGF.getLLVMContext(); |
| 8941 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 8942 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 8943 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 8944 | &CGF.CGM.getModule()); |
| 8945 | auto IP = CGF.Builder.saveIP(); |
| 8946 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 8947 | auto &Builder = CGF.Builder; |
| 8948 | Builder.SetInsertPoint(BB); |
| 8949 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 8950 | for (auto &A : F->args()) |
| 8951 | Args.push_back(&A); |
| 8952 | Builder.CreateCall(Invoke, Args); |
| 8953 | Builder.CreateRetVoid(); |
| 8954 | Builder.restoreIP(IP); |
| 8955 | return F; |
| 8956 | } |
| 8957 | |
| 8958 | /// Create an OpenCL kernel for an enqueued block. |
| 8959 | /// |
| 8960 | /// The type of the first argument (the block literal) is the struct type |
| 8961 | /// of the block literal instead of a pointer type. The first argument |
| 8962 | /// (block literal) is passed directly by value to the kernel. The kernel |
| 8963 | /// allocates the same type of struct on stack and stores the block literal |
| 8964 | /// to it and passes its pointer to the block invoke function. The kernel |
| 8965 | /// has "enqueued-block" function attribute and kernel argument metadata. |
| 8966 | llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel( |
| 8967 | CodeGenFunction &CGF, llvm::Function *Invoke, |
| 8968 | llvm::Value *BlockLiteral) const { |
| 8969 | auto &Builder = CGF.Builder; |
| 8970 | auto &C = CGF.getLLVMContext(); |
| 8971 | |
| 8972 | auto *BlockTy = BlockLiteral->getType()->getPointerElementType(); |
| 8973 | auto *InvokeFT = Invoke->getFunctionType(); |
| 8974 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 8975 | llvm::SmallVector<llvm::Metadata *, 8> AddressQuals; |
| 8976 | llvm::SmallVector<llvm::Metadata *, 8> AccessQuals; |
| 8977 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames; |
| 8978 | llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames; |
| 8979 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals; |
| 8980 | llvm::SmallVector<llvm::Metadata *, 8> ArgNames; |
| 8981 | |
| 8982 | ArgTys.push_back(BlockTy); |
| 8983 | ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 8984 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0))); |
| 8985 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 8986 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 8987 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 8988 | ArgNames.push_back(llvm::MDString::get(C, "block_literal")); |
| 8989 | for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) { |
| 8990 | ArgTys.push_back(InvokeFT->getParamType(I)); |
| 8991 | ArgTys.push_back(BlockTy); |
| 8992 | ArgTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 8993 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3))); |
| 8994 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 8995 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 8996 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 8997 | ArgNames.push_back( |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame^] | 8998 | llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str())); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 8999 | } |
| 9000 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9001 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9002 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9003 | &CGF.CGM.getModule()); |
| 9004 | F->addFnAttr("enqueued-block"); |
| 9005 | auto IP = CGF.Builder.saveIP(); |
| 9006 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9007 | Builder.SetInsertPoint(BB); |
| 9008 | unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy); |
| 9009 | auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr); |
| 9010 | BlockPtr->setAlignment(BlockAlign); |
| 9011 | Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign); |
| 9012 | auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0)); |
| 9013 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9014 | Args.push_back(Cast); |
| 9015 | for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I) |
| 9016 | Args.push_back(I); |
| 9017 | Builder.CreateCall(Invoke, Args); |
| 9018 | Builder.CreateRetVoid(); |
| 9019 | Builder.restoreIP(IP); |
| 9020 | |
| 9021 | F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals)); |
| 9022 | F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals)); |
| 9023 | F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames)); |
| 9024 | F->setMetadata("kernel_arg_base_type", |
| 9025 | llvm::MDNode::get(C, ArgBaseTypeNames)); |
| 9026 | F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals)); |
| 9027 | if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata) |
| 9028 | F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames)); |
| 9029 | |
| 9030 | return F; |
| 9031 | } |