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 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 204 | LLVM_DUMP_METHOD void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 205 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 206 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 207 | switch (TheKind) { |
| 208 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 209 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 210 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 211 | Ty->print(OS); |
| 212 | else |
| 213 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 214 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 215 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 216 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 217 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 218 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 219 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 220 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 221 | case InAlloca: |
| 222 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 223 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 224 | case Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 225 | OS << "Indirect Align=" << getIndirectAlign().getQuantity() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 226 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 227 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 228 | break; |
| 229 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 230 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 231 | break; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 232 | case CoerceAndExpand: |
| 233 | OS << "CoerceAndExpand Type="; |
| 234 | getCoerceAndExpandType()->print(OS); |
| 235 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 236 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 237 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 240 | // Dynamically round a pointer up to a multiple of the given alignment. |
| 241 | static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, |
| 242 | llvm::Value *Ptr, |
| 243 | CharUnits Align) { |
| 244 | llvm::Value *PtrAsInt = Ptr; |
| 245 | // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; |
| 246 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 247 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 248 | llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); |
| 249 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 250 | llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); |
| 251 | PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, |
| 252 | Ptr->getType(), |
| 253 | Ptr->getName() + ".aligned"); |
| 254 | return PtrAsInt; |
| 255 | } |
| 256 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 257 | /// Emit va_arg for a platform using the common void* representation, |
| 258 | /// where arguments are simply emitted in an array of slots on the stack. |
| 259 | /// |
| 260 | /// This version implements the core direct-value passing rules. |
| 261 | /// |
| 262 | /// \param SlotSize - The size and alignment of a stack slot. |
| 263 | /// Each argument will be allocated to a multiple of this number of |
| 264 | /// slots, and all the slots will be aligned to this value. |
| 265 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 266 | /// an argument type with an alignment greater than the slot size |
| 267 | /// will be emitted on a higher-alignment address, potentially |
| 268 | /// leaving one or more empty slots behind as padding. If this |
| 269 | /// is false, the returned address might be less-aligned than |
| 270 | /// DirectAlign. |
| 271 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 272 | Address VAListAddr, |
| 273 | llvm::Type *DirectTy, |
| 274 | CharUnits DirectSize, |
| 275 | CharUnits DirectAlign, |
| 276 | CharUnits SlotSize, |
| 277 | bool AllowHigherAlign) { |
| 278 | // Cast the element type to i8* if necessary. Some platforms define |
| 279 | // va_list as a struct containing an i8* instead of just an i8*. |
| 280 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 281 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 282 | |
| 283 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 284 | |
| 285 | // If the CC aligns values higher than the slot size, do so if needed. |
| 286 | Address Addr = Address::invalid(); |
| 287 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 288 | Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), |
| 289 | DirectAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 290 | } else { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 291 | Addr = Address(Ptr, SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Advance the pointer past the argument, then store that back. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 295 | CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 296 | llvm::Value *NextPtr = |
| 297 | CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, |
| 298 | "argp.next"); |
| 299 | CGF.Builder.CreateStore(NextPtr, VAListAddr); |
| 300 | |
| 301 | // If the argument is smaller than a slot, and this is a big-endian |
| 302 | // target, the argument will be right-adjusted in its slot. |
Strahinja Petrovic | 515a1eb | 2016-06-24 12:12:41 +0000 | [diff] [blame] | 303 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() && |
| 304 | !DirectTy->isStructTy()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 305 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 306 | } |
| 307 | |
| 308 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 309 | return Addr; |
| 310 | } |
| 311 | |
| 312 | /// Emit va_arg for a platform using the common void* representation, |
| 313 | /// where arguments are simply emitted in an array of slots on the stack. |
| 314 | /// |
| 315 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 316 | /// \param ValueInfo - The size and alignment of this type, generally |
| 317 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 318 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 319 | /// Each argument will be allocated to a multiple of this number of |
| 320 | /// slots, and all the slots will be aligned to this value. |
| 321 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 322 | /// an argument type with an alignment greater than the slot size |
| 323 | /// will be emitted on a higher-alignment address, potentially |
| 324 | /// leaving one or more empty slots behind as padding. |
| 325 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 326 | QualType ValueTy, bool IsIndirect, |
| 327 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 328 | CharUnits SlotSizeAndAlign, |
| 329 | bool AllowHigherAlign) { |
| 330 | // The size and alignment of the value that was passed directly. |
| 331 | CharUnits DirectSize, DirectAlign; |
| 332 | if (IsIndirect) { |
| 333 | DirectSize = CGF.getPointerSize(); |
| 334 | DirectAlign = CGF.getPointerAlign(); |
| 335 | } else { |
| 336 | DirectSize = ValueInfo.first; |
| 337 | DirectAlign = ValueInfo.second; |
| 338 | } |
| 339 | |
| 340 | // Cast the address we've calculated to the right type. |
| 341 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 342 | if (IsIndirect) |
| 343 | DirectTy = DirectTy->getPointerTo(0); |
| 344 | |
| 345 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 346 | DirectSize, DirectAlign, |
| 347 | SlotSizeAndAlign, |
| 348 | AllowHigherAlign); |
| 349 | |
| 350 | if (IsIndirect) { |
| 351 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 352 | } |
| 353 | |
| 354 | return Addr; |
| 355 | |
| 356 | } |
| 357 | |
| 358 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 359 | Address Addr1, llvm::BasicBlock *Block1, |
| 360 | Address Addr2, llvm::BasicBlock *Block2, |
| 361 | const llvm::Twine &Name = "") { |
| 362 | assert(Addr1.getType() == Addr2.getType()); |
| 363 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 364 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 365 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 366 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 367 | return Address(PHI, Align); |
| 368 | } |
| 369 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 370 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 371 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 372 | // If someone can figure out a general rule for this, that would be great. |
| 373 | // It's probably just doomed to be platform-dependent, though. |
| 374 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 375 | // Verified for: |
| 376 | // x86-64 FreeBSD, Linux, Darwin |
| 377 | // x86-32 FreeBSD, Linux, Darwin |
| 378 | // PowerPC Linux, Darwin |
| 379 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 380 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 381 | return 32; |
| 382 | } |
| 383 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 384 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 385 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 386 | // The following conventions are known to require this to be false: |
| 387 | // x86_stdcall |
| 388 | // MIPS |
| 389 | // For everything else, we just prefer false unless we opt out. |
| 390 | return false; |
| 391 | } |
| 392 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 393 | void |
| 394 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 395 | llvm::SmallString<24> &Opt) const { |
| 396 | // This assumes the user is passing a library name like "rt" instead of a |
| 397 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 398 | // dynamic. |
| 399 | Opt = "-l"; |
| 400 | Opt += Lib; |
| 401 | } |
| 402 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 403 | unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 404 | // OpenCL kernels are called via an explicit runtime API with arguments |
| 405 | // set with clSetKernelArg(), not as normal sub-functions. |
| 406 | // Return SPIR_KERNEL by default as the kernel calling convention to |
| 407 | // ensure the fingerprint is fixed such way that each OpenCL argument |
| 408 | // gets one matching argument in the produced kernel function argument |
| 409 | // list to enable feasible implementation of clSetKernelArg() with |
| 410 | // aggregates etc. In case we would use the default C calling conv here, |
| 411 | // clSetKernelArg() might break depending on the target-specific |
| 412 | // conventions; different targets might split structs passed as values |
| 413 | // to multiple function arguments etc. |
| 414 | return llvm::CallingConv::SPIR_KERNEL; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 415 | } |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 416 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 417 | llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 418 | llvm::PointerType *T, QualType QT) const { |
| 419 | return llvm::ConstantPointerNull::get(T); |
| 420 | } |
| 421 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 422 | LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 423 | const VarDecl *D) const { |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 424 | assert(!CGM.getLangOpts().OpenCL && |
| 425 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 426 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 427 | return D ? D->getType().getAddressSpace() : LangAS::Default; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 430 | llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 431 | CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr, |
| 432 | LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const { |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 433 | // Since target may map different address spaces in AST to the same address |
| 434 | // space, an address space conversion may end up as a bitcast. |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 435 | if (auto *C = dyn_cast<llvm::Constant>(Src)) |
| 436 | return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 437 | return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DestTy); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 440 | llvm::Constant * |
| 441 | TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src, |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 442 | LangAS SrcAddr, LangAS DestAddr, |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 443 | llvm::Type *DestTy) const { |
| 444 | // Since target may map different address spaces in AST to the same address |
| 445 | // space, an address space conversion may end up as a bitcast. |
| 446 | return llvm::ConstantExpr::getPointerCast(Src, DestTy); |
| 447 | } |
| 448 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 449 | llvm::SyncScope::ID |
| 450 | TargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, llvm::LLVMContext &C) const { |
| 451 | return C.getOrInsertSyncScopeID(""); /* default sync scope */ |
| 452 | } |
| 453 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 454 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 455 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 456 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 457 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 458 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 459 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 460 | if (FD->isUnnamedBitfield()) |
| 461 | return true; |
| 462 | |
| 463 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 464 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 465 | // Constant arrays of empty records count as empty, strip them off. |
| 466 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 467 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 468 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 469 | if (AT->getSize() == 0) |
| 470 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 471 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 472 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 473 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 474 | const RecordType *RT = FT->getAs<RecordType>(); |
| 475 | if (!RT) |
| 476 | return false; |
| 477 | |
| 478 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 479 | // |
| 480 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 481 | // current ABI. |
| 482 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 483 | return false; |
| 484 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 485 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 488 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 489 | /// fields. Note that a structure with a flexible array member is not |
| 490 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 491 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 492 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 493 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 494 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 495 | const RecordDecl *RD = RT->getDecl(); |
| 496 | if (RD->hasFlexibleArrayMember()) |
| 497 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 498 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 499 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 500 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 501 | for (const auto &I : CXXRD->bases()) |
| 502 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 503 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 504 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 505 | for (const auto *I : RD->fields()) |
| 506 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 507 | return false; |
| 508 | return true; |
| 509 | } |
| 510 | |
| 511 | /// isSingleElementStruct - Determine if a structure is a "single |
| 512 | /// element struct", i.e. it has exactly one non-empty field or |
| 513 | /// exactly one field which is itself a single element |
| 514 | /// struct. Structures with flexible array members are never |
| 515 | /// considered single element structs. |
| 516 | /// |
| 517 | /// \return The field declaration for the single non-empty field, if |
| 518 | /// it exists. |
| 519 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 520 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 521 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 522 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 523 | |
| 524 | const RecordDecl *RD = RT->getDecl(); |
| 525 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 526 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 527 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 528 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 529 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 530 | // If this is a C++ record, check the bases first. |
| 531 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 532 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 533 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 534 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 535 | continue; |
| 536 | |
| 537 | // If we already found an element then this isn't a single-element struct. |
| 538 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 539 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 540 | |
| 541 | // If this is non-empty and not a single element struct, the composite |
| 542 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 543 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 544 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 545 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 550 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 551 | QualType FT = FD->getType(); |
| 552 | |
| 553 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 554 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 555 | continue; |
| 556 | |
| 557 | // If we already found an element then this isn't a single-element |
| 558 | // struct. |
| 559 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 560 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 561 | |
| 562 | // Treat single element arrays as the element. |
| 563 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 564 | if (AT->getSize().getZExtValue() != 1) |
| 565 | break; |
| 566 | FT = AT->getElementType(); |
| 567 | } |
| 568 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 569 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 570 | Found = FT.getTypePtr(); |
| 571 | } else { |
| 572 | Found = isSingleElementStruct(FT, Context); |
| 573 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 574 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 578 | // We don't consider a struct a single-element struct if it has |
| 579 | // padding beyond the element type. |
| 580 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 581 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 582 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 583 | return Found; |
| 584 | } |
| 585 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 586 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 587 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 588 | const ABIArgInfo &AI) { |
| 589 | // This default implementation defers to the llvm backend's va_arg |
| 590 | // instruction. It can handle only passing arguments directly |
| 591 | // (typically only handled in the backend for primitive types), or |
| 592 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 593 | // flag has ABI impact in the callee, this implementation cannot |
| 594 | // work.) |
| 595 | |
| 596 | // Only a few cases are covered here at the moment -- those needed |
| 597 | // by the default abi. |
| 598 | llvm::Value *Val; |
| 599 | |
| 600 | if (AI.isIndirect()) { |
| 601 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 602 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 603 | assert( |
| 604 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 605 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 606 | |
| 607 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 608 | CharUnits TyAlignForABI = TyInfo.second; |
| 609 | |
| 610 | llvm::Type *BaseTy = |
| 611 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 612 | llvm::Value *Addr = |
| 613 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 614 | return Address(Addr, TyAlignForABI); |
| 615 | } else { |
| 616 | assert((AI.isDirect() || AI.isExtend()) && |
| 617 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 618 | |
| 619 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 620 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 621 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 622 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 623 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 624 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 625 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 626 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 627 | |
| 628 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 629 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 630 | CGF.Builder.CreateStore(Val, Temp); |
| 631 | return Temp; |
| 632 | } |
| 633 | } |
| 634 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 635 | /// DefaultABIInfo - The default implementation for ABI specific |
| 636 | /// details. This implementation provides information which results in |
| 637 | /// self-consistent and sensible LLVM IR generation, but does not |
| 638 | /// conform to any particular ABI. |
| 639 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 640 | public: |
| 641 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 642 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 643 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 644 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 645 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 646 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 647 | if (!getCXXABI().classifyReturnType(FI)) |
| 648 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 649 | for (auto &I : FI.arguments()) |
| 650 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 651 | } |
| 652 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 653 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 654 | QualType Ty) const override { |
| 655 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 656 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 657 | }; |
| 658 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 659 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 660 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 661 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 662 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 663 | }; |
| 664 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 665 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 666 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 667 | |
| 668 | if (isAggregateTypeForABI(Ty)) { |
| 669 | // Records with non-trivial destructors/copy-constructors should not be |
| 670 | // passed by value. |
| 671 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 672 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 673 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 674 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 675 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 677 | // Treat an enum type as its underlying type. |
| 678 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 679 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 680 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 681 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 682 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 685 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 686 | if (RetTy->isVoidType()) |
| 687 | return ABIArgInfo::getIgnore(); |
| 688 | |
| 689 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 690 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 691 | |
| 692 | // Treat an enum type as its underlying type. |
| 693 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 694 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 695 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 696 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 697 | : ABIArgInfo::getDirect()); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 700 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 701 | // WebAssembly ABI Implementation |
| 702 | // |
| 703 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 704 | //===----------------------------------------------------------------------===// |
| 705 | |
| 706 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 707 | public: |
| 708 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 709 | : DefaultABIInfo(CGT) {} |
| 710 | |
| 711 | private: |
| 712 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 713 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 714 | |
| 715 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 716 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 717 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 718 | void computeInfo(CGFunctionInfo &FI) const override { |
| 719 | if (!getCXXABI().classifyReturnType(FI)) |
| 720 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 721 | for (auto &Arg : FI.arguments()) |
| 722 | Arg.info = classifyArgumentType(Arg.type); |
| 723 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 724 | |
| 725 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 726 | QualType Ty) const override; |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 727 | }; |
| 728 | |
| 729 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 730 | public: |
| 731 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 732 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 733 | }; |
| 734 | |
| 735 | /// \brief Classify argument of given type \p Ty. |
| 736 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 737 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 738 | |
| 739 | if (isAggregateTypeForABI(Ty)) { |
| 740 | // Records with non-trivial destructors/copy-constructors should not be |
| 741 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 742 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 743 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 744 | // Ignore empty structs/unions. |
| 745 | if (isEmptyRecord(getContext(), Ty, true)) |
| 746 | return ABIArgInfo::getIgnore(); |
| 747 | // Lower single-element structs to just pass a regular value. TODO: We |
| 748 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 749 | // though watch out for things like bitfields. |
| 750 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 751 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | // Otherwise just do the default thing. |
| 755 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 756 | } |
| 757 | |
| 758 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 759 | if (isAggregateTypeForABI(RetTy)) { |
| 760 | // Records with non-trivial destructors/copy-constructors should not be |
| 761 | // returned by value. |
| 762 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 763 | // Ignore empty structs/unions. |
| 764 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 765 | return ABIArgInfo::getIgnore(); |
| 766 | // Lower single-element structs to just return a regular value. TODO: We |
| 767 | // could do reasonable-size multiple-element structs too, using |
| 768 | // ABIArgInfo::getDirect(). |
| 769 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 770 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | // Otherwise just do the default thing. |
| 775 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 776 | } |
| 777 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 778 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 779 | QualType Ty) const { |
| 780 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 781 | getContext().getTypeInfoInChars(Ty), |
| 782 | CharUnits::fromQuantity(4), |
| 783 | /*AllowHigherAlign=*/ true); |
| 784 | } |
| 785 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 786 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 787 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 788 | // |
| 789 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 790 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 791 | //===----------------------------------------------------------------------===// |
| 792 | |
| 793 | class PNaClABIInfo : public ABIInfo { |
| 794 | public: |
| 795 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 796 | |
| 797 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 798 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 799 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 800 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 801 | Address EmitVAArg(CodeGenFunction &CGF, |
| 802 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 803 | }; |
| 804 | |
| 805 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 806 | public: |
| 807 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 808 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 809 | }; |
| 810 | |
| 811 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 812 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 813 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 814 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 815 | for (auto &I : FI.arguments()) |
| 816 | I.info = classifyArgumentType(I.type); |
| 817 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 818 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 819 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 820 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 821 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 822 | // function classification. Structs get passed directly for varargs |
| 823 | // functions, through a rewriting transform in |
| 824 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 825 | // this target to actually support a va_arg instructions with an |
| 826 | // aggregate type, unlike other targets. |
| 827 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 830 | /// \brief Classify argument of given type \p Ty. |
| 831 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 832 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 833 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 834 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 835 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 836 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 837 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 838 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 839 | } else if (Ty->isFloatingType()) { |
| 840 | // Floating-point types don't go inreg. |
| 841 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 842 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 843 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 844 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 845 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 849 | if (RetTy->isVoidType()) |
| 850 | return ABIArgInfo::getIgnore(); |
| 851 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 852 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 853 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 854 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 855 | |
| 856 | // Treat an enum type as its underlying type. |
| 857 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 858 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 859 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 860 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 861 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 864 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 865 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 866 | // 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] | 867 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 868 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 869 | IRType->getScalarSizeInBits() != 64; |
| 870 | } |
| 871 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 872 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 873 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 874 | llvm::Type* Ty) { |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 875 | bool IsMMXCons = llvm::StringSwitch<bool>(Constraint) |
| 876 | .Cases("y", "&y", "^Ym", true) |
| 877 | .Default(false); |
| 878 | if (IsMMXCons && Ty->isVectorTy()) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 879 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 880 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 881 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 884 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 888 | return Ty; |
| 889 | } |
| 890 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 891 | /// Returns true if this type can be passed in SSE registers with the |
| 892 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 893 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 894 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 895 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { |
| 896 | if (BT->getKind() == BuiltinType::LongDouble) { |
| 897 | if (&Context.getTargetInfo().getLongDoubleFormat() == |
| 898 | &llvm::APFloat::x87DoubleExtended()) |
| 899 | return false; |
| 900 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 901 | return true; |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 902 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 903 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 904 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 905 | // registers specially. |
| 906 | unsigned VecSize = Context.getTypeSize(VT); |
| 907 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 908 | return true; |
| 909 | } |
| 910 | return false; |
| 911 | } |
| 912 | |
| 913 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 914 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 915 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 916 | return NumMembers <= 4; |
| 917 | } |
| 918 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 919 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 920 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 921 | auto AI = ABIArgInfo::getDirect(T); |
| 922 | AI.setInReg(true); |
| 923 | AI.setCanBeFlattened(false); |
| 924 | return AI; |
| 925 | } |
| 926 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 927 | //===----------------------------------------------------------------------===// |
| 928 | // X86-32 ABI Implementation |
| 929 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 930 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 931 | /// \brief Similar to llvm::CCState, but for Clang. |
| 932 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 933 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 934 | |
| 935 | unsigned CC; |
| 936 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 937 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 938 | }; |
| 939 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 940 | enum { |
| 941 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 942 | VectorcallMaxParamNumAsReg = 6 |
| 943 | }; |
| 944 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 945 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 946 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 947 | enum Class { |
| 948 | Integer, |
| 949 | Float |
| 950 | }; |
| 951 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 952 | static const unsigned MinABIStackAlignInBytes = 4; |
| 953 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 954 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 955 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 956 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 957 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 958 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 959 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 960 | |
| 961 | static bool isRegisterSize(unsigned Size) { |
| 962 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 963 | } |
| 964 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 965 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 966 | // FIXME: Assumes vectorcall is in use. |
| 967 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 968 | } |
| 969 | |
| 970 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 971 | uint64_t NumMembers) const override { |
| 972 | // FIXME: Assumes vectorcall is in use. |
| 973 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 974 | } |
| 975 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 976 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 977 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 978 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 979 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 980 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 981 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 982 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 983 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 984 | /// \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] | 985 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 986 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 987 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 988 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 989 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 990 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 991 | /// \brief Updates the number of available free registers, returns |
| 992 | /// true if any registers were allocated. |
| 993 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 994 | |
| 995 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 996 | bool &NeedsPadding) const; |
| 997 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 998 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 999 | bool canExpandIndirectArgument(QualType Ty) const; |
| 1000 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1001 | /// \brief Rewrite the function info so that all memory arguments use |
| 1002 | /// inalloca. |
| 1003 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 1004 | |
| 1005 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1006 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1007 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1008 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1009 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1010 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 1011 | public: |
| 1012 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1013 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1014 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1015 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1016 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1017 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1018 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1019 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1020 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1021 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 1022 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1023 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1024 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1025 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1026 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 1027 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1028 | bool asReturnValue) const override { |
| 1029 | // LLVM's x86-32 lowering currently only assigns up to three |
| 1030 | // integer registers and three fp registers. Oddly, it'll use up to |
| 1031 | // four vector registers for vectors, but those can overlap with the |
| 1032 | // scalar registers. |
| 1033 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
| 1034 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 1035 | |
| 1036 | bool isSwiftErrorInRegister() const override { |
| 1037 | // x86-32 lowering does not support passing swifterror in a register. |
| 1038 | return false; |
| 1039 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1040 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1041 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1042 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1043 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1044 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1045 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1046 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 1047 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1048 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 1049 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1050 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1051 | static bool isStructReturnInRegABI( |
| 1052 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1053 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1054 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1055 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1056 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1057 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1058 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1059 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1060 | return 4; |
| 1061 | } |
| 1062 | |
| 1063 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1064 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1065 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1066 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1067 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1068 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1069 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1070 | } |
| 1071 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1072 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1073 | std::string &Constraints, |
| 1074 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1075 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1076 | std::vector<LValue> &ResultRegDests, |
| 1077 | std::string &AsmString, |
| 1078 | unsigned NumOutputs) const override; |
| 1079 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1080 | llvm::Constant * |
| 1081 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1082 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1083 | (0x06 << 8) | // .+0x08 |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 1084 | ('v' << 16) | |
| 1085 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1086 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1087 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1088 | |
| 1089 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1090 | return "movl\t%ebp, %ebp" |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 1091 | "\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1092 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1093 | }; |
| 1094 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1095 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1096 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1097 | /// Rewrite input constraint references after adding some output constraints. |
| 1098 | /// In the case where there is one output and one input and we add one output, |
| 1099 | /// we need to replace all operand references greater than or equal to 1: |
| 1100 | /// mov $0, $1 |
| 1101 | /// mov eax, $1 |
| 1102 | /// The result will be: |
| 1103 | /// mov $0, $2 |
| 1104 | /// mov eax, $2 |
| 1105 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1106 | unsigned NumNewOuts, |
| 1107 | std::string &AsmString) { |
| 1108 | std::string Buf; |
| 1109 | llvm::raw_string_ostream OS(Buf); |
| 1110 | size_t Pos = 0; |
| 1111 | while (Pos < AsmString.size()) { |
| 1112 | size_t DollarStart = AsmString.find('$', Pos); |
| 1113 | if (DollarStart == std::string::npos) |
| 1114 | DollarStart = AsmString.size(); |
| 1115 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1116 | if (DollarEnd == std::string::npos) |
| 1117 | DollarEnd = AsmString.size(); |
| 1118 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1119 | Pos = DollarEnd; |
| 1120 | size_t NumDollars = DollarEnd - DollarStart; |
| 1121 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1122 | // We have an operand reference. |
| 1123 | size_t DigitStart = Pos; |
| 1124 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1125 | if (DigitEnd == std::string::npos) |
| 1126 | DigitEnd = AsmString.size(); |
| 1127 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1128 | unsigned OperandIndex; |
| 1129 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1130 | if (OperandIndex >= FirstIn) |
| 1131 | OperandIndex += NumNewOuts; |
| 1132 | OS << OperandIndex; |
| 1133 | } else { |
| 1134 | OS << OperandStr; |
| 1135 | } |
| 1136 | Pos = DigitEnd; |
| 1137 | } |
| 1138 | } |
| 1139 | AsmString = std::move(OS.str()); |
| 1140 | } |
| 1141 | |
| 1142 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1143 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1144 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1145 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1146 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1147 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1148 | unsigned NumOutputs) const { |
| 1149 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1150 | |
| 1151 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1152 | // larger. |
| 1153 | if (!Constraints.empty()) |
| 1154 | Constraints += ','; |
| 1155 | if (RetWidth <= 32) { |
| 1156 | Constraints += "={eax}"; |
| 1157 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1158 | } else { |
| 1159 | // Use the 'A' constraint for EAX:EDX. |
| 1160 | Constraints += "=A"; |
| 1161 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1162 | } |
| 1163 | |
| 1164 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1165 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1166 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1167 | |
| 1168 | // Coerce the integer by bitcasting the return slot pointer. |
| 1169 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1170 | CoerceTy->getPointerTo())); |
| 1171 | ResultRegDests.push_back(ReturnSlot); |
| 1172 | |
| 1173 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1174 | } |
| 1175 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1176 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1177 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1178 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1179 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1180 | uint64_t Size = Context.getTypeSize(Ty); |
| 1181 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1182 | // For i386, type must be register sized. |
| 1183 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1184 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1185 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1186 | |
| 1187 | if (Ty->isVectorType()) { |
| 1188 | // 64- and 128- bit vectors inside structures are not returned in |
| 1189 | // registers. |
| 1190 | if (Size == 64 || Size == 128) |
| 1191 | return false; |
| 1192 | |
| 1193 | return true; |
| 1194 | } |
| 1195 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1196 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1197 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1198 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1199 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1200 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1201 | return true; |
| 1202 | |
| 1203 | // Arrays are treated like records. |
| 1204 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1205 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1206 | |
| 1207 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1208 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1209 | if (!RT) return false; |
| 1210 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1211 | // FIXME: Traverse bases here too. |
| 1212 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1213 | // Structure types are passed in register if all fields would be |
| 1214 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1215 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1216 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1217 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1218 | continue; |
| 1219 | |
| 1220 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1221 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1222 | return false; |
| 1223 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1224 | return true; |
| 1225 | } |
| 1226 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1227 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1228 | // Treat complex types as the element type. |
| 1229 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1230 | Ty = CTy->getElementType(); |
| 1231 | |
| 1232 | // Check for a type which we know has a simple scalar argument-passing |
| 1233 | // convention without any padding. (We're specifically looking for 32 |
| 1234 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1235 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1236 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1237 | return false; |
| 1238 | |
| 1239 | uint64_t Size = Context.getTypeSize(Ty); |
| 1240 | return Size == 32 || Size == 64; |
| 1241 | } |
| 1242 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1243 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1244 | uint64_t &Size) { |
| 1245 | for (const auto *FD : RD->fields()) { |
| 1246 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1247 | // argument is smaller than 32-bits, expanding the struct will create |
| 1248 | // alignment padding. |
| 1249 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1250 | return false; |
| 1251 | |
| 1252 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1253 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1254 | // counts as "basic" is more complicated than what we were doing previously. |
| 1255 | if (FD->isBitField()) |
| 1256 | return false; |
| 1257 | |
| 1258 | Size += Context.getTypeSize(FD->getType()); |
| 1259 | } |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
| 1263 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1264 | uint64_t &Size) { |
| 1265 | // Don't do this if there are any non-empty bases. |
| 1266 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1267 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1268 | Size)) |
| 1269 | return false; |
| 1270 | } |
| 1271 | if (!addFieldSizes(Context, RD, Size)) |
| 1272 | return false; |
| 1273 | return true; |
| 1274 | } |
| 1275 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1276 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1277 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1278 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1279 | /// optimizations. |
| 1280 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1281 | // We can only expand structure types. |
| 1282 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1283 | if (!RT) |
| 1284 | return false; |
| 1285 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1286 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1287 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1288 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1289 | // On non-Windows, we have to conservatively match our old bitcode |
| 1290 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1291 | if (!CXXRD->isCLike()) |
| 1292 | return false; |
| 1293 | } else { |
| 1294 | // Don't do this for dynamic classes. |
| 1295 | if (CXXRD->isDynamicClass()) |
| 1296 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1297 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1298 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1299 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1300 | } else { |
| 1301 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1302 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | // We can do this if there was no alignment padding. |
| 1306 | return Size == getContext().getTypeSize(Ty); |
| 1307 | } |
| 1308 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1309 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1310 | // If the return value is indirect, then the hidden argument is consuming one |
| 1311 | // integer register. |
| 1312 | if (State.FreeRegs) { |
| 1313 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1314 | if (!IsMCUABI) |
| 1315 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1316 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1317 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1320 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1321 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1322 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1323 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1324 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1325 | const Type *Base = nullptr; |
| 1326 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1327 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1328 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1329 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1330 | // The LLVM struct type for such an aggregate should lower properly. |
| 1331 | return ABIArgInfo::getDirect(); |
| 1332 | } |
| 1333 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1334 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1335 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1336 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1337 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1338 | |
| 1339 | // 128-bit vectors are a special case; they are returned in |
| 1340 | // registers and we need to make sure to pick a type the LLVM |
| 1341 | // backend will like. |
| 1342 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1343 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1344 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1345 | |
| 1346 | // Always return in register if it fits in a general purpose |
| 1347 | // register, or if it is 64 bits and has a single element. |
| 1348 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1349 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1350 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1351 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1352 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1353 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1357 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1358 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1359 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1360 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1361 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1362 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1363 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1364 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1365 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1366 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1367 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1368 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1369 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1370 | // Ignore empty structs/unions. |
| 1371 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1372 | return ABIArgInfo::getIgnore(); |
| 1373 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1374 | // Small structures which are register sized are generally returned |
| 1375 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1376 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1377 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1378 | |
| 1379 | // As a special-case, if the struct is a "single-element" struct, and |
| 1380 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1381 | // floating-point register. (MSVC does not apply this special case.) |
| 1382 | // We apply a similar transformation for pointer types to improve the |
| 1383 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1384 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1385 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1386 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1387 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1388 | |
| 1389 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1390 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1391 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1394 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1395 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1396 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1397 | // Treat an enum type as its underlying type. |
| 1398 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1399 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1400 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1401 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 1402 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1405 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1406 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1407 | } |
| 1408 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1409 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1410 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1411 | if (!RT) |
| 1412 | return 0; |
| 1413 | const RecordDecl *RD = RT->getDecl(); |
| 1414 | |
| 1415 | // If this is a C++ record, check the bases first. |
| 1416 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1417 | for (const auto &I : CXXRD->bases()) |
| 1418 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1419 | return false; |
| 1420 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1421 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1422 | QualType FT = i->getType(); |
| 1423 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1424 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1425 | return true; |
| 1426 | |
| 1427 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1428 | return true; |
| 1429 | } |
| 1430 | |
| 1431 | return false; |
| 1432 | } |
| 1433 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1434 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1435 | unsigned Align) const { |
| 1436 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1437 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1438 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1439 | return 0; // Use default alignment. |
| 1440 | |
| 1441 | // On non-Darwin, the stack type alignment is always 4. |
| 1442 | if (!IsDarwinVectorABI) { |
| 1443 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1444 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1445 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1446 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1447 | // 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] | 1448 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1449 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1450 | return 16; |
| 1451 | |
| 1452 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1455 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1456 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1457 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1458 | if (State.FreeRegs) { |
| 1459 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1460 | if (!IsMCUABI) |
| 1461 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1462 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1463 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1464 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1465 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1466 | // Compute the byval alignment. |
| 1467 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1468 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1469 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1470 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1471 | |
| 1472 | // If the stack alignment is less than the type alignment, realign the |
| 1473 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1474 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1475 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1476 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1479 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1480 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1481 | if (!T) |
| 1482 | T = Ty.getTypePtr(); |
| 1483 | |
| 1484 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1485 | BuiltinType::Kind K = BT->getKind(); |
| 1486 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1487 | return Float; |
| 1488 | } |
| 1489 | return Integer; |
| 1490 | } |
| 1491 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1492 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1493 | if (!IsSoftFloatABI) { |
| 1494 | Class C = classify(Ty); |
| 1495 | if (C == Float) |
| 1496 | return false; |
| 1497 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1498 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1499 | unsigned Size = getContext().getTypeSize(Ty); |
| 1500 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1501 | |
| 1502 | if (SizeInRegs == 0) |
| 1503 | return false; |
| 1504 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1505 | if (!IsMCUABI) { |
| 1506 | if (SizeInRegs > State.FreeRegs) { |
| 1507 | State.FreeRegs = 0; |
| 1508 | return false; |
| 1509 | } |
| 1510 | } else { |
| 1511 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1512 | // earlier parameters that are passed on the stack. Also, |
| 1513 | // it does not allow passing >8-byte structs in-register, |
| 1514 | // even if there are 3 free registers available. |
| 1515 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1516 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1517 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1518 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1519 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1520 | return true; |
| 1521 | } |
| 1522 | |
| 1523 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
| 1524 | bool &InReg, |
| 1525 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1526 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1527 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1528 | // (HFAs) have already been dealt with at this point. |
| 1529 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1530 | return false; |
| 1531 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1532 | NeedsPadding = false; |
| 1533 | InReg = !IsMCUABI; |
| 1534 | |
| 1535 | if (!updateFreeRegs(Ty, State)) |
| 1536 | return false; |
| 1537 | |
| 1538 | if (IsMCUABI) |
| 1539 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1540 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1541 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1542 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1543 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1544 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1545 | NeedsPadding = true; |
| 1546 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1547 | return false; |
| 1548 | } |
| 1549 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1550 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1553 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1554 | if (!updateFreeRegs(Ty, State)) |
| 1555 | return false; |
| 1556 | |
| 1557 | if (IsMCUABI) |
| 1558 | return false; |
| 1559 | |
| 1560 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1561 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1562 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1563 | if (getContext().getTypeSize(Ty) > 32) |
| 1564 | return false; |
| 1565 | |
| 1566 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
| 1567 | Ty->isReferenceType()); |
| 1568 | } |
| 1569 | |
| 1570 | return true; |
| 1571 | } |
| 1572 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1573 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1574 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1575 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1576 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1577 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1578 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1579 | // Check with the C++ ABI first. |
| 1580 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1581 | if (RT) { |
| 1582 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1583 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1584 | return getIndirectResult(Ty, false, State); |
| 1585 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1586 | // The field index doesn't matter, we'll fix it up later. |
| 1587 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1588 | } |
| 1589 | } |
| 1590 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1591 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1592 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1593 | const Type *Base = nullptr; |
| 1594 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1595 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1596 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1597 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1598 | if (State.FreeSSERegs >= NumElts) { |
| 1599 | State.FreeSSERegs -= NumElts; |
| 1600 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1601 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1602 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1603 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1604 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1608 | // Structures with flexible arrays are always indirect. |
| 1609 | // FIXME: This should not be byval! |
| 1610 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1611 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1612 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1613 | // Ignore empty structs/unions on non-Windows. |
| 1614 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1615 | return ABIArgInfo::getIgnore(); |
| 1616 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1617 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1618 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1619 | bool NeedsPadding = false; |
| 1620 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1621 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1622 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1623 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1624 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1625 | if (InReg) |
| 1626 | return ABIArgInfo::getDirectInReg(Result); |
| 1627 | else |
| 1628 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1629 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1630 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1631 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1632 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1633 | // of those arguments will match the struct. This is important because the |
| 1634 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1635 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1636 | // Don't do this for the MCU if there are still free integer registers |
| 1637 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1638 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1639 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1640 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1641 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1642 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1643 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1644 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1645 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1646 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1649 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1650 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1651 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1652 | if (IsDarwinVectorABI) { |
| 1653 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1654 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1655 | (Size == 64 && VT->getNumElements() == 1)) |
| 1656 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1657 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1658 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1659 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1660 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1661 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1662 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1663 | return ABIArgInfo::getDirect(); |
| 1664 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1665 | |
| 1666 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1667 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1668 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1669 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1670 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1671 | |
| 1672 | if (Ty->isPromotableIntegerType()) { |
| 1673 | if (InReg) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1674 | return ABIArgInfo::getExtendInReg(Ty); |
| 1675 | return ABIArgInfo::getExtend(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1676 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1677 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1678 | if (InReg) |
| 1679 | return ABIArgInfo::getDirectInReg(); |
| 1680 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1683 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1684 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1685 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1686 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1687 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1688 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1689 | // registers. |
| 1690 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1691 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1692 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1693 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1694 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1695 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1696 | // First pass do all the vector types. |
| 1697 | const Type *Base = nullptr; |
| 1698 | uint64_t NumElts = 0; |
| 1699 | const QualType& Ty = I.type; |
| 1700 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1701 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1702 | if (State.FreeSSERegs >= NumElts) { |
| 1703 | State.FreeSSERegs -= NumElts; |
| 1704 | I.info = ABIArgInfo::getDirect(); |
| 1705 | } else { |
| 1706 | I.info = classifyArgumentType(Ty, State); |
| 1707 | } |
| 1708 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1709 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1710 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1711 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1712 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1713 | // Second pass, do the rest! |
| 1714 | const Type *Base = nullptr; |
| 1715 | uint64_t NumElts = 0; |
| 1716 | const QualType& Ty = I.type; |
| 1717 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1718 | |
| 1719 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1720 | // Assign true HVAs (non vector/native FP types). |
| 1721 | if (State.FreeSSERegs >= NumElts) { |
| 1722 | State.FreeSSERegs -= NumElts; |
| 1723 | I.info = getDirectX86Hva(); |
| 1724 | } else { |
| 1725 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1726 | } |
| 1727 | } else if (!IsHva) { |
| 1728 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1729 | I.info = classifyArgumentType(Ty, State); |
| 1730 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1731 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1735 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1736 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1737 | if (IsMCUABI) |
| 1738 | State.FreeRegs = 3; |
| 1739 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1740 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1741 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1742 | State.FreeRegs = 2; |
| 1743 | State.FreeSSERegs = 6; |
| 1744 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1745 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1746 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1747 | State.FreeRegs = 5; |
| 1748 | State.FreeSSERegs = 8; |
| 1749 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1750 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1751 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1752 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1753 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1754 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1755 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1756 | // return value was sret and put it in a register ourselves if appropriate. |
| 1757 | if (State.FreeRegs) { |
| 1758 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1759 | if (!IsMCUABI) |
| 1760 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1761 | } |
| 1762 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1763 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1764 | // The chain argument effectively gives us another free register. |
| 1765 | if (FI.isChainCall()) |
| 1766 | ++State.FreeRegs; |
| 1767 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1768 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1769 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1770 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1771 | } else { |
| 1772 | // If not vectorcall, revert to normal behavior. |
| 1773 | for (auto &I : FI.arguments()) { |
| 1774 | I.info = classifyArgumentType(I.type, State); |
| 1775 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1776 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1780 | // all the memory arguments to use inalloca. |
| 1781 | if (UsedInAlloca) |
| 1782 | rewriteWithInAlloca(FI); |
| 1783 | } |
| 1784 | |
| 1785 | void |
| 1786 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1787 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1788 | QualType Type) const { |
| 1789 | // Arguments are always 4-byte-aligned. |
| 1790 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1791 | |
| 1792 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1793 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1794 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1795 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1796 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1797 | // Insert padding bytes to respect alignment. |
| 1798 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1799 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1800 | if (StackOffset != FieldEnd) { |
| 1801 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1802 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1803 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1804 | FrameFields.push_back(Ty); |
| 1805 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1808 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1809 | // Leave ignored and inreg arguments alone. |
| 1810 | switch (Info.getKind()) { |
| 1811 | case ABIArgInfo::InAlloca: |
| 1812 | return true; |
| 1813 | case ABIArgInfo::Indirect: |
| 1814 | assert(Info.getIndirectByVal()); |
| 1815 | return true; |
| 1816 | case ABIArgInfo::Ignore: |
| 1817 | return false; |
| 1818 | case ABIArgInfo::Direct: |
| 1819 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1820 | if (Info.getInReg()) |
| 1821 | return false; |
| 1822 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1823 | case ABIArgInfo::Expand: |
| 1824 | case ABIArgInfo::CoerceAndExpand: |
| 1825 | // These are aggregate types which are never passed in registers when |
| 1826 | // inalloca is involved. |
| 1827 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1828 | } |
| 1829 | llvm_unreachable("invalid enum"); |
| 1830 | } |
| 1831 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1832 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1833 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1834 | |
| 1835 | // Build a packed struct type for all of the arguments in memory. |
| 1836 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1837 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1838 | // The stack alignment is always 4. |
| 1839 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1840 | |
| 1841 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1842 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1843 | |
| 1844 | // Put 'this' into the struct before 'sret', if necessary. |
| 1845 | bool IsThisCall = |
| 1846 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1847 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1848 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1849 | isArgInAlloca(I->info)) { |
| 1850 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1851 | ++I; |
| 1852 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1853 | |
| 1854 | // 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] | 1855 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1856 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1857 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1858 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1859 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1863 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1864 | ++I; |
| 1865 | |
| 1866 | // Put arguments passed in memory into the struct. |
| 1867 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1868 | if (isArgInAlloca(I->info)) |
| 1869 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1873 | /*isPacked=*/true), |
| 1874 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1877 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1878 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1879 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1880 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1881 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1882 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1883 | // |
| 1884 | // Just messing with TypeInfo like this works because we never pass |
| 1885 | // anything indirectly. |
| 1886 | TypeInfo.second = CharUnits::fromQuantity( |
| 1887 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
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 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1890 | TypeInfo, CharUnits::fromQuantity(4), |
| 1891 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1894 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1895 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1896 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1897 | |
| 1898 | switch (Opts.getStructReturnConvention()) { |
| 1899 | case CodeGenOptions::SRCK_Default: |
| 1900 | break; |
| 1901 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1902 | return false; |
| 1903 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1904 | return true; |
| 1905 | } |
| 1906 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1907 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1908 | return true; |
| 1909 | |
| 1910 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1911 | case llvm::Triple::DragonFly: |
| 1912 | case llvm::Triple::FreeBSD: |
| 1913 | case llvm::Triple::OpenBSD: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1914 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1915 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1916 | default: |
| 1917 | return false; |
| 1918 | } |
| 1919 | } |
| 1920 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1921 | void X86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1922 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 1923 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1924 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1925 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1926 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1927 | // Get the LLVM function. |
| 1928 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1929 | |
| 1930 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1931 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1932 | B.addStackAlignmentAttr(16); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 1933 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1934 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1935 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1936 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1937 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1938 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1939 | } |
| 1940 | } |
| 1941 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1942 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1943 | CodeGen::CodeGenFunction &CGF, |
| 1944 | llvm::Value *Address) const { |
| 1945 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1946 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1947 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1948 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1949 | // 0-7 are the eight integer registers; the order is different |
| 1950 | // on Darwin (for EH), but the range is the same. |
| 1951 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1952 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1953 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1954 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1955 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1956 | // These have size 16, which is sizeof(long double) on |
| 1957 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1958 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1959 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1960 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1961 | } else { |
| 1962 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1963 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1964 | Builder.CreateAlignedStore( |
| 1965 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1966 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1967 | |
| 1968 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1969 | // These have size 12, which is sizeof(long double) on |
| 1970 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1971 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1972 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1973 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1974 | |
| 1975 | return false; |
| 1976 | } |
| 1977 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1978 | //===----------------------------------------------------------------------===// |
| 1979 | // X86-64 ABI Implementation |
| 1980 | //===----------------------------------------------------------------------===// |
| 1981 | |
| 1982 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1983 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1984 | /// The AVX ABI level for X86 targets. |
| 1985 | enum class X86AVXABILevel { |
| 1986 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1987 | AVX, |
| 1988 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1989 | }; |
| 1990 | |
| 1991 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1992 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1993 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1994 | case X86AVXABILevel::AVX512: |
| 1995 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1996 | case X86AVXABILevel::AVX: |
| 1997 | return 256; |
| 1998 | case X86AVXABILevel::None: |
| 1999 | return 128; |
| 2000 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 2001 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2004 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2005 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2006 | enum Class { |
| 2007 | Integer = 0, |
| 2008 | SSE, |
| 2009 | SSEUp, |
| 2010 | X87, |
| 2011 | X87Up, |
| 2012 | ComplexX87, |
| 2013 | NoClass, |
| 2014 | Memory |
| 2015 | }; |
| 2016 | |
| 2017 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2018 | /// |
| 2019 | /// Merge an accumulating classification \arg Accum with a field |
| 2020 | /// classification \arg Field. |
| 2021 | /// |
| 2022 | /// \param Accum - The accumulating classification. This should |
| 2023 | /// always be either NoClass or the result of a previous merge |
| 2024 | /// call. In addition, this should never be Memory (the caller |
| 2025 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2026 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2027 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2028 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2029 | /// |
| 2030 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2031 | /// final MEMORY or SSE classes when necessary. |
| 2032 | /// |
| 2033 | /// \param AggregateSize - The size of the current aggregate in |
| 2034 | /// the classification process. |
| 2035 | /// |
| 2036 | /// \param Lo - The classification for the parts of the type |
| 2037 | /// residing in the low word of the containing object. |
| 2038 | /// |
| 2039 | /// \param Hi - The classification for the parts of the type |
| 2040 | /// residing in the higher words of the containing object. |
| 2041 | /// |
| 2042 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2043 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2044 | /// classify - Determine the x86_64 register classes in which the |
| 2045 | /// given type T should be passed. |
| 2046 | /// |
| 2047 | /// \param Lo - The classification for the parts of the type |
| 2048 | /// residing in the low word of the containing object. |
| 2049 | /// |
| 2050 | /// \param Hi - The classification for the parts of the type |
| 2051 | /// residing in the high word of the containing object. |
| 2052 | /// |
| 2053 | /// \param OffsetBase - The bit offset of this type in the |
| 2054 | /// containing object. Some parameters are classified different |
| 2055 | /// depending on whether they straddle an eightbyte boundary. |
| 2056 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2057 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2058 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2059 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2060 | /// If a word is unused its result will be NoClass; if a type should |
| 2061 | /// be passed in Memory then at least the classification of \arg Lo |
| 2062 | /// will be Memory. |
| 2063 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2064 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2065 | /// |
| 2066 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2067 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2068 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2069 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2070 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2071 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2072 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2073 | unsigned IROffset, QualType SourceTy, |
| 2074 | unsigned SourceOffset) const; |
| 2075 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2076 | unsigned IROffset, QualType SourceTy, |
| 2077 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2078 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2079 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2080 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2081 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2082 | |
| 2083 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2084 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2085 | /// |
| 2086 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2087 | /// available. |
| 2088 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2089 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2090 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2091 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2092 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2093 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2094 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2095 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2096 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2097 | unsigned &NeededSSE) const; |
| 2098 | |
| 2099 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2100 | unsigned &NeededSSE) const; |
| 2101 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2102 | bool IsIllegalVectorType(QualType Ty) const; |
| 2103 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2104 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2105 | /// unfortunately in ways that were not always consistent with |
| 2106 | /// certain previous compilers. In particular, platforms which |
| 2107 | /// required strict binary compatibility with older versions of GCC |
| 2108 | /// may need to exempt themselves. |
| 2109 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2110 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2113 | /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to |
| 2114 | /// classify it as INTEGER (for compatibility with older clang compilers). |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2115 | bool classifyIntegerMMXAsSSE() const { |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2116 | // Clang <= 3.8 did not do this. |
| 2117 | if (getCodeGenOpts().getClangABICompat() <= |
| 2118 | CodeGenOptions::ClangABI::Ver3_8) |
| 2119 | return false; |
| 2120 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2121 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2122 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2123 | return false; |
| 2124 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2125 | return false; |
| 2126 | return true; |
| 2127 | } |
| 2128 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2129 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2130 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2131 | // 64-bit hardware. |
| 2132 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2133 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2134 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2135 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2136 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2137 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2138 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2139 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2140 | bool isPassedUsingAVXType(QualType type) const { |
| 2141 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2142 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2143 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2144 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2145 | if (info.isDirect()) { |
| 2146 | llvm::Type *ty = info.getCoerceToType(); |
| 2147 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2148 | return (vectorTy->getBitWidth() > 128); |
| 2149 | } |
| 2150 | return false; |
| 2151 | } |
| 2152 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2153 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2154 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2155 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2156 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2157 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2158 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2159 | |
| 2160 | bool has64BitPointers() const { |
| 2161 | return Has64BitPointers; |
| 2162 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2163 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2164 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2165 | bool asReturnValue) const override { |
| 2166 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2167 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2168 | bool isSwiftErrorInRegister() const override { |
| 2169 | return true; |
| 2170 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2171 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2172 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2173 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2174 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2175 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2176 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2177 | : SwiftABIInfo(CGT), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2178 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2179 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2180 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2181 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2182 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2183 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2184 | |
| 2185 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2186 | // FIXME: Assumes vectorcall is in use. |
| 2187 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2188 | } |
| 2189 | |
| 2190 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2191 | uint64_t NumMembers) const override { |
| 2192 | // FIXME: Assumes vectorcall is in use. |
| 2193 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2194 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2195 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2196 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars, |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2197 | bool asReturnValue) const override { |
| 2198 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2199 | } |
| 2200 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2201 | bool isSwiftErrorInRegister() const override { |
| 2202 | return true; |
| 2203 | } |
| 2204 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2205 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2206 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2207 | bool IsVectorCall, bool IsRegCall) const; |
| 2208 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2209 | const ABIArgInfo ¤t) const; |
| 2210 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2211 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2212 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2213 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2214 | }; |
| 2215 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2216 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2217 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2218 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2219 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2220 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2221 | const X86_64ABIInfo &getABIInfo() const { |
| 2222 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2223 | } |
| 2224 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2225 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2226 | return 7; |
| 2227 | } |
| 2228 | |
| 2229 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2230 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2231 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2232 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2233 | // 0-15 are the 16 integer registers. |
| 2234 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2235 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2236 | return false; |
| 2237 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2238 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2239 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2240 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2241 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2242 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2243 | } |
| 2244 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2245 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2246 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2247 | // The default CC on x86-64 sets %al to the number of SSA |
| 2248 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2249 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2250 | // that when AVX types are involved: the ABI explicitly states it is |
| 2251 | // undefined, and it doesn't work in practice because of how the ABI |
| 2252 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2253 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2254 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2255 | for (CallArgList::const_iterator |
| 2256 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2257 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2258 | HasAVXType = true; |
| 2259 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2260 | } |
| 2261 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2262 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2263 | if (!HasAVXType) |
| 2264 | return true; |
| 2265 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2266 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2267 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2270 | llvm::Constant * |
| 2271 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 2272 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 2273 | (0x06 << 8) | // .+0x08 |
| 2274 | ('v' << 16) | |
| 2275 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2276 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2277 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2278 | |
| 2279 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2280 | CodeGen::CodeGenModule &CGM) const override { |
| 2281 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2282 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2283 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2284 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 2285 | // Get the LLVM function. |
| 2286 | auto *Fn = cast<llvm::Function>(GV); |
| 2287 | |
| 2288 | // Now add the 'alignstack' attribute with a value of 16. |
| 2289 | llvm::AttrBuilder B; |
| 2290 | B.addStackAlignmentAttr(16); |
| 2291 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 2292 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2293 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2294 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2295 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2296 | } |
| 2297 | } |
| 2298 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2299 | }; |
| 2300 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2301 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2302 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2303 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2304 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2305 | |
| 2306 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 2307 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2308 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 2309 | // If the argument contains a space, enclose it in quotes. |
| 2310 | if (Lib.find(" ") != StringRef::npos) |
| 2311 | Opt += "\"" + Lib.str() + "\""; |
| 2312 | else |
| 2313 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2314 | } |
| 2315 | }; |
| 2316 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2317 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2318 | // If the argument does not end in .lib, automatically add the suffix. |
| 2319 | // If the argument contains a space, enclose it in quotes. |
| 2320 | // This matches the behavior of MSVC. |
| 2321 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2322 | std::string ArgStr = Quote ? "\"" : ""; |
| 2323 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 2324 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2325 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2326 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2327 | return ArgStr; |
| 2328 | } |
| 2329 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2330 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2331 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2332 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2333 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2334 | unsigned NumRegisterParameters) |
| 2335 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2336 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2337 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2338 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2339 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2340 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2341 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2342 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2343 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2344 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2345 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2346 | |
| 2347 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2348 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2349 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2350 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2351 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2352 | }; |
| 2353 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2354 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 2355 | llvm::GlobalValue *GV, |
| 2356 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 2357 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2358 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 2359 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2360 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2361 | Fn->addFnAttr("stack-probe-size", |
| 2362 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2363 | } |
| 2364 | } |
| 2365 | } |
| 2366 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2367 | void WinX86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2368 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2369 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2370 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2371 | return; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2372 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2373 | } |
| 2374 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2375 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2376 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2377 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2378 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2379 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2380 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2381 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2382 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2383 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2384 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2385 | return 7; |
| 2386 | } |
| 2387 | |
| 2388 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2389 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2390 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2391 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2392 | // 0-15 are the 16 integer registers. |
| 2393 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2394 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2395 | return false; |
| 2396 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2397 | |
| 2398 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2399 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2400 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2401 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2402 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2403 | |
| 2404 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2405 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2406 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2407 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2408 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2409 | }; |
| 2410 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2411 | void WinX86_64TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2412 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2413 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2414 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2415 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2416 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2417 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 2418 | // Get the LLVM function. |
| 2419 | auto *Fn = cast<llvm::Function>(GV); |
| 2420 | |
| 2421 | // Now add the 'alignstack' attribute with a value of 16. |
| 2422 | llvm::AttrBuilder B; |
| 2423 | B.addStackAlignmentAttr(16); |
| 2424 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 2425 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2426 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2427 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2428 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2429 | } |
| 2430 | } |
| 2431 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2432 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2433 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2434 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2435 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2436 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2437 | Class &Hi) const { |
| 2438 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2439 | // |
| 2440 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2441 | // memory. |
| 2442 | // |
| 2443 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2444 | // memory. |
| 2445 | // |
| 2446 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2447 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2448 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2449 | // ABI working for processors that don't support the __m256 type. |
| 2450 | // |
| 2451 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2452 | // |
| 2453 | // Some of these are enforced by the merging logic. Others can arise |
| 2454 | // only with unions; for example: |
| 2455 | // union { _Complex double; unsigned; } |
| 2456 | // |
| 2457 | // Note that clauses (b) and (c) were added in 0.98. |
| 2458 | // |
| 2459 | if (Hi == Memory) |
| 2460 | Lo = Memory; |
| 2461 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2462 | Lo = Memory; |
| 2463 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2464 | Lo = Memory; |
| 2465 | if (Hi == SSEUp && Lo != SSE) |
| 2466 | Hi = SSE; |
| 2467 | } |
| 2468 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2469 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2470 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2471 | // classified recursively so that always two fields are |
| 2472 | // considered. The resulting class is calculated according to |
| 2473 | // the classes of the fields in the eightbyte: |
| 2474 | // |
| 2475 | // (a) If both classes are equal, this is the resulting class. |
| 2476 | // |
| 2477 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2478 | // the other class. |
| 2479 | // |
| 2480 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2481 | // class. |
| 2482 | // |
| 2483 | // (d) If one of the classes is INTEGER, the result is the |
| 2484 | // INTEGER. |
| 2485 | // |
| 2486 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2487 | // MEMORY is used as class. |
| 2488 | // |
| 2489 | // (f) Otherwise class SSE is used. |
| 2490 | |
| 2491 | // Accum should never be memory (we should have returned) or |
| 2492 | // ComplexX87 (because this cannot be passed in a structure). |
| 2493 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2494 | "Invalid accumulated classification during merge."); |
| 2495 | if (Accum == Field || Field == NoClass) |
| 2496 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2497 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2498 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2499 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2500 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2501 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2502 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2503 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2504 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2505 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2506 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2509 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2510 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2511 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2512 | // Class pairs with appropriate constructor methods for the various |
| 2513 | // situations. |
| 2514 | |
| 2515 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2516 | // shouldn't be passed in registers for example, so there is no chance they |
| 2517 | // can straddle an eightbyte. Verify & simplify. |
| 2518 | |
| 2519 | Lo = Hi = NoClass; |
| 2520 | |
| 2521 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2522 | Current = Memory; |
| 2523 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2524 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2525 | BuiltinType::Kind k = BT->getKind(); |
| 2526 | |
| 2527 | if (k == BuiltinType::Void) { |
| 2528 | Current = NoClass; |
| 2529 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2530 | Lo = Integer; |
| 2531 | Hi = Integer; |
| 2532 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2533 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2534 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2535 | Current = SSE; |
| 2536 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2537 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2538 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2539 | Lo = SSE; |
| 2540 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2541 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2542 | Lo = X87; |
| 2543 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2544 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2545 | Current = SSE; |
| 2546 | } else |
| 2547 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2548 | } |
| 2549 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2550 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2551 | return; |
| 2552 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2553 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2554 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2555 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2556 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2557 | return; |
| 2558 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2559 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2560 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2561 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2562 | return; |
| 2563 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2564 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2565 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2566 | if (Ty->isMemberFunctionPointerType()) { |
| 2567 | if (Has64BitPointers) { |
| 2568 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2569 | // Lo and Hi now. |
| 2570 | Lo = Hi = Integer; |
| 2571 | } else { |
| 2572 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2573 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2574 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2575 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2576 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2577 | Lo = Hi = Integer; |
| 2578 | } else { |
| 2579 | Current = Integer; |
| 2580 | } |
| 2581 | } |
| 2582 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2583 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2584 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2585 | return; |
| 2586 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2587 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2588 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2589 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2590 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2591 | // gcc passes the following as integer: |
| 2592 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2593 | // 2 bytes - <2 x char>, <1 x short> |
| 2594 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2595 | Current = Integer; |
| 2596 | |
| 2597 | // If this type crosses an eightbyte boundary, it should be |
| 2598 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2599 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2600 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2601 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2602 | Hi = Lo; |
| 2603 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2604 | QualType ElementType = VT->getElementType(); |
| 2605 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2606 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2607 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2608 | return; |
| 2609 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2610 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2611 | // pass them as integer. For platforms where clang is the de facto |
| 2612 | // platform compiler, we must continue to use integer. |
| 2613 | if (!classifyIntegerMMXAsSSE() && |
| 2614 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2615 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2616 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2617 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2618 | Current = Integer; |
| 2619 | else |
| 2620 | Current = SSE; |
| 2621 | |
| 2622 | // If this type crosses an eightbyte boundary, it should be |
| 2623 | // split. |
| 2624 | if (OffsetBase && OffsetBase != 64) |
| 2625 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2626 | } else if (Size == 128 || |
| 2627 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2628 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2629 | // least significant one belongs to class SSE and all the others to class |
| 2630 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2631 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2632 | // This design isn't correct for 256-bits, but since there're no cases |
| 2633 | // where the upper parts would need to be inspected, avoid adding |
| 2634 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2635 | // |
| 2636 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2637 | // registers if they are "named", i.e. not part of the "..." of a |
| 2638 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2639 | // |
| 2640 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2641 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2642 | Lo = SSE; |
| 2643 | Hi = SSEUp; |
| 2644 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2645 | return; |
| 2646 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2647 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2648 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2649 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2650 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2651 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2652 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2653 | if (Size <= 64) |
| 2654 | Current = Integer; |
| 2655 | else if (Size <= 128) |
| 2656 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2657 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2658 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2659 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2660 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2661 | } else if (ET == getContext().LongDoubleTy) { |
| 2662 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2663 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2664 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2665 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2666 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2667 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2668 | Lo = Hi = SSE; |
| 2669 | else |
| 2670 | llvm_unreachable("unexpected long double representation!"); |
| 2671 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2672 | |
| 2673 | // If this complex type crosses an eightbyte boundary then it |
| 2674 | // should be split. |
| 2675 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2676 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2677 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2678 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2679 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2680 | return; |
| 2681 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2682 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2683 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2684 | // Arrays are treated like structures. |
| 2685 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2686 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2687 | |
| 2688 | // 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] | 2689 | // than eight eightbytes, ..., it has class MEMORY. |
| 2690 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2691 | return; |
| 2692 | |
| 2693 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2694 | // fields, it has class MEMORY. |
| 2695 | // |
| 2696 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2697 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2698 | return; |
| 2699 | |
| 2700 | // Otherwise implement simplified merge. We could be smarter about |
| 2701 | // this, but it isn't worth it and would be harder to verify. |
| 2702 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2703 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2704 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2705 | |
| 2706 | // The only case a 256-bit wide vector could be used is when the array |
| 2707 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2708 | // 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] | 2709 | // |
| 2710 | if (Size > 128 && |
| 2711 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2712 | return; |
| 2713 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2714 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2715 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2716 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2717 | Lo = merge(Lo, FieldLo); |
| 2718 | Hi = merge(Hi, FieldHi); |
| 2719 | if (Lo == Memory || Hi == Memory) |
| 2720 | break; |
| 2721 | } |
| 2722 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2723 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2724 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2725 | return; |
| 2726 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2727 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2728 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2729 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2730 | |
| 2731 | // 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] | 2732 | // than eight eightbytes, ..., it has class MEMORY. |
| 2733 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2734 | return; |
| 2735 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2736 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2737 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2738 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2739 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2740 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2741 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2742 | const RecordDecl *RD = RT->getDecl(); |
| 2743 | |
| 2744 | // Assume variable sized types are passed in memory. |
| 2745 | if (RD->hasFlexibleArrayMember()) |
| 2746 | return; |
| 2747 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2748 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2749 | |
| 2750 | // Reset Lo class, this will be recomputed. |
| 2751 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2752 | |
| 2753 | // If this is a C++ record, classify the bases first. |
| 2754 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2755 | for (const auto &I : CXXRD->bases()) { |
| 2756 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2757 | "Unexpected base class!"); |
| 2758 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2759 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2760 | |
| 2761 | // Classify this field. |
| 2762 | // |
| 2763 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2764 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2765 | // initialized to class NO_CLASS. |
| 2766 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2767 | uint64_t Offset = |
| 2768 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2769 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2770 | Lo = merge(Lo, FieldLo); |
| 2771 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2772 | if (Lo == Memory || Hi == Memory) { |
| 2773 | postMerge(Size, Lo, Hi); |
| 2774 | return; |
| 2775 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2780 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2781 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2782 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2783 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2784 | bool BitField = i->isBitField(); |
| 2785 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2786 | // Ignore padding bit-fields. |
| 2787 | if (BitField && i->isUnnamedBitfield()) |
| 2788 | continue; |
| 2789 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2790 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2791 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2792 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2793 | // The only case a 256-bit wide vector could be used is when the struct |
| 2794 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2795 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2796 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2797 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2798 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2799 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2800 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2801 | return; |
| 2802 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2803 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2804 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2805 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2806 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2807 | return; |
| 2808 | } |
| 2809 | |
| 2810 | // Classify this field. |
| 2811 | // |
| 2812 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2813 | // exceeds a single eightbyte, each is classified |
| 2814 | // separately. Each eightbyte gets initialized to class |
| 2815 | // NO_CLASS. |
| 2816 | Class FieldLo, FieldHi; |
| 2817 | |
| 2818 | // Bit-fields require special handling, they do not force the |
| 2819 | // structure to be passed in memory even if unaligned, and |
| 2820 | // therefore they can straddle an eightbyte. |
| 2821 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2822 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2823 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2824 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2825 | |
| 2826 | uint64_t EB_Lo = Offset / 64; |
| 2827 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2828 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2829 | if (EB_Lo) { |
| 2830 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2831 | FieldLo = NoClass; |
| 2832 | FieldHi = Integer; |
| 2833 | } else { |
| 2834 | FieldLo = Integer; |
| 2835 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2836 | } |
| 2837 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2838 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2839 | Lo = merge(Lo, FieldLo); |
| 2840 | Hi = merge(Hi, FieldHi); |
| 2841 | if (Lo == Memory || Hi == Memory) |
| 2842 | break; |
| 2843 | } |
| 2844 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2845 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2846 | } |
| 2847 | } |
| 2848 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2849 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2850 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2851 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2852 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2853 | // Treat an enum type as its underlying type. |
| 2854 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2855 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2856 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2857 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2858 | : ABIArgInfo::getDirect()); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2861 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2864 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2865 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2866 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2867 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2868 | if (Size <= 64 || Size > LargestVector) |
| 2869 | return true; |
| 2870 | } |
| 2871 | |
| 2872 | return false; |
| 2873 | } |
| 2874 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2875 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2876 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2877 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2878 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2879 | // |
| 2880 | // This assumption is optimistic, as there could be free registers available |
| 2881 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2882 | // the argument in the free register. This does not seem to happen currently, |
| 2883 | // but this code would be much safer if we could mark the argument with |
| 2884 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2885 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2886 | // Treat an enum type as its underlying type. |
| 2887 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2888 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2889 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2890 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2891 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2892 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2893 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2894 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2895 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2896 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2897 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2898 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2899 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2900 | |
| 2901 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2902 | // is important for good codegen. |
| 2903 | // |
| 2904 | // We do this by coercing the value into a scalar type which the backend can |
| 2905 | // handle naturally (i.e., without using byval). |
| 2906 | // |
| 2907 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2908 | // free integer registers. Doing this when there are free integer registers |
| 2909 | // would require more care, as we would have to ensure that the coerced value |
| 2910 | // did not claim the unused register. That would require either reording the |
| 2911 | // arguments to the function (so that any subsequent inreg values came first), |
| 2912 | // or only doing this optimization when there were no following arguments that |
| 2913 | // might be inreg. |
| 2914 | // |
| 2915 | // We currently expect it to be rare (particularly in well written code) for |
| 2916 | // arguments to be passed on the stack when there are still free integer |
| 2917 | // registers available (this would typically imply large structs being passed |
| 2918 | // by value), so this seems like a fair tradeoff for now. |
| 2919 | // |
| 2920 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2921 | // attributes. See PR12193. |
| 2922 | if (freeIntRegs == 0) { |
| 2923 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2924 | |
| 2925 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2926 | // type, which will end up on the stack (with alignment 8). |
| 2927 | if (Align == 8 && Size <= 64) |
| 2928 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2929 | Size)); |
| 2930 | } |
| 2931 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2932 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2935 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2936 | /// 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] | 2937 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2938 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2939 | // vectors; strip them off if present. |
| 2940 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2941 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2942 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2943 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2944 | if (isa<llvm::VectorType>(IRType) || |
| 2945 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2946 | return IRType; |
| 2947 | |
| 2948 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2949 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2950 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2951 | |
| 2952 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2953 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2954 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2957 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2958 | /// is known to either be off the end of the specified type or being in |
| 2959 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2960 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2961 | /// classification that put one of the two halves in the INTEGER class. |
| 2962 | /// |
| 2963 | /// It is conservatively correct to return false. |
| 2964 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2965 | unsigned EndBit, ASTContext &Context) { |
| 2966 | // If the bytes being queried are off the end of the type, there is no user |
| 2967 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2968 | // types that don't contain interesting padding. |
| 2969 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2970 | if (TySize <= StartBit) |
| 2971 | return true; |
| 2972 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2973 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2974 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2975 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2976 | |
| 2977 | // Check each element to see if the element overlaps with the queried range. |
| 2978 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2979 | // If the element is after the span we care about, then we're done.. |
| 2980 | unsigned EltOffset = i*EltSize; |
| 2981 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2982 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2983 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2984 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2985 | EndBit-EltOffset, Context)) |
| 2986 | return false; |
| 2987 | } |
| 2988 | // If it overlaps no elements, then it is safe to process as padding. |
| 2989 | return true; |
| 2990 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2991 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2992 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2993 | const RecordDecl *RD = RT->getDecl(); |
| 2994 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2995 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2996 | // If this is a C++ record, check the bases first. |
| 2997 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2998 | for (const auto &I : CXXRD->bases()) { |
| 2999 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3000 | "Unexpected base class!"); |
| 3001 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3002 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3003 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3004 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 3005 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3006 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3007 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3008 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3009 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3010 | EndBit-BaseOffset, Context)) |
| 3011 | return false; |
| 3012 | } |
| 3013 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3014 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3015 | // Verify that no field has data that overlaps the region of interest. Yes |
| 3016 | // this could be sped up a lot by being smarter about queried fields, |
| 3017 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 3018 | // much. |
| 3019 | unsigned idx = 0; |
| 3020 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3021 | i != e; ++i, ++idx) { |
| 3022 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3023 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3024 | // If we found a field after the region we care about, then we're done. |
| 3025 | if (FieldOffset >= EndBit) break; |
| 3026 | |
| 3027 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 3028 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 3029 | Context)) |
| 3030 | return false; |
| 3031 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3032 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3033 | // If nothing in this record overlapped the area of interest, then we're |
| 3034 | // clean. |
| 3035 | return true; |
| 3036 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3037 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3038 | return false; |
| 3039 | } |
| 3040 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3041 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3042 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3043 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3044 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3045 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3046 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3047 | // Base case if we find a float. |
| 3048 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3049 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3050 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3051 | // 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] | 3052 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3053 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3054 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3055 | IROffset -= SL->getElementOffset(Elt); |
| 3056 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3057 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3058 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3059 | // 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] | 3060 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3061 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3062 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3063 | IROffset -= IROffset/EltSize*EltSize; |
| 3064 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3065 | } |
| 3066 | |
| 3067 | return false; |
| 3068 | } |
| 3069 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3070 | |
| 3071 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3072 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3073 | llvm::Type *X86_64ABIInfo:: |
| 3074 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3075 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3076 | // 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] | 3077 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3078 | // structs that contain 3 floats. |
| 3079 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3080 | SourceOffset*8+64, getContext())) |
| 3081 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3082 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3083 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3084 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3085 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3086 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3087 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3088 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3089 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3090 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3091 | } |
| 3092 | |
| 3093 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3094 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3095 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3096 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3097 | /// 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] | 3098 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3099 | /// etc). |
| 3100 | /// |
| 3101 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3102 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3103 | /// the 8-byte value references. PrefType may be null. |
| 3104 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3105 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3106 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3107 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3108 | llvm::Type *X86_64ABIInfo:: |
| 3109 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3110 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3111 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3112 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3113 | if (IROffset == 0) { |
| 3114 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3115 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3116 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3117 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3118 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3119 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3120 | // goodness in the source type is just tail padding. This is allowed to |
| 3121 | // kick in for struct {double,int} on the int, but not on |
| 3122 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3123 | // have to do this analysis on the source type because we can't depend on |
| 3124 | // unions being lowered a specific way etc. |
| 3125 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3126 | IRType->isIntegerTy(32) || |
| 3127 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3128 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3129 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3130 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3131 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3132 | SourceOffset*8+64, getContext())) |
| 3133 | return IRType; |
| 3134 | } |
| 3135 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3136 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3137 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3138 | // 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] | 3139 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3140 | if (IROffset < SL->getSizeInBytes()) { |
| 3141 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3142 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3143 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3144 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3145 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3146 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3147 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3148 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3149 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3150 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3151 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3152 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3153 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3154 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3155 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3156 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3157 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3158 | // 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] | 3159 | unsigned TySizeInBytes = |
| 3160 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3161 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3162 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3163 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3164 | // It is always safe to classify this as an integer type up to i64 that |
| 3165 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3166 | return llvm::IntegerType::get(getVMContext(), |
| 3167 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3170 | |
| 3171 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3172 | /// be used as elements of a two register pair to pass or return, return a |
| 3173 | /// first class aggregate to represent them. For example, if the low part of |
| 3174 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3175 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3176 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3177 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3178 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3179 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3180 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3181 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3182 | // the second element at offset 8. Check for this: |
| 3183 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3184 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3185 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3186 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3187 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3188 | // To handle this, we have to increase the size of the low part so that the |
| 3189 | // second element will start at an 8 byte offset. We can't increase the size |
| 3190 | // of the second element because it might make us access off the end of the |
| 3191 | // struct. |
| 3192 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3193 | // There are usually two sorts of types the ABI generation code can produce |
| 3194 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3195 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3196 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3197 | // Promote these to a larger type. |
| 3198 | if (Lo->isFloatTy()) |
| 3199 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3200 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3201 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3202 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3203 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3204 | } |
| 3205 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3206 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3207 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3208 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3209 | // Verify that the second element is at an 8-byte offset. |
| 3210 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3211 | "Invalid x86-64 argument pair!"); |
| 3212 | return Result; |
| 3213 | } |
| 3214 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3215 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3216 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3217 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3218 | // classification algorithm. |
| 3219 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3220 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3221 | |
| 3222 | // Check some invariants. |
| 3223 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3224 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3225 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3226 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3227 | switch (Lo) { |
| 3228 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3229 | if (Hi == NoClass) |
| 3230 | return ABIArgInfo::getIgnore(); |
| 3231 | // If the low part is just padding, it takes no register, leave ResType |
| 3232 | // null. |
| 3233 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3234 | "Unknown missing lo part"); |
| 3235 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3236 | |
| 3237 | case SSEUp: |
| 3238 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3239 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3240 | |
| 3241 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3242 | // hidden argument. |
| 3243 | case Memory: |
| 3244 | return getIndirectReturnResult(RetTy); |
| 3245 | |
| 3246 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3247 | // available register of the sequence %rax, %rdx is used. |
| 3248 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3249 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3250 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3251 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3252 | // so that the parameter gets the right LLVM IR attributes. |
| 3253 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3254 | // Treat an enum type as its underlying type. |
| 3255 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3256 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3257 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3258 | if (RetTy->isIntegralOrEnumerationType() && |
| 3259 | RetTy->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3260 | return ABIArgInfo::getExtend(RetTy); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3261 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3262 | break; |
| 3263 | |
| 3264 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3265 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3266 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3267 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3268 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3269 | |
| 3270 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3271 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3272 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3273 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3274 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3275 | |
| 3276 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3277 | // part of the value is returned in %st0 and the imaginary part in |
| 3278 | // %st1. |
| 3279 | case ComplexX87: |
| 3280 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3281 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3282 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3283 | break; |
| 3284 | } |
| 3285 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3286 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3287 | switch (Hi) { |
| 3288 | // Memory was handled previously and X87 should |
| 3289 | // never occur as a hi class. |
| 3290 | case Memory: |
| 3291 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3292 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3293 | |
| 3294 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3295 | case NoClass: |
| 3296 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3297 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3298 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3299 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3300 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3301 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3302 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3303 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3304 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3305 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3306 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3307 | break; |
| 3308 | |
| 3309 | // 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] | 3310 | // is passed in the next available eightbyte chunk if the last used |
| 3311 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3312 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3313 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3314 | case SSEUp: |
| 3315 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3316 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3317 | break; |
| 3318 | |
| 3319 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3320 | // returned together with the previous X87 value in %st0. |
| 3321 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3322 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3323 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3324 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3325 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3326 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3327 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3328 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3329 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3330 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3331 | break; |
| 3332 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3333 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3334 | // 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] | 3335 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3336 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3337 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3338 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3339 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3340 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3343 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3344 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3345 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3346 | const |
| 3347 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3348 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3349 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3350 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3351 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3352 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3353 | // Check some invariants. |
| 3354 | // FIXME: Enforce these by construction. |
| 3355 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3356 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3357 | |
| 3358 | neededInt = 0; |
| 3359 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3360 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3361 | switch (Lo) { |
| 3362 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3363 | if (Hi == NoClass) |
| 3364 | return ABIArgInfo::getIgnore(); |
| 3365 | // If the low part is just padding, it takes no register, leave ResType |
| 3366 | // null. |
| 3367 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3368 | "Unknown missing lo part"); |
| 3369 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3370 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3371 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3372 | // on the stack. |
| 3373 | case Memory: |
| 3374 | |
| 3375 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3376 | // COMPLEX_X87, it is passed in memory. |
| 3377 | case X87: |
| 3378 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3379 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3380 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3381 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3382 | |
| 3383 | case SSEUp: |
| 3384 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3385 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3386 | |
| 3387 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3388 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3389 | // and %r9 is used. |
| 3390 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3391 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3392 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3393 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3394 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3395 | |
| 3396 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3397 | // so that the parameter gets the right LLVM IR attributes. |
| 3398 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3399 | // Treat an enum type as its underlying type. |
| 3400 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3401 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3402 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3403 | if (Ty->isIntegralOrEnumerationType() && |
| 3404 | Ty->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3405 | return ABIArgInfo::getExtend(Ty); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3406 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3407 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3408 | break; |
| 3409 | |
| 3410 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3411 | // available SSE register is used, the registers are taken in the |
| 3412 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3413 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3414 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3415 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3416 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3417 | break; |
| 3418 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3419 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3420 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3421 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3422 | switch (Hi) { |
| 3423 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3424 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3425 | // which is passed in memory. |
| 3426 | case Memory: |
| 3427 | case X87: |
| 3428 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3429 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3430 | |
| 3431 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3432 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3433 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3434 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3435 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3436 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3437 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3438 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3439 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3440 | break; |
| 3441 | |
| 3442 | // X87Up generally doesn't occur here (long double is passed in |
| 3443 | // memory), except in situations involving unions. |
| 3444 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3445 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3446 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
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 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3449 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3450 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3451 | ++neededSSE; |
| 3452 | break; |
| 3453 | |
| 3454 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3455 | // 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] | 3456 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3457 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3458 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3459 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3460 | break; |
| 3461 | } |
| 3462 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3463 | // If a high part was specified, merge it together with the low part. It is |
| 3464 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3465 | // first class struct aggregate with the high and low part: {low, high} |
| 3466 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3467 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3468 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3469 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3472 | ABIArgInfo |
| 3473 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3474 | unsigned &NeededSSE) const { |
| 3475 | auto RT = Ty->getAs<RecordType>(); |
| 3476 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3477 | |
| 3478 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3479 | return getIndirectReturnResult(Ty); |
| 3480 | |
| 3481 | // Sum up bases |
| 3482 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3483 | if (CXXRD->isDynamicClass()) { |
| 3484 | NeededInt = NeededSSE = 0; |
| 3485 | return getIndirectReturnResult(Ty); |
| 3486 | } |
| 3487 | |
| 3488 | for (const auto &I : CXXRD->bases()) |
| 3489 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3490 | .isIndirect()) { |
| 3491 | NeededInt = NeededSSE = 0; |
| 3492 | return getIndirectReturnResult(Ty); |
| 3493 | } |
| 3494 | } |
| 3495 | |
| 3496 | // Sum up members |
| 3497 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3498 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3499 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3500 | .isIndirect()) { |
| 3501 | NeededInt = NeededSSE = 0; |
| 3502 | return getIndirectReturnResult(Ty); |
| 3503 | } |
| 3504 | } else { |
| 3505 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3506 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3507 | LocalNeededSSE, true) |
| 3508 | .isIndirect()) { |
| 3509 | NeededInt = NeededSSE = 0; |
| 3510 | return getIndirectReturnResult(Ty); |
| 3511 | } |
| 3512 | NeededInt += LocalNeededInt; |
| 3513 | NeededSSE += LocalNeededSSE; |
| 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | return ABIArgInfo::getDirect(); |
| 3518 | } |
| 3519 | |
| 3520 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3521 | unsigned &NeededInt, |
| 3522 | unsigned &NeededSSE) const { |
| 3523 | |
| 3524 | NeededInt = 0; |
| 3525 | NeededSSE = 0; |
| 3526 | |
| 3527 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3528 | } |
| 3529 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3530 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3531 | |
Alexander Ivchenko | 4b20b3c | 2018-02-08 11:15:21 +0000 | [diff] [blame] | 3532 | const unsigned CallingConv = FI.getCallingConvention(); |
| 3533 | // It is possible to force Win64 calling convention on any x86_64 target by |
| 3534 | // using __attribute__((ms_abi)). In such case to correctly emit Win64 |
| 3535 | // compatible code delegate this call to WinX86_64ABIInfo::computeInfo. |
| 3536 | if (CallingConv == llvm::CallingConv::Win64) { |
| 3537 | WinX86_64ABIInfo Win64ABIInfo(CGT); |
| 3538 | Win64ABIInfo.computeInfo(FI); |
| 3539 | return; |
| 3540 | } |
| 3541 | |
| 3542 | bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3543 | |
| 3544 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3545 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3546 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3547 | unsigned NeededInt, NeededSSE; |
| 3548 | |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 3549 | if (!getCXXABI().classifyReturnType(FI)) { |
| 3550 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3551 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3552 | FI.getReturnInfo() = |
| 3553 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3554 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3555 | FreeIntRegs -= NeededInt; |
| 3556 | FreeSSERegs -= NeededSSE; |
| 3557 | } else { |
| 3558 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3559 | } |
| 3560 | } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) { |
| 3561 | // Complex Long Double Type is passed in Memory when Regcall |
| 3562 | // calling convention is used. |
| 3563 | const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>(); |
| 3564 | if (getContext().getCanonicalType(CT->getElementType()) == |
| 3565 | getContext().LongDoubleTy) |
| 3566 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3567 | } else |
| 3568 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3569 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3570 | |
| 3571 | // If the return value is indirect, then the hidden argument is consuming one |
| 3572 | // integer register. |
| 3573 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3574 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3575 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3576 | // The chain argument effectively gives us another free register. |
| 3577 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3578 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3579 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3580 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3581 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3582 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3583 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3584 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3585 | it != ie; ++it, ++ArgNo) { |
| 3586 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3587 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3588 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3589 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3590 | else |
| 3591 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3592 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3593 | |
| 3594 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3595 | // eightbyte of an argument, the whole argument is passed on the |
| 3596 | // stack. If registers have already been assigned for some |
| 3597 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3598 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3599 | FreeIntRegs -= NeededInt; |
| 3600 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3601 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3602 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3603 | } |
| 3604 | } |
| 3605 | } |
| 3606 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3607 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3608 | Address VAListAddr, QualType Ty) { |
| 3609 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3610 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3611 | llvm::Value *overflow_arg_area = |
| 3612 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3613 | |
| 3614 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3615 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3616 | // It isn't stated explicitly in the standard, but in practice we use |
| 3617 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3618 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3619 | if (Align > CharUnits::fromQuantity(8)) { |
| 3620 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3621 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3622 | } |
| 3623 | |
| 3624 | // 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] | 3625 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3626 | llvm::Value *Res = |
| 3627 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3628 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3629 | |
| 3630 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3631 | // l->overflow_arg_area + sizeof(type). |
| 3632 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3633 | // an 8 byte boundary. |
| 3634 | |
| 3635 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3636 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3637 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3638 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3639 | "overflow_arg_area.next"); |
| 3640 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3641 | |
| 3642 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3643 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3646 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3647 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3648 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3649 | // struct { |
| 3650 | // i32 gp_offset; |
| 3651 | // i32 fp_offset; |
| 3652 | // i8* overflow_arg_area; |
| 3653 | // i8* reg_save_area; |
| 3654 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3655 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3656 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3657 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3658 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3659 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3660 | |
| 3661 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3662 | // in the registers. If not go to step 7. |
| 3663 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3664 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3665 | |
| 3666 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3667 | // general purpose registers needed to pass type and num_fp to hold |
| 3668 | // the number of floating point registers needed. |
| 3669 | |
| 3670 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3671 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3672 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3673 | // |
| 3674 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3675 | // register save space). |
| 3676 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3677 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3678 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3679 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3680 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3681 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3682 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3683 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3684 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3685 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3686 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3690 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3691 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3692 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3693 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3694 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3695 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3696 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3697 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3698 | } |
| 3699 | |
| 3700 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3701 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3702 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3703 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3704 | |
| 3705 | // Emit code to load the value if it was passed in registers. |
| 3706 | |
| 3707 | CGF.EmitBlock(InRegBlock); |
| 3708 | |
| 3709 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3710 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3711 | // copying to a temporary location in case the parameter is passed |
| 3712 | // in different register classes or requires an alignment greater |
| 3713 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3714 | // |
| 3715 | // FIXME: This really results in shameful code when we end up needing to |
| 3716 | // collect arguments from different places; often what should result in a |
| 3717 | // simple assembling of a structure from scattered addresses has many more |
| 3718 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3719 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3720 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3721 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3722 | "reg_save_area"); |
| 3723 | |
| 3724 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3725 | if (neededInt && neededSSE) { |
| 3726 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3727 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3728 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3729 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3730 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3731 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3732 | llvm::Type *TyLo = ST->getElementType(0); |
| 3733 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3734 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3735 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3736 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3737 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3738 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3739 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3740 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3741 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3742 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3743 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3744 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3745 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3746 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3747 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3748 | CGF.Builder.CreateStore(V, |
| 3749 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3750 | |
| 3751 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3752 | V = CGF.Builder.CreateAlignedLoad( |
| 3753 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3754 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3755 | CharUnits Offset = CharUnits::fromQuantity( |
| 3756 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3757 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3758 | |
| 3759 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3760 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3761 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3762 | CharUnits::fromQuantity(8)); |
| 3763 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3764 | |
| 3765 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3766 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3767 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3768 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3769 | CharUnits TyAlign = SizeAlign.second; |
| 3770 | |
| 3771 | // Copy into a temporary if the type is more aligned than the |
| 3772 | // register save area. |
| 3773 | if (TyAlign.getQuantity() > 8) { |
| 3774 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3775 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3776 | RegAddr = Tmp; |
| 3777 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3778 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3779 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3780 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3781 | CharUnits::fromQuantity(16)); |
| 3782 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3783 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3784 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3785 | // SSE registers are spaced 16 bytes apart in the register save |
| 3786 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3787 | // The ABI isn't explicit about this, but it seems reasonable |
| 3788 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3789 | // naturally 16-byte aligned and the prologue is expected to store |
| 3790 | // all the SSE registers to the RSA. |
| 3791 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3792 | CharUnits::fromQuantity(16)); |
| 3793 | Address RegAddrHi = |
| 3794 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3795 | CharUnits::fromQuantity(16)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3796 | llvm::Type *ST = AI.canHaveCoerceToType() |
| 3797 | ? AI.getCoerceToType() |
| 3798 | : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3799 | llvm::Value *V; |
| 3800 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3801 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3802 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3803 | RegAddrLo, ST->getStructElementType(0))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3804 | CGF.Builder.CreateStore(V, |
| 3805 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3806 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3807 | RegAddrHi, ST->getStructElementType(1))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3808 | CGF.Builder.CreateStore(V, |
| 3809 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3810 | |
| 3811 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3815 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3816 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3817 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3818 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3819 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3820 | gp_offset_p); |
| 3821 | } |
| 3822 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3823 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3824 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3825 | fp_offset_p); |
| 3826 | } |
| 3827 | CGF.EmitBranch(ContBlock); |
| 3828 | |
| 3829 | // Emit code to load the value if it was passed in memory. |
| 3830 | |
| 3831 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3832 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3833 | |
| 3834 | // Return the appropriate result. |
| 3835 | |
| 3836 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3837 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3838 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3839 | return ResAddr; |
| 3840 | } |
| 3841 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3842 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3843 | QualType Ty) const { |
| 3844 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3845 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3846 | CharUnits::fromQuantity(8), |
| 3847 | /*allowHigherAlign*/ false); |
| 3848 | } |
| 3849 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3850 | ABIArgInfo |
| 3851 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3852 | const ABIArgInfo ¤t) const { |
| 3853 | // Assumes vectorCall calling convention. |
| 3854 | const Type *Base = nullptr; |
| 3855 | uint64_t NumElts = 0; |
| 3856 | |
| 3857 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3858 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3859 | FreeSSERegs -= NumElts; |
| 3860 | return getDirectX86Hva(); |
| 3861 | } |
| 3862 | return current; |
| 3863 | } |
| 3864 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3865 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3866 | bool IsReturnType, bool IsVectorCall, |
| 3867 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3868 | |
| 3869 | if (Ty->isVoidType()) |
| 3870 | return ABIArgInfo::getIgnore(); |
| 3871 | |
| 3872 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3873 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3874 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3875 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3876 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3877 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3878 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3879 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3880 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3881 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3882 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3883 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3887 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3888 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3889 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3890 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3891 | const Type *Base = nullptr; |
| 3892 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3893 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3894 | // other targets. |
| 3895 | if ((IsVectorCall || IsRegCall) && |
| 3896 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3897 | if (IsRegCall) { |
| 3898 | if (FreeSSERegs >= NumElts) { |
| 3899 | FreeSSERegs -= NumElts; |
| 3900 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3901 | return ABIArgInfo::getDirect(); |
| 3902 | return ABIArgInfo::getExpand(); |
| 3903 | } |
| 3904 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3905 | } else if (IsVectorCall) { |
| 3906 | if (FreeSSERegs >= NumElts && |
| 3907 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3908 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3909 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3910 | } else if (IsReturnType) { |
| 3911 | return ABIArgInfo::getExpand(); |
| 3912 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3913 | // HVAs are delayed and reclassified in the 2nd step. |
| 3914 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3915 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3916 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3919 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3920 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3921 | // directly. |
| 3922 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3923 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3924 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3927 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3928 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3929 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3930 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3931 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3932 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3933 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3934 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3937 | // Bool type is always extended to the ABI, other builtin types are not |
| 3938 | // extended. |
| 3939 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3940 | if (BT && BT->getKind() == BuiltinType::Bool) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3941 | return ABIArgInfo::getExtend(Ty); |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3942 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3943 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3944 | // passes them indirectly through memory. |
| 3945 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3946 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3947 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3948 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3949 | } |
| 3950 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3951 | return ABIArgInfo::getDirect(); |
| 3952 | } |
| 3953 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3954 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 3955 | unsigned FreeSSERegs, |
| 3956 | bool IsVectorCall, |
| 3957 | bool IsRegCall) const { |
| 3958 | unsigned Count = 0; |
| 3959 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 3960 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 3961 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3962 | if (Count < VectorcallMaxParamNumAsReg) |
| 3963 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 3964 | else { |
| 3965 | // Since these cannot be passed in registers, pretend no registers |
| 3966 | // are left. |
| 3967 | unsigned ZeroSSERegsAvail = 0; |
| 3968 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 3969 | IsVectorCall, IsRegCall); |
| 3970 | } |
| 3971 | ++Count; |
| 3972 | } |
| 3973 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3974 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 3975 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3976 | } |
| 3977 | } |
| 3978 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3979 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3980 | bool IsVectorCall = |
| 3981 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3982 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3983 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3984 | unsigned FreeSSERegs = 0; |
| 3985 | if (IsVectorCall) { |
| 3986 | // We can use up to 4 SSE return registers with vectorcall. |
| 3987 | FreeSSERegs = 4; |
| 3988 | } else if (IsRegCall) { |
| 3989 | // RegCall gives us 16 SSE registers. |
| 3990 | FreeSSERegs = 16; |
| 3991 | } |
| 3992 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3993 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3994 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 3995 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3996 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3997 | if (IsVectorCall) { |
| 3998 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3999 | FreeSSERegs = 6; |
| 4000 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4001 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4002 | FreeSSERegs = 16; |
| 4003 | } |
| 4004 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4005 | if (IsVectorCall) { |
| 4006 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 4007 | } else { |
| 4008 | for (auto &I : FI.arguments()) |
| 4009 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4010 | } |
| 4011 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4014 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4015 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 4016 | |
| 4017 | bool IsIndirect = false; |
| 4018 | |
| 4019 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 4020 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 4021 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 4022 | uint64_t Width = getContext().getTypeSize(Ty); |
| 4023 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 4024 | } |
| 4025 | |
| 4026 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4027 | CGF.getContext().getTypeInfoInChars(Ty), |
| 4028 | CharUnits::fromQuantity(8), |
| 4029 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4030 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4031 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4032 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4033 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4034 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 4035 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4036 | bool IsSoftFloatABI; |
| 4037 | |
| 4038 | CharUnits getParamTypeAlignment(QualType Ty) const; |
| 4039 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4040 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4041 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 4042 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4043 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4044 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4045 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4046 | }; |
| 4047 | |
| 4048 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4049 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4050 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 4051 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4052 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4053 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4054 | // This is recovered from gcc output. |
| 4055 | return 1; // r1 is the dedicated stack pointer |
| 4056 | } |
| 4057 | |
| 4058 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4059 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4060 | }; |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4061 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4062 | |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4063 | CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
| 4064 | // Complex types are passed just like their elements |
| 4065 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4066 | Ty = CTy->getElementType(); |
| 4067 | |
| 4068 | if (Ty->isVectorType()) |
| 4069 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 |
| 4070 | : 4); |
| 4071 | |
| 4072 | // For single-element float/vector structs, we consider the whole type |
| 4073 | // to have the same alignment requirements as its single element. |
| 4074 | const Type *AlignTy = nullptr; |
| 4075 | if (const Type *EltType = isSingleElementStruct(Ty, getContext())) { |
| 4076 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
| 4077 | if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) || |
| 4078 | (BT && BT->isFloatingPoint())) |
| 4079 | AlignTy = EltType; |
| 4080 | } |
| 4081 | |
| 4082 | if (AlignTy) |
| 4083 | return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4); |
| 4084 | return CharUnits::fromQuantity(4); |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4085 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4086 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4087 | // TODO: this implementation is now likely redundant with |
| 4088 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4089 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4090 | QualType Ty) const { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4091 | if (getTarget().getTriple().isOSDarwin()) { |
| 4092 | auto TI = getContext().getTypeInfoInChars(Ty); |
| 4093 | TI.second = getParamTypeAlignment(Ty); |
| 4094 | |
| 4095 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 4096 | return emitVoidPtrVAArg(CGF, VAList, Ty, |
| 4097 | classifyArgumentType(Ty).isIndirect(), TI, SlotSize, |
| 4098 | /*AllowHigherAlign=*/true); |
| 4099 | } |
| 4100 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4101 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4102 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4103 | // TODO: Implement this. For now ignore. |
| 4104 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4105 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4108 | // struct __va_list_tag { |
| 4109 | // unsigned char gpr; |
| 4110 | // unsigned char fpr; |
| 4111 | // unsigned short reserved; |
| 4112 | // void *overflow_arg_area; |
| 4113 | // void *reg_save_area; |
| 4114 | // }; |
| 4115 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4116 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4117 | bool isInt = |
| 4118 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4119 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4120 | |
| 4121 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4122 | // with the argument-lowering code. |
| 4123 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4124 | |
| 4125 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4126 | |
| 4127 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4128 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4129 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4130 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 4131 | } else { |
| 4132 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4133 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4134 | |
| 4135 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4136 | |
| 4137 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4138 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4139 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4140 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4141 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4142 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4143 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4144 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4145 | |
| 4146 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4147 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4148 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4149 | |
| 4150 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4151 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4152 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4153 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4154 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4155 | // Case 1: consume registers. |
| 4156 | Address RegAddr = Address::invalid(); |
| 4157 | { |
| 4158 | CGF.EmitBlock(UsingRegs); |
| 4159 | |
| 4160 | Address RegSaveAreaPtr = |
| 4161 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 4162 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4163 | CharUnits::fromQuantity(8)); |
| 4164 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4165 | |
| 4166 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4167 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4168 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4169 | CharUnits::fromQuantity(32)); |
| 4170 | } |
| 4171 | |
| 4172 | // Get the address of the saved value by scaling the number of |
| 4173 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4174 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4175 | llvm::Value *RegOffset = |
| 4176 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4177 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4178 | RegAddr.getPointer(), RegOffset), |
| 4179 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4180 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4181 | |
| 4182 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4183 | NumRegs = |
| 4184 | Builder.CreateAdd(NumRegs, |
| 4185 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4186 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4187 | |
| 4188 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4189 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4190 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4191 | // Case 2: consume space in the overflow area. |
| 4192 | Address MemAddr = Address::invalid(); |
| 4193 | { |
| 4194 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4195 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4196 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4197 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4198 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4199 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4200 | |
| 4201 | CharUnits Size; |
| 4202 | if (!isIndirect) { |
| 4203 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4204 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4205 | } else { |
| 4206 | Size = CGF.getPointerSize(); |
| 4207 | } |
| 4208 | |
| 4209 | Address OverflowAreaAddr = |
| 4210 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4211 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4212 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4213 | // Round up address of argument to alignment |
| 4214 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4215 | if (Align > OverflowAreaAlign) { |
| 4216 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4217 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4218 | Align); |
| 4219 | } |
| 4220 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4221 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4222 | |
| 4223 | // Increase the overflow area. |
| 4224 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4225 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4226 | CGF.EmitBranch(Cont); |
| 4227 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4228 | |
| 4229 | CGF.EmitBlock(Cont); |
| 4230 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4231 | // Merge the cases with a phi. |
| 4232 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4233 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4234 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4235 | // Load the pointer if the argument was passed indirectly. |
| 4236 | if (isIndirect) { |
| 4237 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4238 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | return Result; |
| 4242 | } |
| 4243 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4244 | bool |
| 4245 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4246 | llvm::Value *Address) const { |
| 4247 | // This is calculated from the LLVM and GCC tables and verified |
| 4248 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4249 | |
| 4250 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4251 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4252 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4253 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4254 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4255 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4256 | |
| 4257 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4258 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4259 | |
| 4260 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4261 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4262 | |
| 4263 | // 64-76 are various 4-byte special-purpose registers: |
| 4264 | // 64: mq |
| 4265 | // 65: lr |
| 4266 | // 66: ctr |
| 4267 | // 67: ap |
| 4268 | // 68-75 cr0-7 |
| 4269 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4270 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4271 | |
| 4272 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4273 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4274 | |
| 4275 | // 109: vrsave |
| 4276 | // 110: vscr |
| 4277 | // 111: spe_acc |
| 4278 | // 112: spefscr |
| 4279 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4280 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4281 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4282 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4285 | // PowerPC-64 |
| 4286 | |
| 4287 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4288 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4289 | class PPC64_SVR4_ABIInfo : public ABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4290 | public: |
| 4291 | enum ABIKind { |
| 4292 | ELFv1 = 0, |
| 4293 | ELFv2 |
| 4294 | }; |
| 4295 | |
| 4296 | private: |
| 4297 | static const unsigned GPRBits = 64; |
| 4298 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4299 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4300 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4301 | |
| 4302 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4303 | // will be passed in a QPX register. |
| 4304 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4305 | if (!HasQPX) |
| 4306 | return false; |
| 4307 | |
| 4308 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4309 | unsigned NumElements = VT->getNumElements(); |
| 4310 | if (NumElements == 1) |
| 4311 | return false; |
| 4312 | |
| 4313 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4314 | if (getContext().getTypeSize(Ty) <= 256) |
| 4315 | return true; |
| 4316 | } else if (VT->getElementType()-> |
| 4317 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4318 | if (getContext().getTypeSize(Ty) <= 128) |
| 4319 | return true; |
| 4320 | } |
| 4321 | } |
| 4322 | |
| 4323 | return false; |
| 4324 | } |
| 4325 | |
| 4326 | bool IsQPXVectorTy(QualType Ty) const { |
| 4327 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4328 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4329 | |
| 4330 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4331 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4332 | bool SoftFloatABI) |
| 4333 | : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
| 4334 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4335 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4336 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4337 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4338 | |
| 4339 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4340 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4341 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4342 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4343 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4344 | uint64_t Members) const override; |
| 4345 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4346 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4347 | // Example: For aggregate arguments that fit in a register, we could |
| 4348 | // use getDirectInReg (as is done below for structs containing a single |
| 4349 | // floating-point value) to avoid pushing them to memory on function |
| 4350 | // entry. This would require changing the logic in PPCISelLowering |
| 4351 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4352 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4353 | if (!getCXXABI().classifyReturnType(FI)) |
| 4354 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4355 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4356 | // We rely on the default argument classification for the most part. |
| 4357 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4358 | // 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] | 4359 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4360 | if (T) { |
| 4361 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4362 | if (IsQPXVectorTy(T) || |
| 4363 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4364 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4365 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4366 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4367 | continue; |
| 4368 | } |
| 4369 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4370 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4371 | } |
| 4372 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4373 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4374 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4375 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4376 | }; |
| 4377 | |
| 4378 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4379 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4380 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4381 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4382 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4383 | bool SoftFloatABI) |
| 4384 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4385 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4386 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4387 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4388 | // This is recovered from gcc output. |
| 4389 | return 1; // r1 is the dedicated stack pointer |
| 4390 | } |
| 4391 | |
| 4392 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4393 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4394 | }; |
| 4395 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4396 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4397 | public: |
| 4398 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4399 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4400 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4401 | // This is recovered from gcc output. |
| 4402 | return 1; // r1 is the dedicated stack pointer |
| 4403 | } |
| 4404 | |
| 4405 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4406 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4407 | }; |
| 4408 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4409 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4410 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4411 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4412 | // extended to 64 bits. |
| 4413 | bool |
| 4414 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4415 | // Treat an enum type as its underlying type. |
| 4416 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4417 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4418 | |
| 4419 | // Promotable integer types are required to be promoted by the ABI. |
| 4420 | if (Ty->isPromotableIntegerType()) |
| 4421 | return true; |
| 4422 | |
| 4423 | // In addition to the usual promotable integer types, we also need to |
| 4424 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4425 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4426 | switch (BT->getKind()) { |
| 4427 | case BuiltinType::Int: |
| 4428 | case BuiltinType::UInt: |
| 4429 | return true; |
| 4430 | default: |
| 4431 | break; |
| 4432 | } |
| 4433 | |
| 4434 | return false; |
| 4435 | } |
| 4436 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4437 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4438 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4439 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4440 | // Complex types are passed just like their elements. |
| 4441 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4442 | Ty = CTy->getElementType(); |
| 4443 | |
| 4444 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4445 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4446 | if (IsQPXVectorTy(Ty)) { |
| 4447 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4448 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4449 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4450 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4451 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4452 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4453 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4454 | |
| 4455 | // For single-element float/vector structs, we consider the whole type |
| 4456 | // to have the same alignment requirements as its single element. |
| 4457 | const Type *AlignAsType = nullptr; |
| 4458 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4459 | if (EltType) { |
| 4460 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4461 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4462 | getContext().getTypeSize(EltType) == 128) || |
| 4463 | (BT && BT->isFloatingPoint())) |
| 4464 | AlignAsType = EltType; |
| 4465 | } |
| 4466 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4467 | // Likewise for ELFv2 homogeneous aggregates. |
| 4468 | const Type *Base = nullptr; |
| 4469 | uint64_t Members = 0; |
| 4470 | if (!AlignAsType && Kind == ELFv2 && |
| 4471 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4472 | AlignAsType = Base; |
| 4473 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4474 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4475 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4476 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4477 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4478 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4479 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4480 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4481 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4482 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4483 | |
| 4484 | // Otherwise, we only need alignment for any aggregate type that |
| 4485 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4486 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4487 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4488 | return CharUnits::fromQuantity(32); |
| 4489 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4490 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4491 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4492 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4495 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4496 | /// aggregate. Base is set to the base element type, and Members is set |
| 4497 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4498 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4499 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4500 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4501 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4502 | if (NElements == 0) |
| 4503 | return false; |
| 4504 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4505 | return false; |
| 4506 | Members *= NElements; |
| 4507 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4508 | const RecordDecl *RD = RT->getDecl(); |
| 4509 | if (RD->hasFlexibleArrayMember()) |
| 4510 | return false; |
| 4511 | |
| 4512 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4513 | |
| 4514 | // If this is a C++ record, check the bases first. |
| 4515 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4516 | for (const auto &I : CXXRD->bases()) { |
| 4517 | // Ignore empty records. |
| 4518 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4519 | continue; |
| 4520 | |
| 4521 | uint64_t FldMembers; |
| 4522 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4523 | return false; |
| 4524 | |
| 4525 | Members += FldMembers; |
| 4526 | } |
| 4527 | } |
| 4528 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4529 | for (const auto *FD : RD->fields()) { |
| 4530 | // Ignore (non-zero arrays of) empty records. |
| 4531 | QualType FT = FD->getType(); |
| 4532 | while (const ConstantArrayType *AT = |
| 4533 | getContext().getAsConstantArrayType(FT)) { |
| 4534 | if (AT->getSize().getZExtValue() == 0) |
| 4535 | return false; |
| 4536 | FT = AT->getElementType(); |
| 4537 | } |
| 4538 | if (isEmptyRecord(getContext(), FT, true)) |
| 4539 | continue; |
| 4540 | |
| 4541 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4542 | if (getContext().getLangOpts().CPlusPlus && |
| 4543 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4544 | continue; |
| 4545 | |
| 4546 | uint64_t FldMembers; |
| 4547 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4548 | return false; |
| 4549 | |
| 4550 | Members = (RD->isUnion() ? |
| 4551 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4552 | } |
| 4553 | |
| 4554 | if (!Base) |
| 4555 | return false; |
| 4556 | |
| 4557 | // Ensure there is no padding. |
| 4558 | if (getContext().getTypeSize(Base) * Members != |
| 4559 | getContext().getTypeSize(Ty)) |
| 4560 | return false; |
| 4561 | } else { |
| 4562 | Members = 1; |
| 4563 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4564 | Members = 2; |
| 4565 | Ty = CT->getElementType(); |
| 4566 | } |
| 4567 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4568 | // Most ABIs only support float, double, and some vector type widths. |
| 4569 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4570 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4571 | |
| 4572 | // The base type must be the same for all members. Types that |
| 4573 | // agree in both total size and mode (float vs. vector) are |
| 4574 | // treated as being equivalent here. |
| 4575 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4576 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4577 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4578 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4579 | // so make sure to widen it explicitly. |
| 4580 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4581 | QualType EltTy = VT->getElementType(); |
| 4582 | unsigned NumElements = |
| 4583 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4584 | Base = getContext() |
| 4585 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4586 | .getTypePtr(); |
| 4587 | } |
| 4588 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4589 | |
| 4590 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4591 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4592 | return false; |
| 4593 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4594 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4595 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4596 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4597 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4598 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4599 | // double, long double, or 128-bit vectors. |
| 4600 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4601 | if (BT->getKind() == BuiltinType::Float || |
| 4602 | BT->getKind() == BuiltinType::Double || |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4603 | BT->getKind() == BuiltinType::LongDouble) { |
| 4604 | if (IsSoftFloatABI) |
| 4605 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4606 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4607 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4608 | } |
| 4609 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4610 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4611 | return true; |
| 4612 | } |
| 4613 | return false; |
| 4614 | } |
| 4615 | |
| 4616 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4617 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4618 | // Vector types require one register, floating point types require one |
| 4619 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4620 | uint32_t NumRegs = |
| 4621 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4622 | |
| 4623 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4624 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4625 | } |
| 4626 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4627 | ABIArgInfo |
| 4628 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4629 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4630 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4631 | if (Ty->isAnyComplexType()) |
| 4632 | return ABIArgInfo::getDirect(); |
| 4633 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4634 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4635 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4636 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4637 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4638 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4639 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4640 | else if (Size < 128) { |
| 4641 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4642 | return ABIArgInfo::getDirect(CoerceTy); |
| 4643 | } |
| 4644 | } |
| 4645 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4646 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4647 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4648 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4649 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4650 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4651 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4652 | |
| 4653 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4654 | const Type *Base = nullptr; |
| 4655 | uint64_t Members = 0; |
| 4656 | if (Kind == ELFv2 && |
| 4657 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4658 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4659 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4660 | return ABIArgInfo::getDirect(CoerceTy); |
| 4661 | } |
| 4662 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4663 | // If an aggregate may end up fully in registers, we do not |
| 4664 | // use the ByVal method, but pass the aggregate as array. |
| 4665 | // This is usually beneficial since we avoid forcing the |
| 4666 | // back-end to store the argument to memory. |
| 4667 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4668 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4669 | llvm::Type *CoerceTy; |
| 4670 | |
| 4671 | // Types up to 8 bytes are passed as integer type (which will be |
| 4672 | // properly aligned in the argument save area doubleword). |
| 4673 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4674 | CoerceTy = |
| 4675 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4676 | // Larger types are passed as arrays, with the base type selected |
| 4677 | // according to the required alignment in the save area. |
| 4678 | else { |
| 4679 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4680 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4681 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4682 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4683 | } |
| 4684 | |
| 4685 | return ABIArgInfo::getDirect(CoerceTy); |
| 4686 | } |
| 4687 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4688 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4689 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4690 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4691 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4692 | } |
| 4693 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4694 | return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) |
| 4695 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
| 4698 | ABIArgInfo |
| 4699 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4700 | if (RetTy->isVoidType()) |
| 4701 | return ABIArgInfo::getIgnore(); |
| 4702 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4703 | if (RetTy->isAnyComplexType()) |
| 4704 | return ABIArgInfo::getDirect(); |
| 4705 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4706 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4707 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4708 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4709 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4710 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4711 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4712 | else if (Size < 128) { |
| 4713 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4714 | return ABIArgInfo::getDirect(CoerceTy); |
| 4715 | } |
| 4716 | } |
| 4717 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4718 | if (isAggregateTypeForABI(RetTy)) { |
| 4719 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4720 | const Type *Base = nullptr; |
| 4721 | uint64_t Members = 0; |
| 4722 | if (Kind == ELFv2 && |
| 4723 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4724 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4725 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4726 | return ABIArgInfo::getDirect(CoerceTy); |
| 4727 | } |
| 4728 | |
| 4729 | // ELFv2 small aggregates are returned in up to two registers. |
| 4730 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4731 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4732 | if (Bits == 0) |
| 4733 | return ABIArgInfo::getIgnore(); |
| 4734 | |
| 4735 | llvm::Type *CoerceTy; |
| 4736 | if (Bits > GPRBits) { |
| 4737 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4738 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4739 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4740 | CoerceTy = |
| 4741 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4742 | return ABIArgInfo::getDirect(CoerceTy); |
| 4743 | } |
| 4744 | |
| 4745 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4746 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4747 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4748 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4749 | return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 4750 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4753 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4754 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4755 | QualType Ty) const { |
| 4756 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4757 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4758 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4759 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4760 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4761 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4762 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4763 | // in separate doublewords. However, Clang expects us to produce a |
| 4764 | // pointer to a structure with the two parts packed tightly. So generate |
| 4765 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4766 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4767 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4768 | CharUnits EltSize = TypeInfo.first / 2; |
| 4769 | if (EltSize < SlotSize) { |
| 4770 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4771 | SlotSize * 2, SlotSize, |
| 4772 | SlotSize, /*AllowHigher*/ true); |
| 4773 | |
| 4774 | Address RealAddr = Addr; |
| 4775 | Address ImagAddr = RealAddr; |
| 4776 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4777 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4778 | SlotSize - EltSize); |
| 4779 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4780 | 2 * SlotSize - EltSize); |
| 4781 | } else { |
| 4782 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4783 | } |
| 4784 | |
| 4785 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4786 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4787 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4788 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4789 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4790 | |
| 4791 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4792 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4793 | /*init*/ true); |
| 4794 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4795 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4796 | } |
| 4797 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4798 | // Otherwise, just use the general rule. |
| 4799 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4800 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | static bool |
| 4804 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4805 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4806 | // This is calculated from the LLVM and GCC tables and verified |
| 4807 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4808 | |
| 4809 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4810 | |
| 4811 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4812 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4813 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4814 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4815 | |
| 4816 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4817 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4818 | |
| 4819 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4820 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4821 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4822 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4823 | // 64: mq |
| 4824 | // 65: lr |
| 4825 | // 66: ctr |
| 4826 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4827 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4828 | |
| 4829 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4830 | // 68-75 cr0-7 |
| 4831 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4832 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4833 | |
| 4834 | // 77-108: v0-31, the 16-byte vector registers |
| 4835 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4836 | |
| 4837 | // 109: vrsave |
| 4838 | // 110: vscr |
| 4839 | // 111: spe_acc |
| 4840 | // 112: spefscr |
| 4841 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4842 | // 114: tfhar |
| 4843 | // 115: tfiar |
| 4844 | // 116: texasr |
| 4845 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4846 | |
| 4847 | return false; |
| 4848 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4849 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4850 | bool |
| 4851 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4852 | CodeGen::CodeGenFunction &CGF, |
| 4853 | llvm::Value *Address) const { |
| 4854 | |
| 4855 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4856 | } |
| 4857 | |
| 4858 | bool |
| 4859 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4860 | llvm::Value *Address) const { |
| 4861 | |
| 4862 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4863 | } |
| 4864 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4865 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4866 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4867 | //===----------------------------------------------------------------------===// |
| 4868 | |
| 4869 | namespace { |
| 4870 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4871 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4872 | public: |
| 4873 | enum ABIKind { |
| 4874 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4875 | DarwinPCS, |
| 4876 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4877 | }; |
| 4878 | |
| 4879 | private: |
| 4880 | ABIKind Kind; |
| 4881 | |
| 4882 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4883 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4884 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4885 | |
| 4886 | private: |
| 4887 | ABIKind getABIKind() const { return Kind; } |
| 4888 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4889 | |
| 4890 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4891 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4892 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4893 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4894 | uint64_t Members) const override; |
| 4895 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4896 | bool isIllegalVectorType(QualType Ty) const; |
| 4897 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4898 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4899 | if (!getCXXABI().classifyReturnType(FI)) |
| 4900 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4901 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4902 | for (auto &it : FI.arguments()) |
| 4903 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4906 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4907 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4908 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4909 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4910 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4911 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4912 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4913 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4914 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 4915 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4916 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4917 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4918 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4919 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4920 | QualType Ty) const override; |
| 4921 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 4922 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4923 | bool asReturnValue) const override { |
| 4924 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4925 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 4926 | bool isSwiftErrorInRegister() const override { |
| 4927 | return true; |
| 4928 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 4929 | |
| 4930 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 4931 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4932 | }; |
| 4933 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4934 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4935 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4936 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4937 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4938 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4939 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 4940 | return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4943 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4944 | return 31; |
| 4945 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4946 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4947 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4948 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 4949 | |
| 4950 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 4951 | public: |
| 4952 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 4953 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 4954 | |
| 4955 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 4956 | llvm::SmallString<24> &Opt) const override { |
| 4957 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 4958 | } |
| 4959 | |
| 4960 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 4961 | llvm::SmallString<32> &Opt) const override { |
| 4962 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 4963 | } |
| 4964 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4965 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4966 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4967 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4968 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4969 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4970 | // Handle illegal vector types here. |
| 4971 | if (isIllegalVectorType(Ty)) { |
| 4972 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4973 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 4974 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 4975 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 4976 | return ABIArgInfo::getDirect(ResType); |
| 4977 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4978 | if (Size <= 32) { |
| 4979 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4980 | return ABIArgInfo::getDirect(ResType); |
| 4981 | } |
| 4982 | if (Size == 64) { |
| 4983 | llvm::Type *ResType = |
| 4984 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4985 | return ABIArgInfo::getDirect(ResType); |
| 4986 | } |
| 4987 | if (Size == 128) { |
| 4988 | llvm::Type *ResType = |
| 4989 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4990 | return ABIArgInfo::getDirect(ResType); |
| 4991 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4992 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4993 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4994 | |
| 4995 | if (!isAggregateTypeForABI(Ty)) { |
| 4996 | // Treat an enum type as its underlying type. |
| 4997 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4998 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4999 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5000 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5001 | ? ABIArgInfo::getExtend(Ty) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5002 | : ABIArgInfo::getDirect()); |
| 5003 | } |
| 5004 | |
| 5005 | // Structures with either a non-trivial destructor or a non-trivial |
| 5006 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5007 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5008 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 5009 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
| 5012 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 5013 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5014 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5015 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 5016 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5017 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 5018 | return ABIArgInfo::getIgnore(); |
| 5019 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5020 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 5021 | // 0. |
| 5022 | if (IsEmpty && Size == 0) |
| 5023 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5024 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5025 | } |
| 5026 | |
| 5027 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5028 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5029 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5030 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5031 | return ABIArgInfo::getDirect( |
| 5032 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5036 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5037 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5038 | // same size and alignment. |
| 5039 | if (getTarget().isRenderScriptTarget()) { |
| 5040 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5041 | } |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 5042 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5043 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5044 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5045 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5046 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 5047 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5048 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5049 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5050 | } |
| 5051 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5052 | } |
| 5053 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5054 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5057 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5058 | if (RetTy->isVoidType()) |
| 5059 | return ABIArgInfo::getIgnore(); |
| 5060 | |
| 5061 | // Large vector types should be returned via memory. |
| 5062 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5063 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5064 | |
| 5065 | if (!isAggregateTypeForABI(RetTy)) { |
| 5066 | // Treat an enum type as its underlying type. |
| 5067 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5068 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5069 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5070 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5071 | ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5072 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5075 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5076 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5077 | return ABIArgInfo::getIgnore(); |
| 5078 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5079 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5080 | uint64_t Members = 0; |
| 5081 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5082 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 5083 | return ABIArgInfo::getDirect(); |
| 5084 | |
| 5085 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5086 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5087 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5088 | // same size and alignment. |
| 5089 | if (getTarget().isRenderScriptTarget()) { |
| 5090 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5091 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5092 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5093 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5094 | |
| 5095 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5096 | // For aggregates with 16-byte alignment, we use i128. |
| 5097 | if (Alignment < 128 && Size == 128) { |
| 5098 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5099 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5100 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5101 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5102 | } |
| 5103 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5104 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5107 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5108 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5109 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5110 | // Check whether VT is legal. |
| 5111 | unsigned NumElements = VT->getNumElements(); |
| 5112 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5113 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5114 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5115 | return true; |
| 5116 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5117 | } |
| 5118 | return false; |
| 5119 | } |
| 5120 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5121 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5122 | llvm::Type *eltTy, |
| 5123 | unsigned elts) const { |
| 5124 | if (!llvm::isPowerOf2_32(elts)) |
| 5125 | return false; |
| 5126 | if (totalSize.getQuantity() != 8 && |
| 5127 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5128 | return false; |
| 5129 | return true; |
| 5130 | } |
| 5131 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5132 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5133 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5134 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5135 | // but with the difference that any floating-point type is allowed, |
| 5136 | // including __fp16. |
| 5137 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5138 | if (BT->isFloatingPoint()) |
| 5139 | return true; |
| 5140 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5141 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5142 | if (VecSize == 64 || VecSize == 128) |
| 5143 | return true; |
| 5144 | } |
| 5145 | return false; |
| 5146 | } |
| 5147 | |
| 5148 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5149 | uint64_t Members) const { |
| 5150 | return Members <= 4; |
| 5151 | } |
| 5152 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5153 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5154 | QualType Ty, |
| 5155 | CodeGenFunction &CGF) const { |
| 5156 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5157 | bool IsIndirect = AI.isIndirect(); |
| 5158 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5159 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5160 | if (IsIndirect) |
| 5161 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5162 | else if (AI.getCoerceToType()) |
| 5163 | BaseTy = AI.getCoerceToType(); |
| 5164 | |
| 5165 | unsigned NumRegs = 1; |
| 5166 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5167 | BaseTy = ArrTy->getElementType(); |
| 5168 | NumRegs = ArrTy->getNumElements(); |
| 5169 | } |
| 5170 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5171 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5172 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5173 | // Standard, section B.4: |
| 5174 | // |
| 5175 | // struct { |
| 5176 | // void *__stack; |
| 5177 | // void *__gr_top; |
| 5178 | // void *__vr_top; |
| 5179 | // int __gr_offs; |
| 5180 | // int __vr_offs; |
| 5181 | // }; |
| 5182 | |
| 5183 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5184 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5185 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5186 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5187 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5188 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5189 | CharUnits TyAlign = TyInfo.second; |
| 5190 | |
| 5191 | Address reg_offs_p = Address::invalid(); |
| 5192 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5193 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5194 | CharUnits reg_top_offset; |
| 5195 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5196 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5197 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5198 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5199 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5200 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5201 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5202 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5203 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5204 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5205 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5206 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5207 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5208 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 5209 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5210 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5211 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5212 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5213 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | //======================================= |
| 5217 | // Find out where argument was passed |
| 5218 | //======================================= |
| 5219 | |
| 5220 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5221 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5222 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5223 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5224 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5225 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5226 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5227 | |
| 5228 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5229 | |
| 5230 | // 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] | 5231 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5232 | CGF.EmitBlock(MaybeRegBlock); |
| 5233 | |
| 5234 | // Integer arguments may need to correct register alignment (for example a |
| 5235 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5236 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5237 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5238 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5239 | |
| 5240 | reg_offs = CGF.Builder.CreateAdd( |
| 5241 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5242 | "align_regoffs"); |
| 5243 | reg_offs = CGF.Builder.CreateAnd( |
| 5244 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5245 | "aligned_regoffs"); |
| 5246 | } |
| 5247 | |
| 5248 | // 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] | 5249 | // The fact that this is done unconditionally reflects the fact that |
| 5250 | // allocating an argument to the stack also uses up all the remaining |
| 5251 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5252 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5253 | NewOffset = CGF.Builder.CreateAdd( |
| 5254 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5255 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5256 | |
| 5257 | // Now we're in a position to decide whether this argument really was in |
| 5258 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5259 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5260 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5261 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5262 | |
| 5263 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5264 | |
| 5265 | //======================================= |
| 5266 | // Argument was in registers |
| 5267 | //======================================= |
| 5268 | |
| 5269 | // Now we emit the code for if the argument was originally passed in |
| 5270 | // registers. First start the appropriate block: |
| 5271 | CGF.EmitBlock(InRegBlock); |
| 5272 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5273 | llvm::Value *reg_top = nullptr; |
| 5274 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 5275 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5276 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5277 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5278 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5279 | Address RegAddr = Address::invalid(); |
| 5280 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5281 | |
| 5282 | if (IsIndirect) { |
| 5283 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5284 | // stored registers or on the stack will actually be a struct **. |
| 5285 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5286 | } |
| 5287 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5288 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5289 | uint64_t NumMembers = 0; |
| 5290 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5291 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5292 | // Homogeneous aggregates passed in registers will have their elements split |
| 5293 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5294 | // qN+1, ...). We reload and store into a temporary local variable |
| 5295 | // contiguously. |
| 5296 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5297 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5298 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5299 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5300 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5301 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5302 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5303 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5304 | int Offset = 0; |
| 5305 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5306 | BaseTyInfo.first.getQuantity() < 16) |
| 5307 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5308 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5309 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5310 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5311 | Address LoadAddr = |
| 5312 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5313 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5314 | |
| 5315 | Address StoreAddr = |
| 5316 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5317 | |
| 5318 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5319 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5320 | } |
| 5321 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5322 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5323 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5324 | // Otherwise the object is contiguous in memory. |
| 5325 | |
| 5326 | // It might be right-aligned in its slot. |
| 5327 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5328 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5329 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5330 | TyInfo.first < SlotSize) { |
| 5331 | CharUnits Offset = SlotSize - TyInfo.first; |
| 5332 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5333 | } |
| 5334 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5335 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | CGF.EmitBranch(ContBlock); |
| 5339 | |
| 5340 | //======================================= |
| 5341 | // Argument was on the stack |
| 5342 | //======================================= |
| 5343 | CGF.EmitBlock(OnStackBlock); |
| 5344 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5345 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 5346 | CharUnits::Zero(), "stack_p"); |
| 5347 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5348 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5349 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5350 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5351 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5352 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5353 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5354 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5355 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5356 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5357 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5358 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5359 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5360 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5361 | "align_stack"); |
| 5362 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5363 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5364 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5365 | Address OnStackAddr(OnStackPtr, |
| 5366 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5367 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5368 | // All stack slots are multiples of 8 bytes. |
| 5369 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5370 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5371 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5372 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5373 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5374 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5375 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5376 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5377 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5378 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5379 | |
| 5380 | // Write the new value of __stack for the next call to va_arg |
| 5381 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5382 | |
| 5383 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5384 | TyInfo.first < StackSlotSize) { |
| 5385 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 5386 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5389 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5390 | |
| 5391 | CGF.EmitBranch(ContBlock); |
| 5392 | |
| 5393 | //======================================= |
| 5394 | // Tidy up |
| 5395 | //======================================= |
| 5396 | CGF.EmitBlock(ContBlock); |
| 5397 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5398 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5399 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5400 | |
| 5401 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5402 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 5403 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5404 | |
| 5405 | return ResAddr; |
| 5406 | } |
| 5407 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5408 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5409 | CodeGenFunction &CGF) const { |
| 5410 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5411 | // illegal vector types. Lower VAArg here for these cases and use |
| 5412 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5413 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5414 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5415 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5416 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5417 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5418 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5419 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5420 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5421 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5422 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5423 | } |
| 5424 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5425 | // The size of the actual thing passed, which might end up just |
| 5426 | // being a pointer for indirect types. |
| 5427 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5428 | |
| 5429 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5430 | // aggregates should be passed indirectly. |
| 5431 | bool IsIndirect = false; |
| 5432 | if (TyInfo.first.getQuantity() > 16) { |
| 5433 | const Type *Base = nullptr; |
| 5434 | uint64_t Members = 0; |
| 5435 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5436 | } |
| 5437 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5438 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5439 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5442 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5443 | QualType Ty) const { |
| 5444 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5445 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5446 | CharUnits::fromQuantity(8), |
| 5447 | /*allowHigherAlign*/ false); |
| 5448 | } |
| 5449 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5450 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5451 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5452 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5453 | |
| 5454 | namespace { |
| 5455 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5456 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5457 | public: |
| 5458 | enum ABIKind { |
| 5459 | APCS = 0, |
| 5460 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5461 | AAPCS_VFP = 2, |
| 5462 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5463 | }; |
| 5464 | |
| 5465 | private: |
| 5466 | ABIKind Kind; |
| 5467 | |
| 5468 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5469 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5470 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5471 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5472 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5473 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5474 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5475 | switch (getTarget().getTriple().getEnvironment()) { |
| 5476 | case llvm::Triple::Android: |
| 5477 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5478 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5479 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5480 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5481 | case llvm::Triple::MuslEABI: |
| 5482 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5483 | return true; |
| 5484 | default: |
| 5485 | return false; |
| 5486 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5487 | } |
| 5488 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5489 | bool isEABIHF() const { |
| 5490 | switch (getTarget().getTriple().getEnvironment()) { |
| 5491 | case llvm::Triple::EABIHF: |
| 5492 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5493 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5494 | return true; |
| 5495 | default: |
| 5496 | return false; |
| 5497 | } |
| 5498 | } |
| 5499 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5500 | ABIKind getABIKind() const { return Kind; } |
| 5501 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5502 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 5503 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5504 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5505 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5506 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5507 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5508 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5509 | uint64_t Members) const override; |
| 5510 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5511 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5512 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5513 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5514 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5515 | |
| 5516 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5517 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5518 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5519 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 5520 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5521 | bool asReturnValue) const override { |
| 5522 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5523 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5524 | bool isSwiftErrorInRegister() const override { |
| 5525 | return true; |
| 5526 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5527 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5528 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5529 | }; |
| 5530 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5531 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5532 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5533 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5534 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5535 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5536 | const ARMABIInfo &getABIInfo() const { |
| 5537 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5538 | } |
| 5539 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5540 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5541 | return 13; |
| 5542 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5543 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5544 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5545 | return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5546 | } |
| 5547 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5548 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5549 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5550 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5551 | |
| 5552 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5553 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5554 | return false; |
| 5555 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5556 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5557 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5558 | if (getABIInfo().isEABI()) return 88; |
| 5559 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5560 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5561 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5562 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5563 | CodeGen::CodeGenModule &CGM) const override { |
| 5564 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5565 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5566 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5567 | if (!FD) |
| 5568 | return; |
| 5569 | |
| 5570 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5571 | if (!Attr) |
| 5572 | return; |
| 5573 | |
| 5574 | const char *Kind; |
| 5575 | switch (Attr->getInterrupt()) { |
| 5576 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5577 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5578 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5579 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5580 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5581 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5582 | } |
| 5583 | |
| 5584 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5585 | |
| 5586 | Fn->addFnAttr("interrupt", Kind); |
| 5587 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5588 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5589 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5590 | return; |
| 5591 | |
| 5592 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5593 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5594 | // the backend to perform a realignment as part of the function prologue. |
| 5595 | llvm::AttrBuilder B; |
| 5596 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5597 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5598 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5599 | }; |
| 5600 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5601 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5602 | public: |
| 5603 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5604 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5605 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5606 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5607 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5608 | |
| 5609 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5610 | llvm::SmallString<24> &Opt) const override { |
| 5611 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5612 | } |
| 5613 | |
| 5614 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5615 | llvm::SmallString<32> &Opt) const override { |
| 5616 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5617 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5618 | }; |
| 5619 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5620 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5621 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5622 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5623 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5624 | return; |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5625 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 5626 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5627 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5628 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5629 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5630 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5631 | FI.getReturnInfo() = |
| 5632 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5633 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5634 | for (auto &I : FI.arguments()) |
| 5635 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5636 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5637 | // Always honor user-specified calling convention. |
| 5638 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5639 | return; |
| 5640 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5641 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5642 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5643 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5644 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5645 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5646 | /// Return the default calling convention that LLVM will use. |
| 5647 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5648 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5649 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5650 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5651 | else if (isEABI()) |
| 5652 | return llvm::CallingConv::ARM_AAPCS; |
| 5653 | else |
| 5654 | return llvm::CallingConv::ARM_APCS; |
| 5655 | } |
| 5656 | |
| 5657 | /// Return the calling convention that our ABI would like us to use |
| 5658 | /// as the C calling convention. |
| 5659 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5660 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5661 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5662 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5663 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5664 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5665 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5666 | llvm_unreachable("bad ABI kind"); |
| 5667 | } |
| 5668 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5669 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5670 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5671 | |
| 5672 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5673 | // they'd just match what LLVM will infer from the triple. |
| 5674 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5675 | if (abiCC != getLLVMDefaultCC()) |
| 5676 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5677 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5678 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 5679 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 5680 | // 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] | 5681 | |
| 5682 | // The Run-time ABI for the ARM Architecture section 4.1.2 requires |
| 5683 | // AEABI-complying FP helper functions to use the base AAPCS. |
| 5684 | // These AEABI functions are expanded in the ARM llvm backend, all the builtin |
| 5685 | // support functions emitted by clang such as the _Complex helpers follow the |
| 5686 | // abiCC. |
| 5687 | if (abiCC != getLLVMDefaultCC()) |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5688 | BuiltinCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5689 | } |
| 5690 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5691 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5692 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5693 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5694 | // A single-precision floating-point type (including promoted |
| 5695 | // half-precision types); A double-precision floating-point type; |
| 5696 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5697 | // with a Base Type of a single- or double-precision floating-point type, |
| 5698 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5699 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5700 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5701 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5702 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5703 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5704 | // Handle illegal vector types here. |
| 5705 | if (isIllegalVectorType(Ty)) { |
| 5706 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5707 | if (Size <= 32) { |
| 5708 | llvm::Type *ResType = |
| 5709 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5710 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5711 | } |
| 5712 | if (Size == 64) { |
| 5713 | llvm::Type *ResType = llvm::VectorType::get( |
| 5714 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5715 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5716 | } |
| 5717 | if (Size == 128) { |
| 5718 | llvm::Type *ResType = llvm::VectorType::get( |
| 5719 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5720 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5721 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5722 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5723 | } |
| 5724 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 5725 | // _Float16 and __fp16 get passed as if it were an int or float, but with |
| 5726 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 5727 | // half type natively, and does not need to interwork with AAPCS code. |
| 5728 | if ((Ty->isFloat16Type() || Ty->isHalfType()) && |
| 5729 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5730 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5731 | llvm::Type::getFloatTy(getVMContext()) : |
| 5732 | llvm::Type::getInt32Ty(getVMContext()); |
| 5733 | return ABIArgInfo::getDirect(ResType); |
| 5734 | } |
| 5735 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5736 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5737 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5738 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5739 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5740 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5741 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5742 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5743 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5744 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5745 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5746 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5747 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5748 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5749 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5750 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5751 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5752 | return ABIArgInfo::getIgnore(); |
| 5753 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5754 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5755 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5756 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5757 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5758 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5759 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5760 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5761 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5762 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5763 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5764 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5765 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5766 | // this convention even for a variadic function: the backend will use GPRs |
| 5767 | // if needed. |
| 5768 | const Type *Base = nullptr; |
| 5769 | uint64_t Members = 0; |
| 5770 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5771 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5772 | llvm::Type *Ty = |
| 5773 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5774 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5775 | } |
| 5776 | } |
| 5777 | |
| 5778 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5779 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5780 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5781 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5782 | // and a pointer is passed. |
| 5783 | return ABIArgInfo::getIndirect( |
| 5784 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5785 | } |
| 5786 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5787 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5788 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5789 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5790 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5791 | uint64_t ABIAlign = 4; |
| 5792 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5793 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5794 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5795 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5796 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5797 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5798 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5799 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5800 | /*ByVal=*/true, |
| 5801 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5802 | } |
| 5803 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5804 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5805 | // same size and alignment. |
| 5806 | if (getTarget().isRenderScriptTarget()) { |
| 5807 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5808 | } |
| 5809 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5810 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5811 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5812 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5813 | // FIXME: Try to match the types of the arguments more accurately where |
| 5814 | // we can. |
| 5815 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5816 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5817 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5818 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5819 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5820 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5821 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5822 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5823 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5824 | } |
| 5825 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5826 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5827 | llvm::LLVMContext &VMContext) { |
| 5828 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5829 | // is called integer-like if its size is less than or equal to one word, and |
| 5830 | // the offset of each of its addressable sub-fields is zero. |
| 5831 | |
| 5832 | uint64_t Size = Context.getTypeSize(Ty); |
| 5833 | |
| 5834 | // Check that the type fits in a word. |
| 5835 | if (Size > 32) |
| 5836 | return false; |
| 5837 | |
| 5838 | // FIXME: Handle vector types! |
| 5839 | if (Ty->isVectorType()) |
| 5840 | return false; |
| 5841 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5842 | // Float types are never treated as "integer like". |
| 5843 | if (Ty->isRealFloatingType()) |
| 5844 | return false; |
| 5845 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5846 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5847 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5848 | return true; |
| 5849 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5850 | // Small complex integer types are "integer like". |
| 5851 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5852 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5853 | |
| 5854 | // Single element and zero sized arrays should be allowed, by the definition |
| 5855 | // above, but they are not. |
| 5856 | |
| 5857 | // Otherwise, it must be a record type. |
| 5858 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5859 | if (!RT) return false; |
| 5860 | |
| 5861 | // Ignore records with flexible arrays. |
| 5862 | const RecordDecl *RD = RT->getDecl(); |
| 5863 | if (RD->hasFlexibleArrayMember()) |
| 5864 | return false; |
| 5865 | |
| 5866 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5867 | // like". |
| 5868 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5869 | |
| 5870 | bool HadField = false; |
| 5871 | unsigned idx = 0; |
| 5872 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5873 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5874 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5875 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5876 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5877 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5878 | // struct { int : 0; int x } |
| 5879 | // is non-integer like according to gcc. |
| 5880 | if (FD->isBitField()) { |
| 5881 | if (!RD->isUnion()) |
| 5882 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5883 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5884 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5885 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5886 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5887 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5888 | } |
| 5889 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5890 | // Check if this field is at offset 0. |
| 5891 | if (Layout.getFieldOffset(idx) != 0) |
| 5892 | return false; |
| 5893 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5894 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5895 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5896 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5897 | // Only allow at most one field in a structure. This doesn't match the |
| 5898 | // wording above, but follows gcc in situations with a field following an |
| 5899 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5900 | if (!RD->isUnion()) { |
| 5901 | if (HadField) |
| 5902 | return false; |
| 5903 | |
| 5904 | HadField = true; |
| 5905 | } |
| 5906 | } |
| 5907 | |
| 5908 | return true; |
| 5909 | } |
| 5910 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5911 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5912 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5913 | bool IsEffectivelyAAPCS_VFP = |
| 5914 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5915 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5916 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5917 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5918 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5919 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5920 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5921 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5922 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5923 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 5924 | // _Float16 and __fp16 get returned as if it were an int or float, but with |
| 5925 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 5926 | // half type natively, and does not need to interwork with AAPCS code. |
| 5927 | if ((RetTy->isFloat16Type() || RetTy->isHalfType()) && |
| 5928 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5929 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5930 | llvm::Type::getFloatTy(getVMContext()) : |
| 5931 | llvm::Type::getInt32Ty(getVMContext()); |
| 5932 | return ABIArgInfo::getDirect(ResType); |
| 5933 | } |
| 5934 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5935 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5936 | // Treat an enum type as its underlying type. |
| 5937 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5938 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5939 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5940 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5941 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5942 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5943 | |
| 5944 | // Are we following APCS? |
| 5945 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5946 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5947 | return ABIArgInfo::getIgnore(); |
| 5948 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5949 | // Complex types are all returned as packed integers. |
| 5950 | // |
| 5951 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5952 | // correctly. |
| 5953 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5954 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5955 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5956 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5957 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5958 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5959 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5960 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5961 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5962 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5963 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5964 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5965 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
| 5968 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5969 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5970 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5971 | |
| 5972 | // Otherwise this is an AAPCS variant. |
| 5973 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5974 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5975 | return ABIArgInfo::getIgnore(); |
| 5976 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5977 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5978 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5979 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5980 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5981 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5982 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5983 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5984 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5985 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5986 | } |
| 5987 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5988 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5989 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5990 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5991 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5992 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 5993 | // same size and alignment. |
| 5994 | if (getTarget().isRenderScriptTarget()) { |
| 5995 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5996 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5997 | if (getDataLayout().isBigEndian()) |
| 5998 | // 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] | 5999 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6000 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6001 | // Return in the smallest viable integer type. |
| 6002 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6003 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6004 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6005 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6006 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6007 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 6008 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 6009 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6010 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6011 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6012 | } |
| 6013 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6014 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6015 | } |
| 6016 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6017 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 6018 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6019 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 6020 | if (isAndroid()) { |
| 6021 | // Android shipped using Clang 3.1, which supported a slightly different |
| 6022 | // vector ABI. The primary differences were that 3-element vector types |
| 6023 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 6024 | // accepts that legacy behavior for Android only. |
| 6025 | // Check whether VT is legal. |
| 6026 | unsigned NumElements = VT->getNumElements(); |
| 6027 | // NumElements should be power of 2 or equal to 3. |
| 6028 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 6029 | return true; |
| 6030 | } else { |
| 6031 | // Check whether VT is legal. |
| 6032 | unsigned NumElements = VT->getNumElements(); |
| 6033 | uint64_t Size = getContext().getTypeSize(VT); |
| 6034 | // NumElements should be power of 2. |
| 6035 | if (!llvm::isPowerOf2_32(NumElements)) |
| 6036 | return true; |
| 6037 | // Size should be greater than 32 bits. |
| 6038 | return Size <= 32; |
| 6039 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6040 | } |
| 6041 | return false; |
| 6042 | } |
| 6043 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 6044 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 6045 | llvm::Type *eltTy, |
| 6046 | unsigned numElts) const { |
| 6047 | if (!llvm::isPowerOf2_32(numElts)) |
| 6048 | return false; |
| 6049 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 6050 | if (size > 64) |
| 6051 | return false; |
| 6052 | if (vectorSize.getQuantity() != 8 && |
| 6053 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 6054 | return false; |
| 6055 | return true; |
| 6056 | } |
| 6057 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 6058 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 6059 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 6060 | // double, or 64-bit or 128-bit vectors. |
| 6061 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 6062 | if (BT->getKind() == BuiltinType::Float || |
| 6063 | BT->getKind() == BuiltinType::Double || |
| 6064 | BT->getKind() == BuiltinType::LongDouble) |
| 6065 | return true; |
| 6066 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 6067 | unsigned VecSize = getContext().getTypeSize(VT); |
| 6068 | if (VecSize == 64 || VecSize == 128) |
| 6069 | return true; |
| 6070 | } |
| 6071 | return false; |
| 6072 | } |
| 6073 | |
| 6074 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 6075 | uint64_t Members) const { |
| 6076 | return Members <= 4; |
| 6077 | } |
| 6078 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6079 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6080 | QualType Ty) const { |
| 6081 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6082 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6083 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6084 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6085 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 6086 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 6087 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6088 | } |
| 6089 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6090 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 6091 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6092 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6093 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6094 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6095 | const Type *Base = nullptr; |
| 6096 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6097 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 6098 | IsIndirect = true; |
| 6099 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6100 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6101 | // allocated by the caller. |
| 6102 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 6103 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6104 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6105 | IsIndirect = true; |
| 6106 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6107 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6108 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6109 | // 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] | 6110 | // Our callers should be prepared to handle an under-aligned address. |
| 6111 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6112 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6113 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6114 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6115 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6116 | // ARMv7k allows type alignment up to 16 bytes. |
| 6117 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6118 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6119 | } else { |
| 6120 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6121 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6122 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6123 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6124 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6125 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6126 | } |
| 6127 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6128 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6129 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6130 | //===----------------------------------------------------------------------===// |
| 6131 | |
| 6132 | namespace { |
| 6133 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6134 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6135 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6136 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6137 | |
| 6138 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6139 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6140 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6141 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6142 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6143 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6144 | }; |
| 6145 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6146 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6147 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6148 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6149 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6150 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6151 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6152 | CodeGen::CodeGenModule &M) const override; |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6153 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6154 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6155 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6156 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6157 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6158 | }; |
| 6159 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6160 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6161 | if (RetTy->isVoidType()) |
| 6162 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6163 | |
| 6164 | // note: this is different from default ABI |
| 6165 | if (!RetTy->isScalarType()) |
| 6166 | return ABIArgInfo::getDirect(); |
| 6167 | |
| 6168 | // Treat an enum type as its underlying type. |
| 6169 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6170 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6171 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6172 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 6173 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6176 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6177 | // Treat an enum type as its underlying type. |
| 6178 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6179 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6180 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6181 | // Return aggregates type as indirect by value |
| 6182 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6183 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6184 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6185 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 6186 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6187 | } |
| 6188 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6189 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6190 | if (!getCXXABI().classifyReturnType(FI)) |
| 6191 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6192 | for (auto &I : FI.arguments()) |
| 6193 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6194 | |
| 6195 | // Always honor user-specified calling convention. |
| 6196 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6197 | return; |
| 6198 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6199 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6200 | } |
| 6201 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6202 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6203 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6204 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6207 | void NVPTXTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6208 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6209 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6210 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6211 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6212 | if (!FD) return; |
| 6213 | |
| 6214 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6215 | |
| 6216 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6217 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6218 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6219 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6220 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6221 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6222 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6223 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6224 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6225 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6226 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6227 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6228 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6229 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6230 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6231 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6232 | // __global__ functions cannot be called from the device, we do not |
| 6233 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6234 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6235 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6236 | addNVVMMetadata(F, "kernel", 1); |
| 6237 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6238 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6239 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6240 | llvm::APSInt MaxThreads(32); |
| 6241 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6242 | if (MaxThreads > 0) |
| 6243 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6244 | |
| 6245 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6246 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6247 | // we don't have to add a PTX directive. |
| 6248 | if (Attr->getMinBlocks()) { |
| 6249 | llvm::APSInt MinBlocks(32); |
| 6250 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6251 | if (MinBlocks > 0) |
| 6252 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6253 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6254 | } |
| 6255 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6256 | } |
| 6257 | } |
| 6258 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6259 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6260 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6261 | llvm::Module *M = F->getParent(); |
| 6262 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6263 | |
| 6264 | // Get "nvvm.annotations" metadata node |
| 6265 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6266 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6267 | llvm::Metadata *MDVals[] = { |
| 6268 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6269 | llvm::ConstantAsMetadata::get( |
| 6270 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6271 | // Append metadata to nvvm.annotations |
| 6272 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6273 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6274 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6275 | |
| 6276 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6277 | // SystemZ ABI Implementation |
| 6278 | //===----------------------------------------------------------------------===// |
| 6279 | |
| 6280 | namespace { |
| 6281 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6282 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6283 | bool HasVector; |
| 6284 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6285 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6286 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6287 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6288 | |
| 6289 | bool isPromotableIntegerType(QualType Ty) const; |
| 6290 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6291 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6292 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6293 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6294 | |
| 6295 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6296 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6297 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6298 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6299 | if (!getCXXABI().classifyReturnType(FI)) |
| 6300 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6301 | for (auto &I : FI.arguments()) |
| 6302 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6303 | } |
| 6304 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6305 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6306 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6307 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 6308 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6309 | bool asReturnValue) const override { |
| 6310 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6311 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6312 | bool isSwiftErrorInRegister() const override { |
Arnold Schwaighofer | 612d693 | 2017-11-07 16:40:51 +0000 | [diff] [blame] | 6313 | return false; |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6314 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6315 | }; |
| 6316 | |
| 6317 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6318 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6319 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6320 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6321 | }; |
| 6322 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6323 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6324 | |
| 6325 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6326 | // Treat an enum type as its underlying type. |
| 6327 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6328 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6329 | |
| 6330 | // Promotable integer types are required to be promoted by the ABI. |
| 6331 | if (Ty->isPromotableIntegerType()) |
| 6332 | return true; |
| 6333 | |
| 6334 | // 32-bit values must also be promoted. |
| 6335 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6336 | switch (BT->getKind()) { |
| 6337 | case BuiltinType::Int: |
| 6338 | case BuiltinType::UInt: |
| 6339 | return true; |
| 6340 | default: |
| 6341 | return false; |
| 6342 | } |
| 6343 | return false; |
| 6344 | } |
| 6345 | |
| 6346 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6347 | return (Ty->isAnyComplexType() || |
| 6348 | Ty->isVectorType() || |
| 6349 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6350 | } |
| 6351 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6352 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6353 | return (HasVector && |
| 6354 | Ty->isVectorType() && |
| 6355 | getContext().getTypeSize(Ty) <= 128); |
| 6356 | } |
| 6357 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6358 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6359 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6360 | switch (BT->getKind()) { |
| 6361 | case BuiltinType::Float: |
| 6362 | case BuiltinType::Double: |
| 6363 | return true; |
| 6364 | default: |
| 6365 | return false; |
| 6366 | } |
| 6367 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6368 | return false; |
| 6369 | } |
| 6370 | |
| 6371 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6372 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6373 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6374 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6375 | |
| 6376 | // If this is a C++ record, check the bases first. |
| 6377 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6378 | for (const auto &I : CXXRD->bases()) { |
| 6379 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6380 | |
| 6381 | // Empty bases don't affect things either way. |
| 6382 | if (isEmptyRecord(getContext(), Base, true)) |
| 6383 | continue; |
| 6384 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6385 | if (!Found.isNull()) |
| 6386 | return Ty; |
| 6387 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6388 | } |
| 6389 | |
| 6390 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6391 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6392 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6393 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6394 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6395 | if (getContext().getLangOpts().CPlusPlus && |
| 6396 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 6397 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6398 | |
| 6399 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6400 | // Nested structures still do though. |
| 6401 | if (!Found.isNull()) |
| 6402 | return Ty; |
| 6403 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
| 6406 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6407 | // 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] | 6408 | if (!Found.isNull()) |
| 6409 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6410 | } |
| 6411 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6412 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6413 | } |
| 6414 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6415 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6416 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6417 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6418 | // struct { |
| 6419 | // i64 __gpr; |
| 6420 | // i64 __fpr; |
| 6421 | // i8 *__overflow_arg_area; |
| 6422 | // i8 *__reg_save_area; |
| 6423 | // }; |
| 6424 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6425 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6426 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6427 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6428 | Ty = getContext().getCanonicalType(Ty); |
| 6429 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6430 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6431 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6432 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6433 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6434 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6435 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6436 | CharUnits UnpaddedSize; |
| 6437 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6438 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6439 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6440 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6441 | } else { |
| 6442 | if (AI.getCoerceToType()) |
| 6443 | ArgTy = AI.getCoerceToType(); |
| 6444 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6445 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6446 | UnpaddedSize = TyInfo.first; |
| 6447 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6448 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6449 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6450 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6451 | PaddedSize = CharUnits::fromQuantity(16); |
| 6452 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6453 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6454 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6455 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6456 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6457 | llvm::Value *PaddedSizeV = |
| 6458 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6459 | |
| 6460 | if (IsVector) { |
| 6461 | // Work out the address of a vector argument on the stack. |
| 6462 | // Vector arguments are always passed in the high bits of a |
| 6463 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6464 | Address OverflowArgAreaPtr = |
| 6465 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6466 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6467 | Address OverflowArgArea = |
| 6468 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6469 | TyInfo.second); |
| 6470 | Address MemAddr = |
| 6471 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6472 | |
| 6473 | // Update overflow_arg_area_ptr pointer |
| 6474 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6475 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6476 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6477 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6478 | |
| 6479 | return MemAddr; |
| 6480 | } |
| 6481 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6482 | assert(PaddedSize.getQuantity() == 8); |
| 6483 | |
| 6484 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6485 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6486 | if (InFPRs) { |
| 6487 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6488 | RegCountField = 1; // __fpr |
| 6489 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6490 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6491 | } else { |
| 6492 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6493 | RegCountField = 0; // __gpr |
| 6494 | RegSaveIndex = 2; // save offset for r2 |
| 6495 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6496 | } |
| 6497 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6498 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 6499 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 6500 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6501 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6502 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6503 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6504 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6505 | |
| 6506 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6507 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6508 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6509 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6510 | |
| 6511 | // Emit code to load the value if it was passed in registers. |
| 6512 | CGF.EmitBlock(InRegBlock); |
| 6513 | |
| 6514 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6515 | llvm::Value *ScaledRegCount = |
| 6516 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6517 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6518 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6519 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6520 | llvm::Value *RegOffset = |
| 6521 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6522 | Address RegSaveAreaPtr = |
| 6523 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 6524 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6525 | llvm::Value *RegSaveArea = |
| 6526 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6527 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6528 | "raw_reg_addr"), |
| 6529 | PaddedSize); |
| 6530 | Address RegAddr = |
| 6531 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6532 | |
| 6533 | // Update the register count |
| 6534 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6535 | llvm::Value *NewRegCount = |
| 6536 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6537 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6538 | CGF.EmitBranch(ContBlock); |
| 6539 | |
| 6540 | // Emit code to load the value if it was passed in memory. |
| 6541 | CGF.EmitBlock(InMemBlock); |
| 6542 | |
| 6543 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6544 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 6545 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 6546 | Address OverflowArgArea = |
| 6547 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6548 | PaddedSize); |
| 6549 | Address RawMemAddr = |
| 6550 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6551 | Address MemAddr = |
| 6552 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6553 | |
| 6554 | // Update overflow_arg_area_ptr pointer |
| 6555 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6556 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6557 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6558 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6559 | CGF.EmitBranch(ContBlock); |
| 6560 | |
| 6561 | // Return the appropriate result. |
| 6562 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6563 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6564 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6565 | |
| 6566 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6567 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6568 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6569 | |
| 6570 | return ResAddr; |
| 6571 | } |
| 6572 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6573 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6574 | if (RetTy->isVoidType()) |
| 6575 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6576 | if (isVectorArgumentType(RetTy)) |
| 6577 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6578 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6579 | return getNaturalAlignIndirect(RetTy); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6580 | return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 6581 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6582 | } |
| 6583 | |
| 6584 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6585 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6586 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6587 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6588 | |
| 6589 | // Integers and enums are extended to full register width. |
| 6590 | if (isPromotableIntegerType(Ty)) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6591 | return ABIArgInfo::getExtend(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6592 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6593 | // Handle vector types and vector-like structure types. Note that |
| 6594 | // as opposed to float-like structure types, we do not allow any |
| 6595 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6596 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6597 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6598 | if (isVectorArgumentType(SingleElementTy) && |
| 6599 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6600 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6601 | |
| 6602 | // 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] | 6603 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6604 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6605 | |
| 6606 | // Handle small structures. |
| 6607 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6608 | // Structures with flexible arrays have variable length, so really |
| 6609 | // fail the size test above. |
| 6610 | const RecordDecl *RD = RT->getDecl(); |
| 6611 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6612 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6613 | |
| 6614 | // The structure is passed as an unextended integer, a float, or a double. |
| 6615 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6616 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6617 | assert(Size == 32 || Size == 64); |
| 6618 | if (Size == 32) |
| 6619 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6620 | else |
| 6621 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6622 | } else |
| 6623 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6624 | return ABIArgInfo::getDirect(PassTy); |
| 6625 | } |
| 6626 | |
| 6627 | // Non-structure compounds are passed indirectly. |
| 6628 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6629 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6630 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6631 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6632 | } |
| 6633 | |
| 6634 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6635 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6636 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6637 | |
| 6638 | namespace { |
| 6639 | |
| 6640 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6641 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6642 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6643 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6644 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6645 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6646 | }; |
| 6647 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6648 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6649 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6650 | void MSP430TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6651 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6652 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6653 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6654 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6655 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 6656 | // Handle 'interrupt' attribute: |
| 6657 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6658 | |
| 6659 | // Step 1: Set ISR calling convention. |
| 6660 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 6661 | |
| 6662 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6663 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6664 | |
| 6665 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 6666 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 6667 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 6668 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6669 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6670 | } |
| 6671 | } |
| 6672 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6673 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6674 | // MIPS ABI Implementation. This works for both little-endian and |
| 6675 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6676 | //===----------------------------------------------------------------------===// |
| 6677 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6678 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6679 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6680 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6681 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6682 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6683 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6684 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6685 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6686 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6687 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6688 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6689 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6690 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6691 | |
| 6692 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6693 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6694 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6695 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6696 | QualType Ty) const override; |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6697 | ABIArgInfo extendType(QualType Ty) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6698 | }; |
| 6699 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6700 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6701 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6702 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6703 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6704 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6705 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6706 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6707 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6708 | return 29; |
| 6709 | } |
| 6710 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6711 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6712 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6713 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6714 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6715 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6716 | |
| 6717 | if (FD->hasAttr<MipsLongCallAttr>()) |
| 6718 | Fn->addFnAttr("long-call"); |
| 6719 | else if (FD->hasAttr<MipsShortCallAttr>()) |
| 6720 | Fn->addFnAttr("short-call"); |
| 6721 | |
| 6722 | // Other attributes do not have a meaning for declarations. |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6723 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6724 | return; |
| 6725 | |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6726 | if (FD->hasAttr<Mips16Attr>()) { |
| 6727 | Fn->addFnAttr("mips16"); |
| 6728 | } |
| 6729 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6730 | Fn->addFnAttr("nomips16"); |
| 6731 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6732 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6733 | if (FD->hasAttr<MicroMipsAttr>()) |
| 6734 | Fn->addFnAttr("micromips"); |
| 6735 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 6736 | Fn->addFnAttr("nomicromips"); |
| 6737 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6738 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6739 | if (!Attr) |
| 6740 | return; |
| 6741 | |
| 6742 | const char *Kind; |
| 6743 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6744 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6745 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6746 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6747 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6748 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6749 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6750 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6751 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6752 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6753 | } |
| 6754 | |
| 6755 | Fn->addFnAttr("interrupt", Kind); |
| 6756 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6757 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6758 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6759 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6760 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6761 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6762 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6763 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6764 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6765 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6766 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6767 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6768 | void MipsABIInfo::CoerceToIntArgs( |
| 6769 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6770 | llvm::IntegerType *IntTy = |
| 6771 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6772 | |
| 6773 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6774 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6775 | ArgList.push_back(IntTy); |
| 6776 | |
| 6777 | // If necessary, add one more integer type to ArgList. |
| 6778 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6779 | |
| 6780 | if (R) |
| 6781 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6782 | } |
| 6783 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6784 | // In N32/64, an aligned double precision floating point field is passed in |
| 6785 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6786 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6787 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6788 | |
| 6789 | if (IsO32) { |
| 6790 | CoerceToIntArgs(TySize, ArgList); |
| 6791 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6792 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6793 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6794 | if (Ty->isComplexType()) |
| 6795 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6796 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6797 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6798 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6799 | // Unions/vectors are passed in integer registers. |
| 6800 | if (!RT || !RT->isStructureOrClassType()) { |
| 6801 | CoerceToIntArgs(TySize, ArgList); |
| 6802 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6803 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6804 | |
| 6805 | const RecordDecl *RD = RT->getDecl(); |
| 6806 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6807 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6808 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6809 | uint64_t LastOffset = 0; |
| 6810 | unsigned idx = 0; |
| 6811 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6812 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6813 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6814 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6815 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6816 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6817 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6818 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6819 | |
| 6820 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6821 | continue; |
| 6822 | |
| 6823 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6824 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6825 | continue; |
| 6826 | |
| 6827 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6828 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6829 | ArgList.push_back(I64); |
| 6830 | |
| 6831 | // Add double type. |
| 6832 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6833 | LastOffset = Offset + 64; |
| 6834 | } |
| 6835 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6836 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6837 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6838 | |
| 6839 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6840 | } |
| 6841 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6842 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6843 | uint64_t Offset) const { |
| 6844 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6845 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6846 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6847 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6848 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6849 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6850 | ABIArgInfo |
| 6851 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6852 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6853 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6854 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6855 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6856 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6857 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6858 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6859 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6860 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6861 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6862 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6863 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6864 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6865 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6866 | return ABIArgInfo::getIgnore(); |
| 6867 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6868 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6869 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6870 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6871 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6872 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6873 | // If we have reached here, aggregates are passed directly by coercing to |
| 6874 | // another structure type. Padding is inserted if the offset of the |
| 6875 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6876 | ABIArgInfo ArgInfo = |
| 6877 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6878 | getPaddingType(OrigOffset, CurrOffset)); |
| 6879 | ArgInfo.setInReg(true); |
| 6880 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6881 | } |
| 6882 | |
| 6883 | // Treat an enum type as its underlying type. |
| 6884 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6885 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6886 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6887 | // All integral types are promoted to the GPR width. |
| 6888 | if (Ty->isIntegralOrEnumerationType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6889 | return extendType(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6890 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6891 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6892 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6893 | } |
| 6894 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6895 | llvm::Type* |
| 6896 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6897 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6898 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6899 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6900 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6901 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6902 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6903 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6904 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6905 | // N32/64 returns struct/classes in floating point registers if the |
| 6906 | // following conditions are met: |
| 6907 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6908 | // 2. The struct/class has one or two fields all of which are floating |
| 6909 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6910 | // 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] | 6911 | // |
| 6912 | // Any other composite results are returned in integer registers. |
| 6913 | // |
| 6914 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6915 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6916 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6917 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6918 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6919 | if (!BT || !BT->isFloatingPoint()) |
| 6920 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6921 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6922 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6923 | } |
| 6924 | |
| 6925 | if (b == e) |
| 6926 | return llvm::StructType::get(getVMContext(), RTList, |
| 6927 | RD->hasAttr<PackedAttr>()); |
| 6928 | |
| 6929 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6930 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6931 | } |
| 6932 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6933 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6934 | return llvm::StructType::get(getVMContext(), RTList); |
| 6935 | } |
| 6936 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6937 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6938 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6939 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6940 | if (RetTy->isVoidType()) |
| 6941 | return ABIArgInfo::getIgnore(); |
| 6942 | |
| 6943 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6944 | // However, N32/N64 ignores zero sized return values. |
| 6945 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6946 | return ABIArgInfo::getIgnore(); |
| 6947 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6948 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6949 | if (Size <= 128) { |
| 6950 | if (RetTy->isAnyComplexType()) |
| 6951 | return ABIArgInfo::getDirect(); |
| 6952 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6953 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6954 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6955 | if (!IsO32 || |
| 6956 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6957 | ABIArgInfo ArgInfo = |
| 6958 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6959 | ArgInfo.setInReg(true); |
| 6960 | return ArgInfo; |
| 6961 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6962 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6963 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6964 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6965 | } |
| 6966 | |
| 6967 | // Treat an enum type as its underlying type. |
| 6968 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6969 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6970 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6971 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 6972 | : ABIArgInfo::getDirect()); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6973 | } |
| 6974 | |
| 6975 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6976 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6977 | if (!getCXXABI().classifyReturnType(FI)) |
| 6978 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6979 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6980 | // 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] | 6981 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6982 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6983 | for (auto &I : FI.arguments()) |
| 6984 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6985 | } |
| 6986 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6987 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6988 | QualType OrigTy) const { |
| 6989 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6990 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6991 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6992 | // 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] | 6993 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6994 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6995 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6996 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6997 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6998 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6999 | DidPromote = true; |
| 7000 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 7001 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7002 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7003 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7004 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7005 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7006 | // The alignment of things in the argument area is never larger than |
| 7007 | // StackAlignInBytes. |
| 7008 | TyInfo.second = |
| 7009 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 7010 | |
| 7011 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 7012 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 7013 | |
| 7014 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7015 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 7016 | |
| 7017 | |
| 7018 | // If there was a promotion, "unpromote" into a temporary. |
| 7019 | // TODO: can we just use a pointer into a subset of the original slot? |
| 7020 | if (DidPromote) { |
| 7021 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 7022 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 7023 | |
| 7024 | // Truncate down to the right width. |
| 7025 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 7026 | : CGF.IntPtrTy); |
| 7027 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 7028 | if (OrigTy->isPointerType()) |
| 7029 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 7030 | |
| 7031 | CGF.Builder.CreateStore(V, Temp); |
| 7032 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7033 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7034 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7035 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7036 | } |
| 7037 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7038 | ABIArgInfo MipsABIInfo::extendType(QualType Ty) const { |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7039 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7040 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7041 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 7042 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7043 | return ABIArgInfo::getSignExtend(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7044 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7045 | return ABIArgInfo::getExtend(Ty); |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7046 | } |
| 7047 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7048 | bool |
| 7049 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7050 | llvm::Value *Address) const { |
| 7051 | // This information comes from gcc's implementation, which seems to |
| 7052 | // as canonical as it gets. |
| 7053 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7054 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 7055 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7056 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7057 | |
| 7058 | // 0-31 are the general purpose registers, $0 - $31. |
| 7059 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 7060 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 7061 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7062 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7063 | |
| 7064 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 7065 | // They are one bit wide and ignored here. |
| 7066 | |
| 7067 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 7068 | // (coprocessor 1 is the FP unit) |
| 7069 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 7070 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 7071 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7072 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7073 | return false; |
| 7074 | } |
| 7075 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7076 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7077 | // AVR ABI Implementation. |
| 7078 | //===----------------------------------------------------------------------===// |
| 7079 | |
| 7080 | namespace { |
| 7081 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7082 | public: |
| 7083 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7084 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 7085 | |
| 7086 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7087 | CodeGen::CodeGenModule &CGM) const override { |
| 7088 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7089 | return; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7090 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 7091 | if (!FD) return; |
| 7092 | auto *Fn = cast<llvm::Function>(GV); |
| 7093 | |
| 7094 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7095 | Fn->addFnAttr("interrupt"); |
| 7096 | |
| 7097 | if (FD->getAttr<AVRSignalAttr>()) |
| 7098 | Fn->addFnAttr("signal"); |
| 7099 | } |
| 7100 | }; |
| 7101 | } |
| 7102 | |
| 7103 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7104 | // 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] | 7105 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7106 | // handling. |
| 7107 | //===----------------------------------------------------------------------===// |
| 7108 | |
| 7109 | namespace { |
| 7110 | |
| 7111 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7112 | public: |
| 7113 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7114 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7115 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7116 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7117 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7118 | }; |
| 7119 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7120 | void TCETargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7121 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 7122 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7123 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7124 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7125 | if (!FD) return; |
| 7126 | |
| 7127 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7128 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7129 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7130 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7131 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7132 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7133 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7134 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7135 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7136 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7137 | llvm::NamedMDNode *OpenCLMetadata = |
| 7138 | M.getModule().getOrInsertNamedMetadata( |
| 7139 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7140 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7141 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7142 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7143 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7144 | Operands.push_back( |
| 7145 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7146 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7147 | Operands.push_back( |
| 7148 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7149 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7150 | Operands.push_back( |
| 7151 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7152 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7153 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7154 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7155 | // (false) for implementing the work_group_size_hint attr later. |
| 7156 | // 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] | 7157 | Operands.push_back( |
| 7158 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7159 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7160 | } |
| 7161 | } |
| 7162 | } |
| 7163 | } |
| 7164 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7165 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7166 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7167 | //===----------------------------------------------------------------------===// |
| 7168 | // Hexagon ABI Implementation |
| 7169 | //===----------------------------------------------------------------------===// |
| 7170 | |
| 7171 | namespace { |
| 7172 | |
| 7173 | class HexagonABIInfo : public ABIInfo { |
| 7174 | |
| 7175 | |
| 7176 | public: |
| 7177 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7178 | |
| 7179 | private: |
| 7180 | |
| 7181 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7182 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7183 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7184 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7185 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7186 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7187 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7188 | }; |
| 7189 | |
| 7190 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7191 | public: |
| 7192 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7193 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7194 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7195 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7196 | return 29; |
| 7197 | } |
| 7198 | }; |
| 7199 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7200 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7201 | |
| 7202 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7203 | if (!getCXXABI().classifyReturnType(FI)) |
| 7204 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7205 | for (auto &I : FI.arguments()) |
| 7206 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7207 | } |
| 7208 | |
| 7209 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7210 | if (!isAggregateTypeForABI(Ty)) { |
| 7211 | // Treat an enum type as its underlying type. |
| 7212 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7213 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7214 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7215 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 7216 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7217 | } |
| 7218 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7219 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7220 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7221 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7222 | // Ignore empty records. |
| 7223 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7224 | return ABIArgInfo::getIgnore(); |
| 7225 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7226 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7227 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7228 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7229 | // Pass in the smallest viable integer type. |
| 7230 | else if (Size > 32) |
| 7231 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7232 | else if (Size > 16) |
| 7233 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7234 | else if (Size > 8) |
| 7235 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7236 | else |
| 7237 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7238 | } |
| 7239 | |
| 7240 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7241 | if (RetTy->isVoidType()) |
| 7242 | return ABIArgInfo::getIgnore(); |
| 7243 | |
| 7244 | // Large vector types should be returned via memory. |
| 7245 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7246 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7247 | |
| 7248 | if (!isAggregateTypeForABI(RetTy)) { |
| 7249 | // Treat an enum type as its underlying type. |
| 7250 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7251 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7252 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7253 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 7254 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7255 | } |
| 7256 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7257 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7258 | return ABIArgInfo::getIgnore(); |
| 7259 | |
| 7260 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7261 | // are returned indirectly. |
| 7262 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7263 | if (Size <= 64) { |
| 7264 | // Return in the smallest viable integer type. |
| 7265 | if (Size <= 8) |
| 7266 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7267 | if (Size <= 16) |
| 7268 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7269 | if (Size <= 32) |
| 7270 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7271 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7272 | } |
| 7273 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7274 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7275 | } |
| 7276 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7277 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7278 | QualType Ty) const { |
| 7279 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7280 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7281 | getContext().getTypeInfoInChars(Ty), |
| 7282 | CharUnits::fromQuantity(4), |
| 7283 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7284 | } |
| 7285 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7286 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7287 | // Lanai ABI Implementation |
| 7288 | //===----------------------------------------------------------------------===// |
| 7289 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7290 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7291 | class LanaiABIInfo : public DefaultABIInfo { |
| 7292 | public: |
| 7293 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7294 | |
| 7295 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7296 | |
| 7297 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7298 | CCState State(FI.getCallingConvention()); |
| 7299 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7300 | // regparm attribute set. |
| 7301 | if (FI.getHasRegParm()) { |
| 7302 | State.FreeRegs = FI.getRegParm(); |
| 7303 | } else { |
| 7304 | State.FreeRegs = 4; |
| 7305 | } |
| 7306 | |
| 7307 | if (!getCXXABI().classifyReturnType(FI)) |
| 7308 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7309 | for (auto &I : FI.arguments()) |
| 7310 | I.info = classifyArgumentType(I.type, State); |
| 7311 | } |
| 7312 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7313 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7314 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7315 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7316 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7317 | |
| 7318 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7319 | unsigned Size = getContext().getTypeSize(Ty); |
| 7320 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7321 | |
| 7322 | if (SizeInRegs == 0) |
| 7323 | return false; |
| 7324 | |
| 7325 | if (SizeInRegs > State.FreeRegs) { |
| 7326 | State.FreeRegs = 0; |
| 7327 | return false; |
| 7328 | } |
| 7329 | |
| 7330 | State.FreeRegs -= SizeInRegs; |
| 7331 | |
| 7332 | return true; |
| 7333 | } |
| 7334 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7335 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7336 | CCState &State) const { |
| 7337 | if (!ByVal) { |
| 7338 | if (State.FreeRegs) { |
| 7339 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7340 | return getNaturalAlignIndirectInReg(Ty); |
| 7341 | } |
| 7342 | return getNaturalAlignIndirect(Ty, false); |
| 7343 | } |
| 7344 | |
| 7345 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7346 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7347 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7348 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7349 | /*Realign=*/TypeAlign > |
| 7350 | MinABIStackAlignInBytes); |
| 7351 | } |
| 7352 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7353 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7354 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7355 | // Check with the C++ ABI first. |
| 7356 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7357 | if (RT) { |
| 7358 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7359 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7360 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7361 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7362 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7363 | } |
| 7364 | } |
| 7365 | |
| 7366 | if (isAggregateTypeForABI(Ty)) { |
| 7367 | // Structures with flexible arrays are always indirect. |
| 7368 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7369 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7370 | |
| 7371 | // Ignore empty structs/unions. |
| 7372 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7373 | return ABIArgInfo::getIgnore(); |
| 7374 | |
| 7375 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7376 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7377 | if (SizeInRegs <= State.FreeRegs) { |
| 7378 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7379 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7380 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7381 | State.FreeRegs -= SizeInRegs; |
| 7382 | return ABIArgInfo::getDirectInReg(Result); |
| 7383 | } else { |
| 7384 | State.FreeRegs = 0; |
| 7385 | } |
| 7386 | return getIndirectResult(Ty, true, State); |
| 7387 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7388 | |
| 7389 | // Treat an enum type as its underlying type. |
| 7390 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7391 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7392 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7393 | bool InReg = shouldUseInReg(Ty, State); |
| 7394 | if (Ty->isPromotableIntegerType()) { |
| 7395 | if (InReg) |
| 7396 | return ABIArgInfo::getDirectInReg(); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7397 | return ABIArgInfo::getExtend(Ty); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7398 | } |
| 7399 | if (InReg) |
| 7400 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7401 | return ABIArgInfo::getDirect(); |
| 7402 | } |
| 7403 | |
| 7404 | namespace { |
| 7405 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7406 | public: |
| 7407 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7408 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7409 | }; |
| 7410 | } |
| 7411 | |
| 7412 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7413 | // AMDGPU ABI Implementation |
| 7414 | //===----------------------------------------------------------------------===// |
| 7415 | |
| 7416 | namespace { |
| 7417 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7418 | class AMDGPUABIInfo final : public DefaultABIInfo { |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7419 | private: |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7420 | static const unsigned MaxNumRegsForArgsRet = 16; |
| 7421 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7422 | unsigned numRegsForType(QualType Ty) const; |
| 7423 | |
| 7424 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 7425 | bool isHomogeneousAggregateSmallEnough(const Type *Base, |
| 7426 | uint64_t Members) const override; |
| 7427 | |
| 7428 | public: |
| 7429 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : |
| 7430 | DefaultABIInfo(CGT) {} |
| 7431 | |
| 7432 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7433 | ABIArgInfo classifyKernelArgumentType(QualType Ty) const; |
| 7434 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7435 | |
| 7436 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7437 | }; |
| 7438 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7439 | bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 7440 | return true; |
| 7441 | } |
| 7442 | |
| 7443 | bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( |
| 7444 | const Type *Base, uint64_t Members) const { |
| 7445 | uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; |
| 7446 | |
| 7447 | // Homogeneous Aggregates may occupy at most 16 registers. |
| 7448 | return Members * NumRegs <= MaxNumRegsForArgsRet; |
| 7449 | } |
| 7450 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7451 | /// Estimate number of registers the type will use when passed in registers. |
| 7452 | unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { |
| 7453 | unsigned NumRegs = 0; |
| 7454 | |
| 7455 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 7456 | // Compute from the number of elements. The reported size is based on the |
| 7457 | // in-memory size, which includes the padding 4th element for 3-vectors. |
| 7458 | QualType EltTy = VT->getElementType(); |
| 7459 | unsigned EltSize = getContext().getTypeSize(EltTy); |
| 7460 | |
| 7461 | // 16-bit element vectors should be passed as packed. |
| 7462 | if (EltSize == 16) |
| 7463 | return (VT->getNumElements() + 1) / 2; |
| 7464 | |
| 7465 | unsigned EltNumRegs = (EltSize + 31) / 32; |
| 7466 | return EltNumRegs * VT->getNumElements(); |
| 7467 | } |
| 7468 | |
| 7469 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7470 | const RecordDecl *RD = RT->getDecl(); |
| 7471 | assert(!RD->hasFlexibleArrayMember()); |
| 7472 | |
| 7473 | for (const FieldDecl *Field : RD->fields()) { |
| 7474 | QualType FieldTy = Field->getType(); |
| 7475 | NumRegs += numRegsForType(FieldTy); |
| 7476 | } |
| 7477 | |
| 7478 | return NumRegs; |
| 7479 | } |
| 7480 | |
| 7481 | return (getContext().getTypeSize(Ty) + 31) / 32; |
| 7482 | } |
| 7483 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7484 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7485 | llvm::CallingConv::ID CC = FI.getCallingConvention(); |
| 7486 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7487 | if (!getCXXABI().classifyReturnType(FI)) |
| 7488 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7489 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7490 | unsigned NumRegsLeft = MaxNumRegsForArgsRet; |
| 7491 | for (auto &Arg : FI.arguments()) { |
| 7492 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) { |
| 7493 | Arg.info = classifyKernelArgumentType(Arg.type); |
| 7494 | } else { |
| 7495 | Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); |
| 7496 | } |
| 7497 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7498 | } |
| 7499 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7500 | ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { |
| 7501 | if (isAggregateTypeForABI(RetTy)) { |
| 7502 | // Records with non-trivial destructors/copy-constructors should not be |
| 7503 | // returned by value. |
| 7504 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 7505 | // Ignore empty structs/unions. |
| 7506 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7507 | return ABIArgInfo::getIgnore(); |
| 7508 | |
| 7509 | // Lower single-element structs to just return a regular value. |
| 7510 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 7511 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7512 | |
| 7513 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
| 7514 | const RecordDecl *RD = RT->getDecl(); |
| 7515 | if (RD->hasFlexibleArrayMember()) |
| 7516 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7517 | } |
| 7518 | |
| 7519 | // Pack aggregates <= 4 bytes into single VGPR or pair. |
| 7520 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7521 | if (Size <= 16) |
| 7522 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7523 | |
| 7524 | if (Size <= 32) |
| 7525 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7526 | |
| 7527 | if (Size <= 64) { |
| 7528 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7529 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7530 | } |
| 7531 | |
| 7532 | if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) |
| 7533 | return ABIArgInfo::getDirect(); |
| 7534 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7535 | } |
| 7536 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7537 | // Otherwise just do the default thing. |
| 7538 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7539 | } |
| 7540 | |
| 7541 | /// For kernels all parameters are really passed in a special buffer. It doesn't |
| 7542 | /// make sense to pass anything byval, so everything must be direct. |
| 7543 | ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { |
| 7544 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7545 | |
| 7546 | // TODO: Can we omit empty structs? |
| 7547 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7548 | // Coerce single element structs to its element. |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7549 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7550 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7551 | |
| 7552 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7553 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7554 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 7555 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 7556 | } |
| 7557 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7558 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, |
| 7559 | unsigned &NumRegsLeft) const { |
| 7560 | assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); |
| 7561 | |
| 7562 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7563 | |
| 7564 | if (isAggregateTypeForABI(Ty)) { |
| 7565 | // Records with non-trivial destructors/copy-constructors should not be |
| 7566 | // passed by value. |
| 7567 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7568 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7569 | |
| 7570 | // Ignore empty structs/unions. |
| 7571 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7572 | return ABIArgInfo::getIgnore(); |
| 7573 | |
| 7574 | // Lower single-element structs to just pass a regular value. TODO: We |
| 7575 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 7576 | // though watch out for things like bitfields. |
| 7577 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7578 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7579 | |
| 7580 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7581 | const RecordDecl *RD = RT->getDecl(); |
| 7582 | if (RD->hasFlexibleArrayMember()) |
| 7583 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7584 | } |
| 7585 | |
| 7586 | // Pack aggregates <= 8 bytes into single VGPR or pair. |
| 7587 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7588 | if (Size <= 64) { |
| 7589 | unsigned NumRegs = (Size + 31) / 32; |
| 7590 | NumRegsLeft -= std::min(NumRegsLeft, NumRegs); |
| 7591 | |
| 7592 | if (Size <= 16) |
| 7593 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7594 | |
| 7595 | if (Size <= 32) |
| 7596 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7597 | |
| 7598 | // XXX: Should this be i64 instead, and should the limit increase? |
| 7599 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7600 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7601 | } |
| 7602 | |
| 7603 | if (NumRegsLeft > 0) { |
| 7604 | unsigned NumRegs = numRegsForType(Ty); |
| 7605 | if (NumRegsLeft >= NumRegs) { |
| 7606 | NumRegsLeft -= NumRegs; |
| 7607 | return ABIArgInfo::getDirect(); |
| 7608 | } |
| 7609 | } |
| 7610 | } |
| 7611 | |
| 7612 | // Otherwise just do the default thing. |
| 7613 | ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); |
| 7614 | if (!ArgInfo.isIndirect()) { |
| 7615 | unsigned NumRegs = numRegsForType(Ty); |
| 7616 | NumRegsLeft -= std::min(NumRegs, NumRegsLeft); |
| 7617 | } |
| 7618 | |
| 7619 | return ArgInfo; |
| 7620 | } |
| 7621 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7622 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7623 | public: |
| 7624 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7625 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7626 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7627 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7628 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7629 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7630 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7631 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7632 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7633 | LangAS getASTAllocaAddressSpace() const override { |
| 7634 | return getLangASFromTargetAS( |
| 7635 | getABIInfo().getDataLayout().getAllocaAddrSpace()); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7636 | } |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7637 | LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7638 | const VarDecl *D) const override; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7639 | llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S, |
| 7640 | llvm::LLVMContext &C) const override; |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 7641 | llvm::Function * |
| 7642 | createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 7643 | llvm::Function *BlockInvokeFunc, |
| 7644 | llvm::Value *BlockLiteral) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7645 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7646 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7647 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7648 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7649 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 7650 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7651 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7652 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7653 | if (!FD) |
| 7654 | return; |
| 7655 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7656 | llvm::Function *F = cast<llvm::Function>(GV); |
| 7657 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7658 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 7659 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
| 7660 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 7661 | if (ReqdWGS || FlatWGS) { |
| 7662 | unsigned Min = FlatWGS ? FlatWGS->getMin() : 0; |
| 7663 | unsigned Max = FlatWGS ? FlatWGS->getMax() : 0; |
| 7664 | if (ReqdWGS && Min == 0 && Max == 0) |
| 7665 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7666 | |
| 7667 | if (Min != 0) { |
| 7668 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 7669 | |
| 7670 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 7671 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 7672 | } else |
| 7673 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7674 | } |
| 7675 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7676 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 7677 | unsigned Min = Attr->getMin(); |
| 7678 | unsigned Max = Attr->getMax(); |
| 7679 | |
| 7680 | if (Min != 0) { |
| 7681 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 7682 | |
| 7683 | std::string AttrVal = llvm::utostr(Min); |
| 7684 | if (Max != 0) |
| 7685 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 7686 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 7687 | } else |
| 7688 | assert(Max == 0 && "Max must be zero"); |
| 7689 | } |
| 7690 | |
| 7691 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7692 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7693 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7694 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7695 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 7696 | } |
| 7697 | |
| 7698 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7699 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 7700 | |
| 7701 | if (NumVGPR != 0) |
| 7702 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7703 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7704 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7705 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7706 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7707 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7708 | } |
| 7709 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7710 | // Currently LLVM assumes null pointers always have value 0, |
| 7711 | // which results in incorrectly transformed IR. Therefore, instead of |
| 7712 | // emitting null pointers in private and local address spaces, a null |
| 7713 | // pointer in generic address space is emitted which is casted to a |
| 7714 | // pointer in local or private address space. |
| 7715 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 7716 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 7717 | QualType QT) const { |
| 7718 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 7719 | return llvm::ConstantPointerNull::get(PT); |
| 7720 | |
| 7721 | auto &Ctx = CGM.getContext(); |
| 7722 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 7723 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 7724 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 7725 | llvm::ConstantPointerNull::get(NPT), PT); |
| 7726 | } |
| 7727 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7728 | LangAS |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7729 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7730 | const VarDecl *D) const { |
| 7731 | assert(!CGM.getLangOpts().OpenCL && |
| 7732 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 7733 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7734 | LangAS DefaultGlobalAS = getLangASFromTargetAS( |
| 7735 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7736 | if (!D) |
| 7737 | return DefaultGlobalAS; |
| 7738 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7739 | LangAS AddrSpace = D->getType().getAddressSpace(); |
| 7740 | assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7741 | if (AddrSpace != LangAS::Default) |
| 7742 | return AddrSpace; |
| 7743 | |
| 7744 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 7745 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 7746 | return ConstAS.getValue(); |
| 7747 | } |
| 7748 | return DefaultGlobalAS; |
| 7749 | } |
| 7750 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7751 | llvm::SyncScope::ID |
| 7752 | AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, |
| 7753 | llvm::LLVMContext &C) const { |
| 7754 | StringRef Name; |
| 7755 | switch (S) { |
| 7756 | case SyncScope::OpenCLWorkGroup: |
| 7757 | Name = "workgroup"; |
| 7758 | break; |
| 7759 | case SyncScope::OpenCLDevice: |
| 7760 | Name = "agent"; |
| 7761 | break; |
| 7762 | case SyncScope::OpenCLAllSVMDevices: |
| 7763 | Name = ""; |
| 7764 | break; |
| 7765 | case SyncScope::OpenCLSubGroup: |
| 7766 | Name = "subgroup"; |
| 7767 | } |
| 7768 | return C.getOrInsertSyncScopeID(Name); |
| 7769 | } |
| 7770 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7771 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 7772 | // SPARC v8 ABI Implementation. |
| 7773 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7774 | // |
| 7775 | // Ensures that complex values are passed in registers. |
| 7776 | // |
| 7777 | namespace { |
| 7778 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 7779 | public: |
| 7780 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7781 | |
| 7782 | private: |
| 7783 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7784 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7785 | }; |
| 7786 | } // end anonymous namespace |
| 7787 | |
| 7788 | |
| 7789 | ABIArgInfo |
| 7790 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 7791 | if (Ty->isAnyComplexType()) { |
| 7792 | return ABIArgInfo::getDirect(); |
| 7793 | } |
| 7794 | else { |
| 7795 | return DefaultABIInfo::classifyReturnType(Ty); |
| 7796 | } |
| 7797 | } |
| 7798 | |
| 7799 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7800 | |
| 7801 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7802 | for (auto &Arg : FI.arguments()) |
| 7803 | Arg.info = classifyArgumentType(Arg.type); |
| 7804 | } |
| 7805 | |
| 7806 | namespace { |
| 7807 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7808 | public: |
| 7809 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7810 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 7811 | }; |
| 7812 | } // end anonymous namespace |
| 7813 | |
| 7814 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7815 | // SPARC v9 ABI Implementation. |
| 7816 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7817 | // |
| 7818 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 7819 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 7820 | // the array, structs larger than 16 bytes are passed indirectly. |
| 7821 | // |
| 7822 | // One case requires special care: |
| 7823 | // |
| 7824 | // struct mixed { |
| 7825 | // int i; |
| 7826 | // float f; |
| 7827 | // }; |
| 7828 | // |
| 7829 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 7830 | // parameter array, but the int is passed in an integer register, and the float |
| 7831 | // is passed in a floating point register. This is represented as two arguments |
| 7832 | // with the LLVM IR inreg attribute: |
| 7833 | // |
| 7834 | // declare void f(i32 inreg %i, float inreg %f) |
| 7835 | // |
| 7836 | // The code generator will only allocate 4 bytes from the parameter array for |
| 7837 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 7838 | // bytes. |
| 7839 | // |
| 7840 | namespace { |
| 7841 | class SparcV9ABIInfo : public ABIInfo { |
| 7842 | public: |
| 7843 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7844 | |
| 7845 | private: |
| 7846 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7847 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7848 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7849 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7850 | |
| 7851 | // Coercion type builder for structs passed in registers. The coercion type |
| 7852 | // serves two purposes: |
| 7853 | // |
| 7854 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 7855 | // in registers. |
| 7856 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 7857 | // code generator knows to pass them in floating point registers. |
| 7858 | // |
| 7859 | // We also compute the InReg flag which indicates that the struct contains |
| 7860 | // aligned 32-bit floats. |
| 7861 | // |
| 7862 | struct CoerceBuilder { |
| 7863 | llvm::LLVMContext &Context; |
| 7864 | const llvm::DataLayout &DL; |
| 7865 | SmallVector<llvm::Type*, 8> Elems; |
| 7866 | uint64_t Size; |
| 7867 | bool InReg; |
| 7868 | |
| 7869 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 7870 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 7871 | |
| 7872 | // Pad Elems with integers until Size is ToSize. |
| 7873 | void pad(uint64_t ToSize) { |
| 7874 | assert(ToSize >= Size && "Cannot remove elements"); |
| 7875 | if (ToSize == Size) |
| 7876 | return; |
| 7877 | |
| 7878 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7879 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7880 | if (Aligned > Size && Aligned <= ToSize) { |
| 7881 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 7882 | Size = Aligned; |
| 7883 | } |
| 7884 | |
| 7885 | // Add whole 64-bit words. |
| 7886 | while (Size + 64 <= ToSize) { |
| 7887 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 7888 | Size += 64; |
| 7889 | } |
| 7890 | |
| 7891 | // Final in-word padding. |
| 7892 | if (Size < ToSize) { |
| 7893 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 7894 | Size = ToSize; |
| 7895 | } |
| 7896 | } |
| 7897 | |
| 7898 | // Add a floating point element at Offset. |
| 7899 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 7900 | // Unaligned floats are treated as integers. |
| 7901 | if (Offset % Bits) |
| 7902 | return; |
| 7903 | // The InReg flag is only required if there are any floats < 64 bits. |
| 7904 | if (Bits < 64) |
| 7905 | InReg = true; |
| 7906 | pad(Offset); |
| 7907 | Elems.push_back(Ty); |
| 7908 | Size = Offset + Bits; |
| 7909 | } |
| 7910 | |
| 7911 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 7912 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 7913 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 7914 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 7915 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 7916 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 7917 | switch (ElemTy->getTypeID()) { |
| 7918 | case llvm::Type::StructTyID: |
| 7919 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 7920 | break; |
| 7921 | case llvm::Type::FloatTyID: |
| 7922 | addFloat(ElemOffset, ElemTy, 32); |
| 7923 | break; |
| 7924 | case llvm::Type::DoubleTyID: |
| 7925 | addFloat(ElemOffset, ElemTy, 64); |
| 7926 | break; |
| 7927 | case llvm::Type::FP128TyID: |
| 7928 | addFloat(ElemOffset, ElemTy, 128); |
| 7929 | break; |
| 7930 | case llvm::Type::PointerTyID: |
| 7931 | if (ElemOffset % 64 == 0) { |
| 7932 | pad(ElemOffset); |
| 7933 | Elems.push_back(ElemTy); |
| 7934 | Size += 64; |
| 7935 | } |
| 7936 | break; |
| 7937 | default: |
| 7938 | break; |
| 7939 | } |
| 7940 | } |
| 7941 | } |
| 7942 | |
| 7943 | // Check if Ty is a usable substitute for the coercion type. |
| 7944 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 7945 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7946 | } |
| 7947 | |
| 7948 | // Get the coercion type as a literal struct type. |
| 7949 | llvm::Type *getType() const { |
| 7950 | if (Elems.size() == 1) |
| 7951 | return Elems.front(); |
| 7952 | else |
| 7953 | return llvm::StructType::get(Context, Elems); |
| 7954 | } |
| 7955 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7956 | }; |
| 7957 | } // end anonymous namespace |
| 7958 | |
| 7959 | ABIArgInfo |
| 7960 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 7961 | if (Ty->isVoidType()) |
| 7962 | return ABIArgInfo::getIgnore(); |
| 7963 | |
| 7964 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7965 | |
| 7966 | // Anything too big to fit in registers is passed with an explicit indirect |
| 7967 | // pointer / sret pointer. |
| 7968 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7969 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7970 | |
| 7971 | // Treat an enum type as its underlying type. |
| 7972 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7973 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7974 | |
| 7975 | // Integer types smaller than a register are extended. |
| 7976 | if (Size < 64 && Ty->isIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7977 | return ABIArgInfo::getExtend(Ty); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7978 | |
| 7979 | // Other non-aggregates go in registers. |
| 7980 | if (!isAggregateTypeForABI(Ty)) |
| 7981 | return ABIArgInfo::getDirect(); |
| 7982 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7983 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 7984 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 7985 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7986 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 7987 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7988 | // 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] | 7989 | // Build a coercion type from the LLVM struct type. |
| 7990 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7991 | if (!StrTy) |
| 7992 | return ABIArgInfo::getDirect(); |
| 7993 | |
| 7994 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 7995 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7996 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7997 | |
| 7998 | // Try to use the original type for coercion. |
| 7999 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 8000 | |
| 8001 | if (CB.InReg) |
| 8002 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 8003 | else |
| 8004 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8005 | } |
| 8006 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8007 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8008 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8009 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 8010 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8011 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8012 | AI.setCoerceToType(ArgTy); |
| 8013 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8014 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8015 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8016 | CGBuilderTy &Builder = CGF.Builder; |
| 8017 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 8018 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 8019 | |
| 8020 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 8021 | |
| 8022 | Address ArgAddr = Address::invalid(); |
| 8023 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8024 | switch (AI.getKind()) { |
| 8025 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8026 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8027 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8028 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8029 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8030 | case ABIArgInfo::Extend: { |
| 8031 | Stride = SlotSize; |
| 8032 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 8033 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8034 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8035 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8036 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8037 | case ABIArgInfo::Direct: { |
| 8038 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8039 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8040 | ArgAddr = Addr; |
| 8041 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8042 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8043 | |
| 8044 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8045 | Stride = SlotSize; |
| 8046 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 8047 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 8048 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8049 | break; |
| 8050 | |
| 8051 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8052 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8053 | } |
| 8054 | |
| 8055 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8056 | llvm::Value *NextPtr = |
| 8057 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 8058 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8059 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8060 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8061 | } |
| 8062 | |
| 8063 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8064 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 8065 | for (auto &I : FI.arguments()) |
| 8066 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8067 | } |
| 8068 | |
| 8069 | namespace { |
| 8070 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8071 | public: |
| 8072 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8073 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8074 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8075 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8076 | return 14; |
| 8077 | } |
| 8078 | |
| 8079 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8080 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8081 | }; |
| 8082 | } // end anonymous namespace |
| 8083 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8084 | bool |
| 8085 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 8086 | llvm::Value *Address) const { |
| 8087 | // This is calculated from the LLVM and GCC tables and verified |
| 8088 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 8089 | |
| 8090 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 8091 | |
| 8092 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 8093 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 8094 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 8095 | |
| 8096 | // 0-31: the 8-byte general-purpose registers |
| 8097 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 8098 | |
| 8099 | // 32-63: f0-31, the 4-byte floating-point registers |
| 8100 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 8101 | |
| 8102 | // Y = 64 |
| 8103 | // PSR = 65 |
| 8104 | // WIM = 66 |
| 8105 | // TBR = 67 |
| 8106 | // PC = 68 |
| 8107 | // NPC = 69 |
| 8108 | // FSR = 70 |
| 8109 | // CSR = 71 |
| 8110 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8111 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8112 | // 72-87: d0-15, the 8-byte floating-point registers |
| 8113 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 8114 | |
| 8115 | return false; |
| 8116 | } |
| 8117 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8118 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8119 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8120 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8121 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8122 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8123 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8124 | |
| 8125 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 8126 | /// it by reference between functions that append to it. |
| 8127 | typedef llvm::SmallString<128> SmallStringEnc; |
| 8128 | |
| 8129 | /// TypeStringCache caches the meta encodings of Types. |
| 8130 | /// |
| 8131 | /// The reason for caching TypeStrings is two fold: |
| 8132 | /// 1. To cache a type's encoding for later uses; |
| 8133 | /// 2. As a means to break recursive member type inclusion. |
| 8134 | /// |
| 8135 | /// A cache Entry can have a Status of: |
| 8136 | /// NonRecursive: The type encoding is not recursive; |
| 8137 | /// Recursive: The type encoding is recursive; |
| 8138 | /// Incomplete: An incomplete TypeString; |
| 8139 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 8140 | /// Recursive type encoding. |
| 8141 | /// |
| 8142 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 8143 | /// as possible. Whilst it may contain types which are recursive, the type |
| 8144 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 8145 | /// the type is encountered. |
| 8146 | /// |
| 8147 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 8148 | /// possible. The type itself is recursive and it may contain other types which |
| 8149 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 8150 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 8151 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 8152 | /// |
| 8153 | /// An Incomplete entry is always a RecordType and only encodes its |
| 8154 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 8155 | /// are placed into the cache during type expansion as a means to identify and |
| 8156 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 8157 | /// the entry becomes IncompleteUsed. |
| 8158 | /// |
| 8159 | /// During the expansion of a RecordType's members: |
| 8160 | /// |
| 8161 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 8162 | /// cached encoding is used; |
| 8163 | /// |
| 8164 | /// If the cache contains a Recursive encoding for the member type, the |
| 8165 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 8166 | /// |
| 8167 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 8168 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 8169 | /// |
| 8170 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 8171 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 8172 | /// it is swapped back in; |
| 8173 | /// |
| 8174 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 8175 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 8176 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 8177 | /// |
| 8178 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 8179 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 8180 | /// Else the member is part of a recursive type and thus the recursion has |
| 8181 | /// been exited too soon for the encoding to be correct for the member. |
| 8182 | /// |
| 8183 | class TypeStringCache { |
| 8184 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 8185 | struct Entry { |
| 8186 | std::string Str; // The encoded TypeString for the type. |
| 8187 | enum Status State; // Information about the encoding in 'Str'. |
| 8188 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 8189 | // during the expansion of RecordType's members. |
| 8190 | }; |
| 8191 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 8192 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 8193 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 8194 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8195 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8196 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 8197 | bool removeIncomplete(const IdentifierInfo *ID); |
| 8198 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8199 | bool IsRecursive); |
| 8200 | StringRef lookupStr(const IdentifierInfo *ID); |
| 8201 | }; |
| 8202 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8203 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8204 | /// FieldEncoding is a helper for this ordering process. |
| 8205 | class FieldEncoding { |
| 8206 | bool HasName; |
| 8207 | std::string Enc; |
| 8208 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8209 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8210 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8211 | bool operator<(const FieldEncoding &rhs) const { |
| 8212 | if (HasName != rhs.HasName) return HasName; |
| 8213 | return Enc < rhs.Enc; |
| 8214 | } |
| 8215 | }; |
| 8216 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8217 | class XCoreABIInfo : public DefaultABIInfo { |
| 8218 | public: |
| 8219 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8220 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8221 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8222 | }; |
| 8223 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8224 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8225 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8226 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8227 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8228 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 8229 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8230 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8231 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8232 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8233 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8234 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 8235 | // TODO: this implementation is likely now redundant with the default |
| 8236 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8237 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8238 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8239 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8240 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8241 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8242 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 8243 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8244 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8245 | // Handle the argument. |
| 8246 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8247 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8248 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8249 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8250 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8251 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8252 | |
| 8253 | Address Val = Address::invalid(); |
| 8254 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8255 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8256 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8257 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8258 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8259 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8260 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8261 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 8262 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8263 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8264 | case ABIArgInfo::Extend: |
| 8265 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8266 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 8267 | ArgSize = CharUnits::fromQuantity( |
| 8268 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8269 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8270 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8271 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8272 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 8273 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 8274 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8275 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8276 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8277 | |
| 8278 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8279 | if (!ArgSize.isZero()) { |
| 8280 | llvm::Value *APN = |
| 8281 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 8282 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8283 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8284 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8285 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8286 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8287 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8288 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8289 | /// into the cache as a means to identify and break recursion. |
| 8290 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8291 | /// be reinserted by removeIncomplete(). |
| 8292 | /// All other types of encoding should have been used rather than arriving here. |
| 8293 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8294 | std::string StubEnc) { |
| 8295 | if (!ID) |
| 8296 | return; |
| 8297 | Entry &E = Map[ID]; |
| 8298 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8299 | "Incorrectly use of addIncomplete"); |
| 8300 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8301 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8302 | E.Str.swap(StubEnc); |
| 8303 | E.State = Incomplete; |
| 8304 | ++IncompleteCount; |
| 8305 | } |
| 8306 | |
| 8307 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8308 | /// must be removed from the cache. |
| 8309 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8310 | /// Returns true if the RecordType was defined recursively. |
| 8311 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8312 | if (!ID) |
| 8313 | return false; |
| 8314 | auto I = Map.find(ID); |
| 8315 | assert(I != Map.end() && "Entry not present"); |
| 8316 | Entry &E = I->second; |
| 8317 | assert( (E.State == Incomplete || |
| 8318 | E.State == IncompleteUsed) && |
| 8319 | "Entry must be an incomplete type"); |
| 8320 | bool IsRecursive = false; |
| 8321 | if (E.State == IncompleteUsed) { |
| 8322 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8323 | IsRecursive = true; |
| 8324 | --IncompleteUsedCount; |
| 8325 | } |
| 8326 | if (E.Swapped.empty()) |
| 8327 | Map.erase(I); |
| 8328 | else { |
| 8329 | // Swap the Recursive back. |
| 8330 | E.Swapped.swap(E.Str); |
| 8331 | E.Swapped.clear(); |
| 8332 | E.State = Recursive; |
| 8333 | } |
| 8334 | --IncompleteCount; |
| 8335 | return IsRecursive; |
| 8336 | } |
| 8337 | |
| 8338 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8339 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8340 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8341 | bool IsRecursive) { |
| 8342 | if (!ID || IncompleteUsedCount) |
| 8343 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8344 | Entry &E = Map[ID]; |
| 8345 | if (IsRecursive && !E.Str.empty()) { |
| 8346 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8347 | "This is not the same Recursive entry"); |
| 8348 | // The parent container was not recursive after all, so we could have used |
| 8349 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8350 | // we started viz: IncompleteCount!=0. |
| 8351 | return; |
| 8352 | } |
| 8353 | assert(E.Str.empty() && "Entry already present"); |
| 8354 | E.Str = Str.str(); |
| 8355 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8356 | } |
| 8357 | |
| 8358 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8359 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8360 | /// encoding is Recursive, return an empty StringRef. |
| 8361 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8362 | if (!ID) |
| 8363 | return StringRef(); // We have no key. |
| 8364 | auto I = Map.find(ID); |
| 8365 | if (I == Map.end()) |
| 8366 | return StringRef(); // We have no encoding. |
| 8367 | Entry &E = I->second; |
| 8368 | if (E.State == Recursive && IncompleteCount) |
| 8369 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8370 | |
| 8371 | if (E.State == Incomplete) { |
| 8372 | // The incomplete type is being used to break out of recursion. |
| 8373 | E.State = IncompleteUsed; |
| 8374 | ++IncompleteUsedCount; |
| 8375 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8376 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8377 | } |
| 8378 | |
| 8379 | /// The XCore ABI includes a type information section that communicates symbol |
| 8380 | /// type information to the linker. The linker uses this information to verify |
| 8381 | /// safety/correctness of things such as array bound and pointers et al. |
| 8382 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8383 | /// This type information (TypeString) is emitted into meta data for all global |
| 8384 | /// symbols: definitions, declarations, functions & variables. |
| 8385 | /// |
| 8386 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8387 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8388 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8389 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8390 | /// |
| 8391 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8392 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8393 | |
| 8394 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8395 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8396 | CodeGen::CodeGenModule &CGM) const { |
| 8397 | SmallStringEnc Enc; |
| 8398 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8399 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8400 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8401 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8402 | llvm::NamedMDNode *MD = |
| 8403 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8404 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8405 | } |
| 8406 | } |
| 8407 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8408 | //===----------------------------------------------------------------------===// |
| 8409 | // SPIR ABI Implementation |
| 8410 | //===----------------------------------------------------------------------===// |
| 8411 | |
| 8412 | namespace { |
| 8413 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8414 | public: |
| 8415 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8416 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8417 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8418 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8419 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8420 | } // End anonymous namespace. |
| 8421 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8422 | namespace clang { |
| 8423 | namespace CodeGen { |
| 8424 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8425 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8426 | SPIRABI.computeInfo(FI); |
| 8427 | } |
| 8428 | } |
| 8429 | } |
| 8430 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8431 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8432 | return llvm::CallingConv::SPIR_KERNEL; |
| 8433 | } |
| 8434 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8435 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8436 | const CodeGen::CodeGenModule &CGM, |
| 8437 | TypeStringCache &TSC); |
| 8438 | |
| 8439 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8440 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8441 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8442 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8443 | const RecordDecl *RD, |
| 8444 | const CodeGen::CodeGenModule &CGM, |
| 8445 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8446 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8447 | SmallStringEnc Enc; |
| 8448 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8449 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8450 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8451 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8452 | Enc += "b("; |
| 8453 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8454 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8455 | Enc += ':'; |
| 8456 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8457 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8458 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8459 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8460 | Enc += ')'; |
| 8461 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 8462 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8463 | } |
| 8464 | return true; |
| 8465 | } |
| 8466 | |
| 8467 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 8468 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 8469 | /// Union types have their fields ordered according to the ABI. |
| 8470 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 8471 | const CodeGen::CodeGenModule &CGM, |
| 8472 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 8473 | // Append the cached TypeString if we have one. |
| 8474 | StringRef TypeString = TSC.lookupStr(ID); |
| 8475 | if (!TypeString.empty()) { |
| 8476 | Enc += TypeString; |
| 8477 | return true; |
| 8478 | } |
| 8479 | |
| 8480 | // Start to emit an incomplete TypeString. |
| 8481 | size_t Start = Enc.size(); |
| 8482 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 8483 | Enc += '('; |
| 8484 | if (ID) |
| 8485 | Enc += ID->getName(); |
| 8486 | Enc += "){"; |
| 8487 | |
| 8488 | // We collect all encoded fields and order as necessary. |
| 8489 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8490 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 8491 | if (RD && !RD->field_empty()) { |
| 8492 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 8493 | // so that recursive calls to this RecordType will use it whilst building a |
| 8494 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8495 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8496 | std::string StubEnc(Enc.substr(Start).str()); |
| 8497 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 8498 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 8499 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 8500 | (void) TSC.removeIncomplete(ID); |
| 8501 | return false; |
| 8502 | } |
| 8503 | IsRecursive = TSC.removeIncomplete(ID); |
| 8504 | // The ABI requires unions to be sorted but not structures. |
| 8505 | // See FieldEncoding::operator< for sort algorithm. |
| 8506 | if (RT->isUnionType()) |
| 8507 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8508 | // We can now complete the TypeString. |
| 8509 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8510 | for (unsigned I = 0; I != E; ++I) { |
| 8511 | if (I) |
| 8512 | Enc += ','; |
| 8513 | Enc += FE[I].str(); |
| 8514 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8515 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8516 | Enc += '}'; |
| 8517 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 8518 | return true; |
| 8519 | } |
| 8520 | |
| 8521 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 8522 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 8523 | TypeStringCache &TSC, |
| 8524 | const IdentifierInfo *ID) { |
| 8525 | // Append the cached TypeString if we have one. |
| 8526 | StringRef TypeString = TSC.lookupStr(ID); |
| 8527 | if (!TypeString.empty()) { |
| 8528 | Enc += TypeString; |
| 8529 | return true; |
| 8530 | } |
| 8531 | |
| 8532 | size_t Start = Enc.size(); |
| 8533 | Enc += "e("; |
| 8534 | if (ID) |
| 8535 | Enc += ID->getName(); |
| 8536 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8537 | |
| 8538 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8539 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8540 | SmallVector<FieldEncoding, 16> FE; |
| 8541 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 8542 | ++I) { |
| 8543 | SmallStringEnc EnumEnc; |
| 8544 | EnumEnc += "m("; |
| 8545 | EnumEnc += I->getName(); |
| 8546 | EnumEnc += "){"; |
| 8547 | I->getInitVal().toString(EnumEnc); |
| 8548 | EnumEnc += '}'; |
| 8549 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 8550 | } |
| 8551 | std::sort(FE.begin(), FE.end()); |
| 8552 | unsigned E = FE.size(); |
| 8553 | for (unsigned I = 0; I != E; ++I) { |
| 8554 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8555 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8556 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8557 | } |
| 8558 | } |
| 8559 | Enc += '}'; |
| 8560 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 8561 | return true; |
| 8562 | } |
| 8563 | |
| 8564 | /// Appends type's qualifier to Enc. |
| 8565 | /// This is done prior to appending the type's encoding. |
| 8566 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 8567 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 8568 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8569 | int Lookup = 0; |
| 8570 | if (QT.isConstQualified()) |
| 8571 | Lookup += 1<<0; |
| 8572 | if (QT.isRestrictQualified()) |
| 8573 | Lookup += 1<<1; |
| 8574 | if (QT.isVolatileQualified()) |
| 8575 | Lookup += 1<<2; |
| 8576 | Enc += Table[Lookup]; |
| 8577 | } |
| 8578 | |
| 8579 | /// Appends built-in types to Enc. |
| 8580 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 8581 | const char *EncType; |
| 8582 | switch (BT->getKind()) { |
| 8583 | case BuiltinType::Void: |
| 8584 | EncType = "0"; |
| 8585 | break; |
| 8586 | case BuiltinType::Bool: |
| 8587 | EncType = "b"; |
| 8588 | break; |
| 8589 | case BuiltinType::Char_U: |
| 8590 | EncType = "uc"; |
| 8591 | break; |
| 8592 | case BuiltinType::UChar: |
| 8593 | EncType = "uc"; |
| 8594 | break; |
| 8595 | case BuiltinType::SChar: |
| 8596 | EncType = "sc"; |
| 8597 | break; |
| 8598 | case BuiltinType::UShort: |
| 8599 | EncType = "us"; |
| 8600 | break; |
| 8601 | case BuiltinType::Short: |
| 8602 | EncType = "ss"; |
| 8603 | break; |
| 8604 | case BuiltinType::UInt: |
| 8605 | EncType = "ui"; |
| 8606 | break; |
| 8607 | case BuiltinType::Int: |
| 8608 | EncType = "si"; |
| 8609 | break; |
| 8610 | case BuiltinType::ULong: |
| 8611 | EncType = "ul"; |
| 8612 | break; |
| 8613 | case BuiltinType::Long: |
| 8614 | EncType = "sl"; |
| 8615 | break; |
| 8616 | case BuiltinType::ULongLong: |
| 8617 | EncType = "ull"; |
| 8618 | break; |
| 8619 | case BuiltinType::LongLong: |
| 8620 | EncType = "sll"; |
| 8621 | break; |
| 8622 | case BuiltinType::Float: |
| 8623 | EncType = "ft"; |
| 8624 | break; |
| 8625 | case BuiltinType::Double: |
| 8626 | EncType = "d"; |
| 8627 | break; |
| 8628 | case BuiltinType::LongDouble: |
| 8629 | EncType = "ld"; |
| 8630 | break; |
| 8631 | default: |
| 8632 | return false; |
| 8633 | } |
| 8634 | Enc += EncType; |
| 8635 | return true; |
| 8636 | } |
| 8637 | |
| 8638 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 8639 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 8640 | const CodeGen::CodeGenModule &CGM, |
| 8641 | TypeStringCache &TSC) { |
| 8642 | Enc += "p("; |
| 8643 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 8644 | return false; |
| 8645 | Enc += ')'; |
| 8646 | return true; |
| 8647 | } |
| 8648 | |
| 8649 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8650 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 8651 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8652 | const CodeGen::CodeGenModule &CGM, |
| 8653 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 8654 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 8655 | return false; |
| 8656 | Enc += "a("; |
| 8657 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 8658 | CAT->getSize().toStringUnsigned(Enc); |
| 8659 | else |
| 8660 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 8661 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8662 | // The Qualifiers should be attached to the type rather than the array. |
| 8663 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8664 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 8665 | return false; |
| 8666 | Enc += ')'; |
| 8667 | return true; |
| 8668 | } |
| 8669 | |
| 8670 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 8671 | /// and the arguments. |
| 8672 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 8673 | const CodeGen::CodeGenModule &CGM, |
| 8674 | TypeStringCache &TSC) { |
| 8675 | Enc += "f{"; |
| 8676 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 8677 | return false; |
| 8678 | Enc += "}("; |
| 8679 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 8680 | // N.B. we are only interested in the adjusted param types. |
| 8681 | auto I = FPT->param_type_begin(); |
| 8682 | auto E = FPT->param_type_end(); |
| 8683 | if (I != E) { |
| 8684 | do { |
| 8685 | if (!appendType(Enc, *I, CGM, TSC)) |
| 8686 | return false; |
| 8687 | ++I; |
| 8688 | if (I != E) |
| 8689 | Enc += ','; |
| 8690 | } while (I != E); |
| 8691 | if (FPT->isVariadic()) |
| 8692 | Enc += ",va"; |
| 8693 | } else { |
| 8694 | if (FPT->isVariadic()) |
| 8695 | Enc += "va"; |
| 8696 | else |
| 8697 | Enc += '0'; |
| 8698 | } |
| 8699 | } |
| 8700 | Enc += ')'; |
| 8701 | return true; |
| 8702 | } |
| 8703 | |
| 8704 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 8705 | /// type encodings. |
| 8706 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8707 | const CodeGen::CodeGenModule &CGM, |
| 8708 | TypeStringCache &TSC) { |
| 8709 | |
| 8710 | QualType QT = QType.getCanonicalType(); |
| 8711 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8712 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 8713 | // The Qualifiers should be attached to the type rather than the array. |
| 8714 | // Thus we don't call appendQualifier() here. |
| 8715 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 8716 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8717 | appendQualifier(Enc, QT); |
| 8718 | |
| 8719 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 8720 | return appendBuiltinType(Enc, BT); |
| 8721 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8722 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 8723 | return appendPointerType(Enc, PT, CGM, TSC); |
| 8724 | |
| 8725 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 8726 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 8727 | |
| 8728 | if (const RecordType *RT = QT->getAsStructureType()) |
| 8729 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8730 | |
| 8731 | if (const RecordType *RT = QT->getAsUnionType()) |
| 8732 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 8733 | |
| 8734 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 8735 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 8736 | |
| 8737 | return false; |
| 8738 | } |
| 8739 | |
| 8740 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8741 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 8742 | if (!D) |
| 8743 | return false; |
| 8744 | |
| 8745 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8746 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 8747 | return false; |
| 8748 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 8749 | } |
| 8750 | |
| 8751 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8752 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 8753 | return false; |
| 8754 | QualType QT = VD->getType().getCanonicalType(); |
| 8755 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 8756 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8757 | // The Qualifiers should be attached to the type rather than the array. |
| 8758 | // Thus we don't call appendQualifier() here. |
| 8759 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8760 | } |
| 8761 | return appendType(Enc, QT, CGM, TSC); |
| 8762 | } |
| 8763 | return false; |
| 8764 | } |
| 8765 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 8766 | //===----------------------------------------------------------------------===// |
| 8767 | // RISCV ABI Implementation |
| 8768 | //===----------------------------------------------------------------------===// |
| 8769 | |
| 8770 | namespace { |
| 8771 | class RISCVABIInfo : public DefaultABIInfo { |
| 8772 | private: |
| 8773 | unsigned XLen; // Size of the integer ('x') registers in bits. |
| 8774 | static const int NumArgGPRs = 8; |
| 8775 | |
| 8776 | public: |
| 8777 | RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 8778 | : DefaultABIInfo(CGT), XLen(XLen) {} |
| 8779 | |
| 8780 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 8781 | // non-virtual, but computeInfo is virtual, so we overload it. |
| 8782 | void computeInfo(CGFunctionInfo &FI) const override; |
| 8783 | |
| 8784 | ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, |
| 8785 | int &ArgGPRsLeft) const; |
| 8786 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8787 | |
| 8788 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8789 | QualType Ty) const override; |
| 8790 | |
| 8791 | ABIArgInfo extendType(QualType Ty) const; |
| 8792 | }; |
| 8793 | } // end anonymous namespace |
| 8794 | |
| 8795 | void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8796 | QualType RetTy = FI.getReturnType(); |
| 8797 | if (!getCXXABI().classifyReturnType(FI)) |
| 8798 | FI.getReturnInfo() = classifyReturnType(RetTy); |
| 8799 | |
| 8800 | // IsRetIndirect is true if classifyArgumentType indicated the value should |
| 8801 | // be passed indirect or if the type size is greater than 2*xlen. e.g. fp128 |
| 8802 | // is passed direct in LLVM IR, relying on the backend lowering code to |
| 8803 | // rewrite the argument list and pass indirectly on RV32. |
| 8804 | bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect || |
| 8805 | getContext().getTypeSize(RetTy) > (2 * XLen); |
| 8806 | |
| 8807 | // We must track the number of GPRs used in order to conform to the RISC-V |
| 8808 | // ABI, as integer scalars passed in registers should have signext/zeroext |
| 8809 | // when promoted, but are anyext if passed on the stack. As GPR usage is |
| 8810 | // different for variadic arguments, we must also track whether we are |
| 8811 | // examining a vararg or not. |
| 8812 | int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs; |
| 8813 | int NumFixedArgs = FI.getNumRequiredArgs(); |
| 8814 | |
| 8815 | int ArgNum = 0; |
| 8816 | for (auto &ArgInfo : FI.arguments()) { |
| 8817 | bool IsFixed = ArgNum < NumFixedArgs; |
| 8818 | ArgInfo.info = classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft); |
| 8819 | ArgNum++; |
| 8820 | } |
| 8821 | } |
| 8822 | |
| 8823 | ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed, |
| 8824 | int &ArgGPRsLeft) const { |
| 8825 | assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow"); |
| 8826 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 8827 | |
| 8828 | // Structures with either a non-trivial destructor or a non-trivial |
| 8829 | // copy constructor are always passed indirectly. |
| 8830 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 8831 | if (ArgGPRsLeft) |
| 8832 | ArgGPRsLeft -= 1; |
| 8833 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 8834 | CGCXXABI::RAA_DirectInMemory); |
| 8835 | } |
| 8836 | |
| 8837 | // Ignore empty structs/unions. |
| 8838 | if (isEmptyRecord(getContext(), Ty, true)) |
| 8839 | return ABIArgInfo::getIgnore(); |
| 8840 | |
| 8841 | uint64_t Size = getContext().getTypeSize(Ty); |
| 8842 | uint64_t NeededAlign = getContext().getTypeAlign(Ty); |
| 8843 | bool MustUseStack = false; |
| 8844 | // Determine the number of GPRs needed to pass the current argument |
| 8845 | // according to the ABI. 2*XLen-aligned varargs are passed in "aligned" |
| 8846 | // register pairs, so may consume 3 registers. |
| 8847 | int NeededArgGPRs = 1; |
| 8848 | if (!IsFixed && NeededAlign == 2 * XLen) |
| 8849 | NeededArgGPRs = 2 + (ArgGPRsLeft % 2); |
| 8850 | else if (Size > XLen && Size <= 2 * XLen) |
| 8851 | NeededArgGPRs = 2; |
| 8852 | |
| 8853 | if (NeededArgGPRs > ArgGPRsLeft) { |
| 8854 | MustUseStack = true; |
| 8855 | NeededArgGPRs = ArgGPRsLeft; |
| 8856 | } |
| 8857 | |
| 8858 | ArgGPRsLeft -= NeededArgGPRs; |
| 8859 | |
| 8860 | if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) { |
| 8861 | // Treat an enum type as its underlying type. |
| 8862 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8863 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8864 | |
| 8865 | // All integral types are promoted to XLen width, unless passed on the |
| 8866 | // stack. |
| 8867 | if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) { |
| 8868 | return extendType(Ty); |
| 8869 | } |
| 8870 | |
| 8871 | return ABIArgInfo::getDirect(); |
| 8872 | } |
| 8873 | |
| 8874 | // Aggregates which are <= 2*XLen will be passed in registers if possible, |
| 8875 | // so coerce to integers. |
| 8876 | if (Size <= 2 * XLen) { |
| 8877 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 8878 | |
| 8879 | // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is |
| 8880 | // required, and a 2-element XLen array if only XLen alignment is required. |
| 8881 | if (Size <= XLen) { |
| 8882 | return ABIArgInfo::getDirect( |
| 8883 | llvm::IntegerType::get(getVMContext(), XLen)); |
| 8884 | } else if (Alignment == 2 * XLen) { |
| 8885 | return ABIArgInfo::getDirect( |
| 8886 | llvm::IntegerType::get(getVMContext(), 2 * XLen)); |
| 8887 | } else { |
| 8888 | return ABIArgInfo::getDirect(llvm::ArrayType::get( |
| 8889 | llvm::IntegerType::get(getVMContext(), XLen), 2)); |
| 8890 | } |
| 8891 | } |
| 8892 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 8893 | } |
| 8894 | |
| 8895 | ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const { |
| 8896 | if (RetTy->isVoidType()) |
| 8897 | return ABIArgInfo::getIgnore(); |
| 8898 | |
| 8899 | int ArgGPRsLeft = 2; |
| 8900 | |
| 8901 | // The rules for return and argument types are the same, so defer to |
| 8902 | // classifyArgumentType. |
| 8903 | return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft); |
| 8904 | } |
| 8905 | |
| 8906 | Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8907 | QualType Ty) const { |
| 8908 | CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8); |
| 8909 | |
| 8910 | // Empty records are ignored for parameter passing purposes. |
| 8911 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 8912 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 8913 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 8914 | return Addr; |
| 8915 | } |
| 8916 | |
| 8917 | std::pair<CharUnits, CharUnits> SizeAndAlign = |
| 8918 | getContext().getTypeInfoInChars(Ty); |
| 8919 | |
| 8920 | // Arguments bigger than 2*Xlen bytes are passed indirectly. |
| 8921 | bool IsIndirect = SizeAndAlign.first > 2 * SlotSize; |
| 8922 | |
| 8923 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign, |
| 8924 | SlotSize, /*AllowHigherAlign=*/true); |
| 8925 | } |
| 8926 | |
| 8927 | ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const { |
| 8928 | int TySize = getContext().getTypeSize(Ty); |
| 8929 | // RV64 ABI requires unsigned 32 bit integers to be sign extended. |
| 8930 | if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 8931 | return ABIArgInfo::getSignExtend(Ty); |
| 8932 | return ABIArgInfo::getExtend(Ty); |
| 8933 | } |
| 8934 | |
| 8935 | namespace { |
| 8936 | class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8937 | public: |
| 8938 | RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 8939 | : TargetCodeGenInfo(new RISCVABIInfo(CGT, XLen)) {} |
| 8940 | }; |
| 8941 | } // namespace |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8942 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8943 | //===----------------------------------------------------------------------===// |
| 8944 | // Driver code |
| 8945 | //===----------------------------------------------------------------------===// |
| 8946 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8947 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 8948 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 8949 | } |
| 8950 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 8951 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8952 | if (TheTargetCodeGenInfo) |
| 8953 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8954 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8955 | // Helper to set the unique_ptr while still keeping the return value. |
| 8956 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 8957 | this->TheTargetCodeGenInfo.reset(P); |
| 8958 | return *P; |
| 8959 | }; |
| 8960 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 8961 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 8962 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8963 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8964 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8965 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 8966 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8967 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 8968 | case llvm::Triple::mips: |
| 8969 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 8970 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8971 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 8972 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8973 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 8974 | case llvm::Triple::mips64: |
| 8975 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8976 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 8977 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 8978 | case llvm::Triple::avr: |
| 8979 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 8980 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 8981 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 8982 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8983 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 8984 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 8985 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 8986 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 8987 | return SetCGInfo( |
| 8988 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8989 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8990 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8991 | } |
| 8992 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8993 | case llvm::Triple::wasm32: |
| 8994 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 8995 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8996 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8997 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 8998 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8999 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9000 | case llvm::Triple::thumbeb: { |
| 9001 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 9002 | return SetCGInfo( |
| 9003 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 9004 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9005 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9006 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 9007 | StringRef ABIStr = getTarget().getABI(); |
| 9008 | if (ABIStr == "apcs-gnu") |
| 9009 | Kind = ARMABIInfo::APCS; |
| 9010 | else if (ABIStr == "aapcs16") |
| 9011 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 9012 | else if (CodeGenOpts.FloatABI == "hard" || |
| 9013 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9014 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 9015 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9016 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9017 | Kind = ARMABIInfo::AAPCS_VFP; |
| 9018 | |
| 9019 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 9020 | } |
| 9021 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9022 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9023 | return SetCGInfo( |
| 9024 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 9025 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9026 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9027 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9028 | if (getTarget().getABI() == "elfv2") |
| 9029 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9030 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9031 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9032 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9033 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9034 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9035 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9036 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9037 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 9038 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9039 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9040 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9041 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9042 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9043 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9044 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9045 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9046 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9047 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9048 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 9049 | case llvm::Triple::nvptx: |
| 9050 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9051 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 9052 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9053 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9054 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9055 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9056 | case llvm::Triple::riscv32: |
| 9057 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 32)); |
| 9058 | case llvm::Triple::riscv64: |
| 9059 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 64)); |
| 9060 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9061 | case llvm::Triple::systemz: { |
| 9062 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9063 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9064 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 9065 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9066 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 9067 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9068 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9069 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9070 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9071 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 9072 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9073 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 9074 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 9075 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9076 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9077 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 9078 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9079 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9080 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9081 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 9082 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9083 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 9084 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9085 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9086 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9087 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9088 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9089 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9090 | X86AVXABILevel AVXLevel = |
| 9091 | (ABI == "avx512" |
| 9092 | ? X86AVXABILevel::AVX512 |
| 9093 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9094 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9095 | switch (Triple.getOS()) { |
| 9096 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9097 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 9098 | case llvm::Triple::PS4: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9099 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9100 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9101 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9102 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9103 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 9104 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9105 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 9106 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9107 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 9108 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9109 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 9110 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9111 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 9112 | case llvm::Triple::sparc: |
| 9113 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 9114 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9115 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9116 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9117 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 9118 | case llvm::Triple::spir: |
| 9119 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9120 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9121 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9122 | } |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9123 | |
| 9124 | /// Create an OpenCL kernel for an enqueued block. |
| 9125 | /// |
| 9126 | /// The kernel has the same function type as the block invoke function. Its |
| 9127 | /// name is the name of the block invoke function postfixed with "_kernel". |
| 9128 | /// It simply calls the block invoke function then returns. |
| 9129 | llvm::Function * |
| 9130 | TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 9131 | llvm::Function *Invoke, |
| 9132 | llvm::Value *BlockLiteral) const { |
| 9133 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9134 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9135 | for (auto &P : InvokeFT->params()) |
| 9136 | ArgTys.push_back(P); |
| 9137 | auto &C = CGF.getLLVMContext(); |
| 9138 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9139 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9140 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9141 | &CGF.CGM.getModule()); |
| 9142 | auto IP = CGF.Builder.saveIP(); |
| 9143 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9144 | auto &Builder = CGF.Builder; |
| 9145 | Builder.SetInsertPoint(BB); |
| 9146 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9147 | for (auto &A : F->args()) |
| 9148 | Args.push_back(&A); |
| 9149 | Builder.CreateCall(Invoke, Args); |
| 9150 | Builder.CreateRetVoid(); |
| 9151 | Builder.restoreIP(IP); |
| 9152 | return F; |
| 9153 | } |
| 9154 | |
| 9155 | /// Create an OpenCL kernel for an enqueued block. |
| 9156 | /// |
| 9157 | /// The type of the first argument (the block literal) is the struct type |
| 9158 | /// of the block literal instead of a pointer type. The first argument |
| 9159 | /// (block literal) is passed directly by value to the kernel. The kernel |
| 9160 | /// allocates the same type of struct on stack and stores the block literal |
| 9161 | /// to it and passes its pointer to the block invoke function. The kernel |
| 9162 | /// has "enqueued-block" function attribute and kernel argument metadata. |
| 9163 | llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel( |
| 9164 | CodeGenFunction &CGF, llvm::Function *Invoke, |
| 9165 | llvm::Value *BlockLiteral) const { |
| 9166 | auto &Builder = CGF.Builder; |
| 9167 | auto &C = CGF.getLLVMContext(); |
| 9168 | |
| 9169 | auto *BlockTy = BlockLiteral->getType()->getPointerElementType(); |
| 9170 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9171 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9172 | llvm::SmallVector<llvm::Metadata *, 8> AddressQuals; |
| 9173 | llvm::SmallVector<llvm::Metadata *, 8> AccessQuals; |
| 9174 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames; |
| 9175 | llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames; |
| 9176 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals; |
| 9177 | llvm::SmallVector<llvm::Metadata *, 8> ArgNames; |
| 9178 | |
| 9179 | ArgTys.push_back(BlockTy); |
| 9180 | ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9181 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0))); |
| 9182 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9183 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9184 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9185 | ArgNames.push_back(llvm::MDString::get(C, "block_literal")); |
| 9186 | for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) { |
| 9187 | ArgTys.push_back(InvokeFT->getParamType(I)); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9188 | ArgTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9189 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3))); |
| 9190 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9191 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9192 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9193 | ArgNames.push_back( |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 9194 | llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str())); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9195 | } |
| 9196 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9197 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9198 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9199 | &CGF.CGM.getModule()); |
| 9200 | F->addFnAttr("enqueued-block"); |
| 9201 | auto IP = CGF.Builder.saveIP(); |
| 9202 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9203 | Builder.SetInsertPoint(BB); |
| 9204 | unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy); |
| 9205 | auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr); |
| 9206 | BlockPtr->setAlignment(BlockAlign); |
| 9207 | Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign); |
| 9208 | auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0)); |
| 9209 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9210 | Args.push_back(Cast); |
| 9211 | for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I) |
| 9212 | Args.push_back(I); |
| 9213 | Builder.CreateCall(Invoke, Args); |
| 9214 | Builder.CreateRetVoid(); |
| 9215 | Builder.restoreIP(IP); |
| 9216 | |
| 9217 | F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals)); |
| 9218 | F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals)); |
| 9219 | F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames)); |
| 9220 | F->setMetadata("kernel_arg_base_type", |
| 9221 | llvm::MDNode::get(C, ArgBaseTypeNames)); |
| 9222 | F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals)); |
| 9223 | if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata) |
| 9224 | F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames)); |
| 9225 | |
| 9226 | return F; |
| 9227 | } |