Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1 | //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// |
Anton Korobeynikov | c4a59eb | 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 | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetInfo.h" |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 16 | #include "ABIInfo.h" |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 18 | #include "CGValue.h" |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Mark Lacey | 8b54999 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 21 | #include "clang/CodeGen/CGFunctionInfo.h" |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 22 | #include "clang/CodeGen/SwiftCallingConv.h" |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CodeGenOptions.h" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DataLayout.h" |
| 27 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 29 | #include <algorithm> // std::sort |
| 30 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 34 | // Helper for coercing an aggregate argument or return value into an integer |
| 35 | // array of the same size (including padding) and alignment. This alternate |
| 36 | // coercion happens only for the RenderScript ABI and can be removed after |
| 37 | // runtimes that rely on it are no longer supported. |
Matt Wala | 1d15151 | 2015-08-10 15:58:40 -0700 | [diff] [blame] | 38 | // |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 39 | // RenderScript assumes that the size of the argument / return value in the IR |
| 40 | // is the same as the size of the corresponding qualified type. This helper |
| 41 | // coerces the aggregate type into an array of the same size (including |
| 42 | // padding). This coercion is used in lieu of expansion of struct members or |
| 43 | // other canonical coercions that return a coerced-type of larger size. |
Matt Wala | 1d15151 | 2015-08-10 15:58:40 -0700 | [diff] [blame] | 44 | // |
| 45 | // Ty - The argument / return value type |
| 46 | // Context - The associated ASTContext |
| 47 | // LLVMContext - The associated LLVMContext |
| 48 | static ABIArgInfo coerceToIntArray(QualType Ty, |
| 49 | ASTContext &Context, |
| 50 | llvm::LLVMContext &LLVMContext) { |
| 51 | // Alignment and Size are measured in bits. |
| 52 | const uint64_t Size = Context.getTypeSize(Ty); |
| 53 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 54 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment); |
| 55 | const uint64_t NumElements = (Size + Alignment - 1) / Alignment; |
| 56 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 57 | } |
| 58 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 59 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 60 | llvm::Value *Array, |
| 61 | llvm::Value *Value, |
| 62 | unsigned FirstIndex, |
| 63 | unsigned LastIndex) { |
| 64 | // Alternatively, we could emit this as a loop in the source. |
| 65 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 66 | llvm::Value *Cell = |
| 67 | Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 68 | Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 72 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 73 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 74 | T->isMemberFunctionPointerType(); |
| 75 | } |
| 76 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 77 | ABIArgInfo |
| 78 | ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, |
| 79 | llvm::Type *Padding) const { |
| 80 | return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), |
| 81 | ByRef, Realign, Padding); |
| 82 | } |
| 83 | |
| 84 | ABIArgInfo |
| 85 | ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { |
| 86 | return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), |
| 87 | /*ByRef*/ false, Realign); |
| 88 | } |
| 89 | |
| 90 | Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 91 | QualType Ty) const { |
| 92 | return Address::invalid(); |
| 93 | } |
| 94 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 95 | ABIInfo::~ABIInfo() {} |
| 96 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 97 | /// Does the given lowering require more than the given number of |
| 98 | /// registers when expanded? |
| 99 | /// |
| 100 | /// This is intended to be the basis of a reasonable basic implementation |
| 101 | /// of should{Pass,Return}IndirectlyForSwift. |
| 102 | /// |
| 103 | /// For most targets, a limit of four total registers is reasonable; this |
| 104 | /// limits the amount of code required in order to move around the value |
| 105 | /// in case it wasn't produced immediately prior to the call by the caller |
| 106 | /// (or wasn't produced in exactly the right registers) or isn't used |
| 107 | /// immediately within the callee. But some targets may need to further |
| 108 | /// limit the register count due to an inability to support that many |
| 109 | /// return registers. |
| 110 | static bool occupiesMoreThan(CodeGenTypes &cgt, |
| 111 | ArrayRef<llvm::Type*> scalarTypes, |
| 112 | unsigned maxAllRegisters) { |
| 113 | unsigned intCount = 0, fpCount = 0; |
| 114 | for (llvm::Type *type : scalarTypes) { |
| 115 | if (type->isPointerTy()) { |
| 116 | intCount++; |
| 117 | } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) { |
| 118 | auto ptrWidth = cgt.getTarget().getPointerWidth(0); |
| 119 | intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth; |
| 120 | } else { |
| 121 | assert(type->isVectorTy() || type->isFloatingPointTy()); |
| 122 | fpCount++; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return (intCount + fpCount > maxAllRegisters); |
| 127 | } |
| 128 | |
| 129 | bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 130 | llvm::Type *eltTy, |
| 131 | unsigned numElts) const { |
| 132 | // The default implementation of this assumes that the target guarantees |
| 133 | // 128-bit SIMD support but nothing more. |
| 134 | return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16); |
| 135 | } |
| 136 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 137 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 138 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 139 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 140 | if (!RD) |
| 141 | return CGCXXABI::RAA_Default; |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 142 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 146 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 147 | const RecordType *RT = T->getAs<RecordType>(); |
| 148 | if (!RT) |
| 149 | return CGCXXABI::RAA_Default; |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 150 | return getRecordArgABI(RT, CXXABI); |
| 151 | } |
| 152 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 153 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 154 | /// should ensure that all elements of the union have the same "machine type". |
| 155 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 156 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 157 | const RecordDecl *UD = UT->getDecl(); |
| 158 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 159 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 160 | return UD->field_begin()->getType(); |
| 161 | } |
| 162 | } |
| 163 | return Ty; |
| 164 | } |
| 165 | |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 166 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 167 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 170 | ASTContext &ABIInfo::getContext() const { |
| 171 | return CGT.getContext(); |
| 172 | } |
| 173 | |
| 174 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 175 | return CGT.getLLVMContext(); |
| 176 | } |
| 177 | |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 178 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 179 | return CGT.getDataLayout(); |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 180 | } |
| 181 | |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 182 | const TargetInfo &ABIInfo::getTarget() const { |
| 183 | return CGT.getTarget(); |
| 184 | } |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 185 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 186 | bool ABIInfo:: isAndroid() const { return getTarget().getTriple().isAndroid(); } |
| 187 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 188 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 193 | uint64_t Members) const { |
| 194 | return false; |
| 195 | } |
| 196 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 197 | bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 198 | return false; |
| 199 | } |
| 200 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 201 | LLVM_DUMP_METHOD void ABIArgInfo::dump() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 202 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 203 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 204 | switch (TheKind) { |
| 205 | case Direct: |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 206 | OS << "Direct Type="; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 207 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 208 | Ty->print(OS); |
| 209 | else |
| 210 | OS << "null"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 211 | break; |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 212 | case Extend: |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 213 | OS << "Extend"; |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 214 | break; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 215 | case Ignore: |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 216 | OS << "Ignore"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 217 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 218 | case InAlloca: |
| 219 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 220 | break; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 221 | case Indirect: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 222 | OS << "Indirect Align=" << getIndirectAlign().getQuantity() |
Joerg Sonnenberger | e9b5d77 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 223 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 224 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 225 | break; |
| 226 | case Expand: |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 227 | OS << "Expand"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 228 | break; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 229 | case CoerceAndExpand: |
| 230 | OS << "CoerceAndExpand Type="; |
| 231 | getCoerceAndExpandType()->print(OS); |
| 232 | break; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 233 | } |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 234 | OS << ")\n"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 237 | // Dynamically round a pointer up to a multiple of the given alignment. |
| 238 | static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, |
| 239 | llvm::Value *Ptr, |
| 240 | CharUnits Align) { |
| 241 | llvm::Value *PtrAsInt = Ptr; |
| 242 | // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; |
| 243 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 244 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 245 | llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); |
| 246 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 247 | llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); |
| 248 | PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, |
| 249 | Ptr->getType(), |
| 250 | Ptr->getName() + ".aligned"); |
| 251 | return PtrAsInt; |
| 252 | } |
| 253 | |
| 254 | /// Emit va_arg for a platform using the common void* representation, |
| 255 | /// where arguments are simply emitted in an array of slots on the stack. |
| 256 | /// |
| 257 | /// This version implements the core direct-value passing rules. |
| 258 | /// |
| 259 | /// \param SlotSize - The size and alignment of a stack slot. |
| 260 | /// Each argument will be allocated to a multiple of this number of |
| 261 | /// slots, and all the slots will be aligned to this value. |
| 262 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 263 | /// an argument type with an alignment greater than the slot size |
| 264 | /// will be emitted on a higher-alignment address, potentially |
| 265 | /// leaving one or more empty slots behind as padding. If this |
| 266 | /// is false, the returned address might be less-aligned than |
| 267 | /// DirectAlign. |
| 268 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 269 | Address VAListAddr, |
| 270 | llvm::Type *DirectTy, |
| 271 | CharUnits DirectSize, |
| 272 | CharUnits DirectAlign, |
| 273 | CharUnits SlotSize, |
| 274 | bool AllowHigherAlign) { |
| 275 | // Cast the element type to i8* if necessary. Some platforms define |
| 276 | // va_list as a struct containing an i8* instead of just an i8*. |
| 277 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 278 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 279 | |
| 280 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 281 | |
| 282 | // If the CC aligns values higher than the slot size, do so if needed. |
| 283 | Address Addr = Address::invalid(); |
| 284 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
| 285 | Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), |
| 286 | DirectAlign); |
| 287 | } else { |
| 288 | Addr = Address(Ptr, SlotSize); |
| 289 | } |
| 290 | |
| 291 | // Advance the pointer past the argument, then store that back. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 292 | CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 293 | llvm::Value *NextPtr = |
| 294 | CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, |
| 295 | "argp.next"); |
| 296 | CGF.Builder.CreateStore(NextPtr, VAListAddr); |
| 297 | |
| 298 | // If the argument is smaller than a slot, and this is a big-endian |
| 299 | // target, the argument will be right-adjusted in its slot. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 300 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() && |
| 301 | !DirectTy->isStructTy()) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 302 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 303 | } |
| 304 | |
| 305 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 306 | return Addr; |
| 307 | } |
| 308 | |
| 309 | /// Emit va_arg for a platform using the common void* representation, |
| 310 | /// where arguments are simply emitted in an array of slots on the stack. |
| 311 | /// |
| 312 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 313 | /// \param ValueInfo - The size and alignment of this type, generally |
| 314 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 315 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 316 | /// Each argument will be allocated to a multiple of this number of |
| 317 | /// slots, and all the slots will be aligned to this value. |
| 318 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 319 | /// an argument type with an alignment greater than the slot size |
| 320 | /// will be emitted on a higher-alignment address, potentially |
| 321 | /// leaving one or more empty slots behind as padding. |
| 322 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 323 | QualType ValueTy, bool IsIndirect, |
| 324 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 325 | CharUnits SlotSizeAndAlign, |
| 326 | bool AllowHigherAlign) { |
| 327 | // The size and alignment of the value that was passed directly. |
| 328 | CharUnits DirectSize, DirectAlign; |
| 329 | if (IsIndirect) { |
| 330 | DirectSize = CGF.getPointerSize(); |
| 331 | DirectAlign = CGF.getPointerAlign(); |
| 332 | } else { |
| 333 | DirectSize = ValueInfo.first; |
| 334 | DirectAlign = ValueInfo.second; |
| 335 | } |
| 336 | |
| 337 | // Cast the address we've calculated to the right type. |
| 338 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 339 | if (IsIndirect) |
| 340 | DirectTy = DirectTy->getPointerTo(0); |
| 341 | |
| 342 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 343 | DirectSize, DirectAlign, |
| 344 | SlotSizeAndAlign, |
| 345 | AllowHigherAlign); |
| 346 | |
| 347 | if (IsIndirect) { |
| 348 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 349 | } |
| 350 | |
| 351 | return Addr; |
| 352 | |
| 353 | } |
| 354 | |
| 355 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 356 | Address Addr1, llvm::BasicBlock *Block1, |
| 357 | Address Addr2, llvm::BasicBlock *Block2, |
| 358 | const llvm::Twine &Name = "") { |
| 359 | assert(Addr1.getType() == Addr2.getType()); |
| 360 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 361 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 362 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 363 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 364 | return Address(PHI, Align); |
| 365 | } |
| 366 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 367 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 368 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 369 | // If someone can figure out a general rule for this, that would be great. |
| 370 | // It's probably just doomed to be platform-dependent, though. |
| 371 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 372 | // Verified for: |
| 373 | // x86-64 FreeBSD, Linux, Darwin |
| 374 | // x86-32 FreeBSD, Linux, Darwin |
| 375 | // PowerPC Linux, Darwin |
| 376 | // ARM Darwin (*not* EABI) |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 377 | // AArch64 Linux |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 378 | return 32; |
| 379 | } |
| 380 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 381 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 382 | const FunctionNoProtoType *fnType) const { |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 383 | // The following conventions are known to require this to be false: |
| 384 | // x86_stdcall |
| 385 | // MIPS |
| 386 | // For everything else, we just prefer false unless we opt out. |
| 387 | return false; |
| 388 | } |
| 389 | |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 390 | void |
| 391 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 392 | llvm::SmallString<24> &Opt) const { |
| 393 | // This assumes the user is passing a library name like "rt" instead of a |
| 394 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 395 | // dynamic. |
| 396 | Opt = "-l"; |
| 397 | Opt += Lib; |
| 398 | } |
| 399 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 400 | unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 401 | return llvm::CallingConv::C; |
| 402 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 403 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 404 | |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 405 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 406 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 407 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 408 | bool AllowArrays) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 409 | if (FD->isUnnamedBitfield()) |
| 410 | return true; |
| 411 | |
| 412 | QualType FT = FD->getType(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 413 | |
Eli Friedman | 7e7ad3f | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 414 | // Constant arrays of empty records count as empty, strip them off. |
| 415 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 416 | if (AllowArrays) |
Eli Friedman | 7e7ad3f | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 417 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 418 | if (AT->getSize() == 0) |
| 419 | return true; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 420 | FT = AT->getElementType(); |
Eli Friedman | 7e7ad3f | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 421 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 422 | |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 423 | const RecordType *RT = FT->getAs<RecordType>(); |
| 424 | if (!RT) |
| 425 | return false; |
| 426 | |
| 427 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 428 | // |
| 429 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 430 | // current ABI. |
| 431 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 432 | return false; |
| 433 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 434 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 437 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 438 | /// fields. Note that a structure with a flexible array member is not |
| 439 | /// considered empty. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 440 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 441 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 442 | if (!RT) |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 443 | return false; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 444 | const RecordDecl *RD = RT->getDecl(); |
| 445 | if (RD->hasFlexibleArrayMember()) |
| 446 | return false; |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 447 | |
Argyrios Kyrtzidis | c5f18f3 | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 448 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 449 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 450 | for (const auto &I : CXXRD->bases()) |
| 451 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | c5f18f3 | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 452 | return false; |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 453 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 454 | for (const auto *I : RD->fields()) |
| 455 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 456 | return false; |
| 457 | return true; |
| 458 | } |
| 459 | |
| 460 | /// isSingleElementStruct - Determine if a structure is a "single |
| 461 | /// element struct", i.e. it has exactly one non-empty field or |
| 462 | /// exactly one field which is itself a single element |
| 463 | /// struct. Structures with flexible array members are never |
| 464 | /// considered single element structs. |
| 465 | /// |
| 466 | /// \return The field declaration for the single non-empty field, if |
| 467 | /// it exists. |
| 468 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 469 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 470 | if (!RT) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 471 | return nullptr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 472 | |
| 473 | const RecordDecl *RD = RT->getDecl(); |
| 474 | if (RD->hasFlexibleArrayMember()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 475 | return nullptr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 476 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 477 | const Type *Found = nullptr; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 478 | |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 479 | // If this is a C++ record, check the bases first. |
| 480 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 481 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 482 | // Ignore empty records. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 483 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 484 | continue; |
| 485 | |
| 486 | // If we already found an element then this isn't a single-element struct. |
| 487 | if (Found) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 488 | return nullptr; |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 489 | |
| 490 | // If this is non-empty and not a single element struct, the composite |
| 491 | // cannot be a single element struct. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 492 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 493 | if (!Found) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 494 | return nullptr; |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | // Check for single element. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 499 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 500 | QualType FT = FD->getType(); |
| 501 | |
| 502 | // Ignore empty fields. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 503 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 504 | continue; |
| 505 | |
| 506 | // If we already found an element then this isn't a single-element |
| 507 | // struct. |
| 508 | if (Found) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 509 | return nullptr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 510 | |
| 511 | // Treat single element arrays as the element. |
| 512 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 513 | if (AT->getSize().getZExtValue() != 1) |
| 514 | break; |
| 515 | FT = AT->getElementType(); |
| 516 | } |
| 517 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 518 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 519 | Found = FT.getTypePtr(); |
| 520 | } else { |
| 521 | Found = isSingleElementStruct(FT, Context); |
| 522 | if (!Found) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 523 | return nullptr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 527 | // We don't consider a struct a single-element struct if it has |
| 528 | // padding beyond the element type. |
| 529 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 530 | return nullptr; |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 531 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 532 | return Found; |
| 533 | } |
| 534 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 535 | namespace { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 536 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 537 | const ABIArgInfo &AI) { |
| 538 | // This default implementation defers to the llvm backend's va_arg |
| 539 | // instruction. It can handle only passing arguments directly |
| 540 | // (typically only handled in the backend for primitive types), or |
| 541 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 542 | // flag has ABI impact in the callee, this implementation cannot |
| 543 | // work.) |
| 544 | |
| 545 | // Only a few cases are covered here at the moment -- those needed |
| 546 | // by the default abi. |
| 547 | llvm::Value *Val; |
| 548 | |
| 549 | if (AI.isIndirect()) { |
| 550 | assert(!AI.getPaddingType() && |
| 551 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
| 552 | assert( |
| 553 | !AI.getIndirectRealign() && |
| 554 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
| 555 | |
| 556 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 557 | CharUnits TyAlignForABI = TyInfo.second; |
| 558 | |
| 559 | llvm::Type *BaseTy = |
| 560 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 561 | llvm::Value *Addr = |
| 562 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 563 | return Address(Addr, TyAlignForABI); |
| 564 | } else { |
| 565 | assert((AI.isDirect() || AI.isExtend()) && |
| 566 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 567 | |
| 568 | assert(!AI.getInReg() && |
| 569 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
| 570 | assert(!AI.getPaddingType() && |
| 571 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
| 572 | assert(!AI.getDirectOffset() && |
| 573 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
| 574 | assert(!AI.getCoerceToType() && |
| 575 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
| 576 | |
| 577 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 578 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 579 | CGF.Builder.CreateStore(Val, Temp); |
| 580 | return Temp; |
| 581 | } |
| 582 | } |
| 583 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 584 | /// DefaultABIInfo - The default implementation for ABI specific |
| 585 | /// details. This implementation provides information which results in |
| 586 | /// self-consistent and sensible LLVM IR generation, but does not |
| 587 | /// conform to any particular ABI. |
| 588 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 589 | public: |
| 590 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 591 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 592 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 593 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 594 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 595 | void computeInfo(CGFunctionInfo &FI) const override { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 596 | if (!getCXXABI().classifyReturnType(FI)) |
| 597 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 598 | for (auto &I : FI.arguments()) |
| 599 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 602 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 603 | QualType Ty) const override { |
| 604 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 605 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 606 | }; |
| 607 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 608 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 609 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 610 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 611 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 612 | }; |
| 613 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 614 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 615 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 616 | |
| 617 | if (isAggregateTypeForABI(Ty)) { |
| 618 | // Records with non-trivial destructors/copy-constructors should not be |
| 619 | // passed by value. |
| 620 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 621 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 622 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 623 | return getNaturalAlignIndirect(Ty); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 624 | } |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 625 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 626 | // Treat an enum type as its underlying type. |
| 627 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 628 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 629 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 630 | return (Ty->isPromotableIntegerType() ? |
| 631 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Bob Wilson | 0024f94 | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 634 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 635 | if (RetTy->isVoidType()) |
| 636 | return ABIArgInfo::getIgnore(); |
| 637 | |
| 638 | if (isAggregateTypeForABI(RetTy)) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 639 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | 0024f94 | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 640 | |
| 641 | // Treat an enum type as its underlying type. |
| 642 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 643 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 644 | |
| 645 | return (RetTy->isPromotableIntegerType() ? |
| 646 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 647 | } |
| 648 | |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 649 | //===----------------------------------------------------------------------===// |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 650 | // WebAssembly ABI Implementation |
| 651 | // |
| 652 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 653 | //===----------------------------------------------------------------------===// |
| 654 | |
| 655 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 656 | public: |
| 657 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 658 | : DefaultABIInfo(CGT) {} |
| 659 | |
| 660 | private: |
| 661 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 662 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 663 | |
| 664 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 665 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
| 666 | // overload them. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 667 | void computeInfo(CGFunctionInfo &FI) const override { |
| 668 | if (!getCXXABI().classifyReturnType(FI)) |
| 669 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 670 | for (auto &Arg : FI.arguments()) |
| 671 | Arg.info = classifyArgumentType(Arg.type); |
| 672 | } |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 673 | |
| 674 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 675 | QualType Ty) const override; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 676 | }; |
| 677 | |
| 678 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 679 | public: |
| 680 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 681 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 682 | }; |
| 683 | |
| 684 | /// \brief Classify argument of given type \p Ty. |
| 685 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 686 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 687 | |
| 688 | if (isAggregateTypeForABI(Ty)) { |
| 689 | // Records with non-trivial destructors/copy-constructors should not be |
| 690 | // passed by value. |
| 691 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| 692 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 693 | // Ignore empty structs/unions. |
| 694 | if (isEmptyRecord(getContext(), Ty, true)) |
| 695 | return ABIArgInfo::getIgnore(); |
| 696 | // Lower single-element structs to just pass a regular value. TODO: We |
| 697 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 698 | // though watch out for things like bitfields. |
| 699 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 700 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 701 | } |
| 702 | |
| 703 | // Otherwise just do the default thing. |
| 704 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 705 | } |
| 706 | |
| 707 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 708 | if (isAggregateTypeForABI(RetTy)) { |
| 709 | // Records with non-trivial destructors/copy-constructors should not be |
| 710 | // returned by value. |
| 711 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 712 | // Ignore empty structs/unions. |
| 713 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 714 | return ABIArgInfo::getIgnore(); |
| 715 | // Lower single-element structs to just return a regular value. TODO: We |
| 716 | // could do reasonable-size multiple-element structs too, using |
| 717 | // ABIArgInfo::getDirect(). |
| 718 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 719 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | // Otherwise just do the default thing. |
| 724 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 725 | } |
| 726 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 727 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 728 | QualType Ty) const { |
| 729 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 730 | getContext().getTypeInfoInChars(Ty), |
| 731 | CharUnits::fromQuantity(4), |
| 732 | /*AllowHigherAlign=*/ true); |
| 733 | } |
| 734 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 735 | //===----------------------------------------------------------------------===// |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 736 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 737 | // |
| 738 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 739 | // are always passed on the stack. |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 740 | //===----------------------------------------------------------------------===// |
| 741 | |
| 742 | class PNaClABIInfo : public ABIInfo { |
| 743 | public: |
| 744 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 745 | |
| 746 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 747 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 748 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 749 | void computeInfo(CGFunctionInfo &FI) const override; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 750 | Address EmitVAArg(CodeGenFunction &CGF, |
| 751 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 752 | }; |
| 753 | |
| 754 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 755 | public: |
| 756 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 757 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 758 | }; |
| 759 | |
| 760 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 761 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 762 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 763 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 764 | for (auto &I : FI.arguments()) |
| 765 | I.info = classifyArgumentType(I.type); |
| 766 | } |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 767 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 768 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 769 | QualType Ty) const { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 770 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 771 | // function classification. Structs get passed directly for varargs |
| 772 | // functions, through a rewriting transform in |
| 773 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 774 | // this target to actually support a va_arg instructions with an |
| 775 | // aggregate type, unlike other targets. |
| 776 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 779 | /// \brief Classify argument of given type \p Ty. |
| 780 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 781 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 782 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 783 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 784 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 785 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 786 | // Treat an enum type as its underlying type. |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 787 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 788 | } else if (Ty->isFloatingType()) { |
| 789 | // Floating-point types don't go inreg. |
| 790 | return ABIArgInfo::getDirect(); |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 791 | } |
Eli Bendersky | c0783dc | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 792 | |
| 793 | return (Ty->isPromotableIntegerType() ? |
| 794 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 798 | if (RetTy->isVoidType()) |
| 799 | return ABIArgInfo::getIgnore(); |
| 800 | |
Eli Bendersky | e45dfd1 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 801 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 802 | if (isAggregateTypeForABI(RetTy)) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 803 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 804 | |
| 805 | // Treat an enum type as its underlying type. |
| 806 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 807 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 808 | |
| 809 | return (RetTy->isPromotableIntegerType() ? |
| 810 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 811 | } |
| 812 | |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 813 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 814 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 815 | // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 816 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 817 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 818 | IRType->getScalarSizeInBits() != 64; |
| 819 | } |
| 820 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 821 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 822 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 823 | llvm::Type* Ty) { |
Tim Northover | 1bea653 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 824 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 825 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 826 | // Invalid MMX constraint |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 827 | return nullptr; |
Tim Northover | 1bea653 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 830 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 1bea653 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | // No operation needed |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 834 | return Ty; |
| 835 | } |
| 836 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 837 | /// Returns true if this type can be passed in SSE registers with the |
| 838 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 839 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 840 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 841 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 842 | return true; |
| 843 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 844 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 845 | // registers specially. |
| 846 | unsigned VecSize = Context.getTypeSize(VT); |
| 847 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 848 | return true; |
| 849 | } |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 854 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 855 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 856 | return NumMembers <= 4; |
| 857 | } |
| 858 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 859 | //===----------------------------------------------------------------------===// |
| 860 | // X86-32 ABI Implementation |
| 861 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 862 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 863 | /// \brief Similar to llvm::CCState, but for Clang. |
| 864 | struct CCState { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 865 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 866 | |
| 867 | unsigned CC; |
| 868 | unsigned FreeRegs; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 869 | unsigned FreeSSERegs; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 870 | }; |
| 871 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 872 | /// X86_32ABIInfo - The X86-32 ABI information. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 873 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 874 | enum Class { |
| 875 | Integer, |
| 876 | Float |
| 877 | }; |
| 878 | |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 879 | static const unsigned MinABIStackAlignInBytes = 4; |
| 880 | |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 881 | bool IsDarwinVectorABI; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 882 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 883 | bool IsWin32StructABI; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 884 | bool IsSoftFloatABI; |
| 885 | bool IsMCUABI; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 886 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 887 | |
| 888 | static bool isRegisterSize(unsigned Size) { |
| 889 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 890 | } |
| 891 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 892 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 893 | // FIXME: Assumes vectorcall is in use. |
| 894 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 895 | } |
| 896 | |
| 897 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 898 | uint64_t NumMembers) const override { |
| 899 | // FIXME: Assumes vectorcall is in use. |
| 900 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 901 | } |
| 902 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 903 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 904 | |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 905 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 906 | /// such that the argument will be passed in memory. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 907 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 908 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 909 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 910 | |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 911 | /// \brief Return the alignment to use for the given type on the stack. |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 912 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 913 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 914 | Class classify(QualType Ty) const; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 915 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 916 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 917 | /// \brief Updates the number of available free registers, returns |
| 918 | /// true if any registers were allocated. |
| 919 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 920 | |
| 921 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 922 | bool &NeedsPadding) const; |
| 923 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
| 924 | |
| 925 | bool canExpandIndirectArgument(QualType Ty) const; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 926 | |
| 927 | /// \brief Rewrite the function info so that all memory arguments use |
| 928 | /// inalloca. |
| 929 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 930 | |
| 931 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 932 | CharUnits &StackOffset, ABIArgInfo &Info, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 933 | QualType Type) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 934 | |
Rafael Espindola | b33a3c4 | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 935 | public: |
| 936 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 937 | void computeInfo(CGFunctionInfo &FI) const override; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 938 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 939 | QualType Ty) const override; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 940 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 941 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 942 | bool RetSmallStructInRegABI, bool Win32StructABI, |
| 943 | unsigned NumRegisterParameters, bool SoftFloatABI) |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 944 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 945 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 946 | IsWin32StructABI(Win32StructABI), |
| 947 | IsSoftFloatABI(SoftFloatABI), |
| 948 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
| 949 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 950 | |
| 951 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 952 | ArrayRef<llvm::Type*> scalars, |
| 953 | bool asReturnValue) const override { |
| 954 | // LLVM's x86-32 lowering currently only assigns up to three |
| 955 | // integer registers and three fp registers. Oddly, it'll use up to |
| 956 | // four vector registers for vectors, but those can overlap with the |
| 957 | // scalar registers. |
| 958 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
| 959 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 960 | }; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 961 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 962 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 963 | public: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 964 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 965 | bool RetSmallStructInRegABI, bool Win32StructABI, |
| 966 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 967 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 968 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 969 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 970 | |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 971 | static bool isStructReturnInRegABI( |
| 972 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 973 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 974 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 975 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 976 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 977 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 978 | // Darwin uses different dwarf register numbers for EH. |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 979 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 980 | return 4; |
| 981 | } |
| 982 | |
| 983 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 984 | llvm::Value *Address) const override; |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 985 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 986 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 987 | StringRef Constraint, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 988 | llvm::Type* Ty) const override { |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 989 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 990 | } |
| 991 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 992 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 993 | std::string &Constraints, |
| 994 | std::vector<llvm::Type *> &ResultRegTypes, |
| 995 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 996 | std::vector<LValue> &ResultRegDests, |
| 997 | std::string &AsmString, |
| 998 | unsigned NumOutputs) const override; |
| 999 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1000 | llvm::Constant * |
| 1001 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b914e87 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1002 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1003 | (0x06 << 8) | // .+0x08 |
| 1004 | ('F' << 16) | |
| 1005 | ('T' << 24); |
| 1006 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1007 | } |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1008 | |
| 1009 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1010 | return "movl\t%ebp, %ebp" |
| 1011 | "\t\t## marker for objc_retainAutoreleaseReturnValue"; |
| 1012 | } |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1013 | }; |
| 1014 | |
| 1015 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1016 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1017 | /// Rewrite input constraint references after adding some output constraints. |
| 1018 | /// In the case where there is one output and one input and we add one output, |
| 1019 | /// we need to replace all operand references greater than or equal to 1: |
| 1020 | /// mov $0, $1 |
| 1021 | /// mov eax, $1 |
| 1022 | /// The result will be: |
| 1023 | /// mov $0, $2 |
| 1024 | /// mov eax, $2 |
| 1025 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1026 | unsigned NumNewOuts, |
| 1027 | std::string &AsmString) { |
| 1028 | std::string Buf; |
| 1029 | llvm::raw_string_ostream OS(Buf); |
| 1030 | size_t Pos = 0; |
| 1031 | while (Pos < AsmString.size()) { |
| 1032 | size_t DollarStart = AsmString.find('$', Pos); |
| 1033 | if (DollarStart == std::string::npos) |
| 1034 | DollarStart = AsmString.size(); |
| 1035 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1036 | if (DollarEnd == std::string::npos) |
| 1037 | DollarEnd = AsmString.size(); |
| 1038 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1039 | Pos = DollarEnd; |
| 1040 | size_t NumDollars = DollarEnd - DollarStart; |
| 1041 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1042 | // We have an operand reference. |
| 1043 | size_t DigitStart = Pos; |
| 1044 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1045 | if (DigitEnd == std::string::npos) |
| 1046 | DigitEnd = AsmString.size(); |
| 1047 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1048 | unsigned OperandIndex; |
| 1049 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1050 | if (OperandIndex >= FirstIn) |
| 1051 | OperandIndex += NumNewOuts; |
| 1052 | OS << OperandIndex; |
| 1053 | } else { |
| 1054 | OS << OperandStr; |
| 1055 | } |
| 1056 | Pos = DigitEnd; |
| 1057 | } |
| 1058 | } |
| 1059 | AsmString = std::move(OS.str()); |
| 1060 | } |
| 1061 | |
| 1062 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1063 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1064 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1065 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1066 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1067 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1068 | unsigned NumOutputs) const { |
| 1069 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1070 | |
| 1071 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1072 | // larger. |
| 1073 | if (!Constraints.empty()) |
| 1074 | Constraints += ','; |
| 1075 | if (RetWidth <= 32) { |
| 1076 | Constraints += "={eax}"; |
| 1077 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1078 | } else { |
| 1079 | // Use the 'A' constraint for EAX:EDX. |
| 1080 | Constraints += "=A"; |
| 1081 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1082 | } |
| 1083 | |
| 1084 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1085 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1086 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1087 | |
| 1088 | // Coerce the integer by bitcasting the return slot pointer. |
| 1089 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1090 | CoerceTy->getPointerTo())); |
| 1091 | ResultRegDests.push_back(ReturnSlot); |
| 1092 | |
| 1093 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1094 | } |
| 1095 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1096 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1097 | /// returned in a register (for the Darwin and MCU ABI). |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1098 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1099 | ASTContext &Context) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1100 | uint64_t Size = Context.getTypeSize(Ty); |
| 1101 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1102 | // For i386, type must be register sized. |
| 1103 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1104 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1105 | return false; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1106 | |
| 1107 | if (Ty->isVectorType()) { |
| 1108 | // 64- and 128- bit vectors inside structures are not returned in |
| 1109 | // registers. |
| 1110 | if (Size == 64 || Size == 128) |
| 1111 | return false; |
| 1112 | |
| 1113 | return true; |
| 1114 | } |
| 1115 | |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1116 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1117 | // member function pointer it is ok. |
Daniel Dunbar | a1842d3 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1118 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | 55e59e1 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1119 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1120 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1121 | return true; |
| 1122 | |
| 1123 | // Arrays are treated like records. |
| 1124 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1125 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1126 | |
| 1127 | // Otherwise, it must be a record type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1128 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1129 | if (!RT) return false; |
| 1130 | |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1131 | // FIXME: Traverse bases here too. |
| 1132 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1133 | // Structure types are passed in register if all fields would be |
| 1134 | // passed in a register. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1135 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1136 | // Empty fields are ignored. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1137 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1138 | continue; |
| 1139 | |
| 1140 | // Check fields recursively. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1141 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1142 | return false; |
| 1143 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1144 | return true; |
| 1145 | } |
| 1146 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1147 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1148 | // Treat complex types as the element type. |
| 1149 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1150 | Ty = CTy->getElementType(); |
| 1151 | |
| 1152 | // Check for a type which we know has a simple scalar argument-passing |
| 1153 | // convention without any padding. (We're specifically looking for 32 |
| 1154 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1155 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1156 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1157 | return false; |
| 1158 | |
| 1159 | uint64_t Size = Context.getTypeSize(Ty); |
| 1160 | return Size == 32 || Size == 64; |
| 1161 | } |
| 1162 | |
| 1163 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1164 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1165 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1166 | /// optimizations. |
| 1167 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1168 | // We can only expand structure types. |
| 1169 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1170 | if (!RT) |
| 1171 | return false; |
| 1172 | const RecordDecl *RD = RT->getDecl(); |
| 1173 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1174 | if (!IsWin32StructABI ) { |
| 1175 | // On non-Windows, we have to conservatively match our old bitcode |
| 1176 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1177 | if (!CXXRD->isCLike()) |
| 1178 | return false; |
| 1179 | } else { |
| 1180 | // Don't do this for dynamic classes. |
| 1181 | if (CXXRD->isDynamicClass()) |
| 1182 | return false; |
| 1183 | // Don't do this if there are any non-empty bases. |
| 1184 | for (const CXXBaseSpecifier &Base : CXXRD->bases()) { |
| 1185 | if (!isEmptyRecord(getContext(), Base.getType(), /*AllowArrays=*/true)) |
| 1186 | return false; |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | uint64_t Size = 0; |
| 1192 | |
| 1193 | for (const auto *FD : RD->fields()) { |
| 1194 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1195 | // argument is smaller than 32-bits, expanding the struct will create |
| 1196 | // alignment padding. |
| 1197 | if (!is32Or64BitBasicType(FD->getType(), getContext())) |
| 1198 | return false; |
| 1199 | |
| 1200 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1201 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1202 | // counts as "basic" is more complicated than what we were doing previously. |
| 1203 | if (FD->isBitField()) |
| 1204 | return false; |
| 1205 | |
| 1206 | Size += getContext().getTypeSize(FD->getType()); |
| 1207 | } |
| 1208 | |
| 1209 | // We can do this if there was no alignment padding. |
| 1210 | return Size == getContext().getTypeSize(Ty); |
| 1211 | } |
| 1212 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1213 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1214 | // If the return value is indirect, then the hidden argument is consuming one |
| 1215 | // integer register. |
| 1216 | if (State.FreeRegs) { |
| 1217 | --State.FreeRegs; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1218 | if (!IsMCUABI) |
| 1219 | return getNaturalAlignIndirectInReg(RetTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1220 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1221 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1224 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1225 | CCState &State) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1226 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1227 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1228 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1229 | const Type *Base = nullptr; |
| 1230 | uint64_t NumElts = 0; |
| 1231 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1232 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1233 | // The LLVM struct type for such an aggregate should lower properly. |
| 1234 | return ABIArgInfo::getDirect(); |
| 1235 | } |
| 1236 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1237 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1238 | // On Darwin, some vectors are returned in registers. |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1239 | if (IsDarwinVectorABI) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1240 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1241 | |
| 1242 | // 128-bit vectors are a special case; they are returned in |
| 1243 | // registers and we need to make sure to pick a type the LLVM |
| 1244 | // backend will like. |
| 1245 | if (Size == 128) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1246 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1247 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1248 | |
| 1249 | // Always return in register if it fits in a general purpose |
| 1250 | // register, or if it is 64 bits and has a single element. |
| 1251 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1252 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1253 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1254 | Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1255 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1256 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | return ABIArgInfo::getDirect(); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1260 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1261 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1262 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1263 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1264 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1265 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1266 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1267 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1268 | |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1269 | // If specified, structs and unions are always indirect. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1270 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
| 1271 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1272 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1273 | // Ignore empty structs/unions. |
| 1274 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1275 | return ABIArgInfo::getIgnore(); |
| 1276 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1277 | // Small structures which are register sized are generally returned |
| 1278 | // in a register. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1279 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1280 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1281 | |
| 1282 | // As a special-case, if the struct is a "single-element" struct, and |
| 1283 | // the field is of type "float" or "double", return it in a |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1284 | // floating-point register. (MSVC does not apply this special case.) |
| 1285 | // We apply a similar transformation for pointer types to improve the |
| 1286 | // quality of the generated IR. |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1287 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1288 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1289 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1290 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1291 | |
| 1292 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1293 | // padding. |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1294 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1297 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1298 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1299 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1300 | // Treat an enum type as its underlying type. |
| 1301 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1302 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1303 | |
| 1304 | return (RetTy->isPromotableIntegerType() ? |
| 1305 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Eli Friedman | f4bd4d8 | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1308 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1309 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1310 | } |
| 1311 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1312 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1313 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1314 | if (!RT) |
| 1315 | return 0; |
| 1316 | const RecordDecl *RD = RT->getDecl(); |
| 1317 | |
| 1318 | // If this is a C++ record, check the bases first. |
| 1319 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1320 | for (const auto &I : CXXRD->bases()) |
| 1321 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1322 | return false; |
| 1323 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1324 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1325 | QualType FT = i->getType(); |
| 1326 | |
Eli Friedman | f4bd4d8 | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1327 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1328 | return true; |
| 1329 | |
| 1330 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1331 | return true; |
| 1332 | } |
| 1333 | |
| 1334 | return false; |
| 1335 | } |
| 1336 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1337 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1338 | unsigned Align) const { |
| 1339 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1340 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1341 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1342 | return 0; // Use default alignment. |
| 1343 | |
| 1344 | // On non-Darwin, the stack type alignment is always 4. |
| 1345 | if (!IsDarwinVectorABI) { |
| 1346 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1347 | return MinABIStackAlignInBytes; |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1348 | } |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1349 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1350 | // Otherwise, if the type contains an SSE vector type, the alignment is 16. |
Eli Friedman | f4bd4d8 | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1351 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1352 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1353 | return 16; |
| 1354 | |
| 1355 | return MinABIStackAlignInBytes; |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1358 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1359 | CCState &State) const { |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1360 | if (!ByVal) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1361 | if (State.FreeRegs) { |
| 1362 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1363 | if (!IsMCUABI) |
| 1364 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1365 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1366 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1367 | } |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1368 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1369 | // Compute the byval alignment. |
| 1370 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1371 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1372 | if (StackAlign == 0) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1373 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1374 | |
| 1375 | // If the stack alignment is less than the type alignment, realign the |
| 1376 | // argument. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1377 | bool Realign = TypeAlign > StackAlign; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1378 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1379 | /*ByVal=*/true, Realign); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1382 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1383 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1384 | if (!T) |
| 1385 | T = Ty.getTypePtr(); |
| 1386 | |
| 1387 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1388 | BuiltinType::Kind K = BT->getKind(); |
| 1389 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1390 | return Float; |
| 1391 | } |
| 1392 | return Integer; |
| 1393 | } |
| 1394 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1395 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1396 | if (!IsSoftFloatABI) { |
| 1397 | Class C = classify(Ty); |
| 1398 | if (C == Float) |
| 1399 | return false; |
| 1400 | } |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1401 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1402 | unsigned Size = getContext().getTypeSize(Ty); |
| 1403 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | 5f14fcb | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1404 | |
| 1405 | if (SizeInRegs == 0) |
| 1406 | return false; |
| 1407 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1408 | if (!IsMCUABI) { |
| 1409 | if (SizeInRegs > State.FreeRegs) { |
| 1410 | State.FreeRegs = 0; |
| 1411 | return false; |
| 1412 | } |
| 1413 | } else { |
| 1414 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1415 | // earlier parameters that are passed on the stack. Also, |
| 1416 | // it does not allow passing >8-byte structs in-register, |
| 1417 | // even if there are 3 free registers available. |
| 1418 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1419 | return false; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1420 | } |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1421 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1422 | State.FreeRegs -= SizeInRegs; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1423 | return true; |
| 1424 | } |
| 1425 | |
| 1426 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
| 1427 | bool &InReg, |
| 1428 | bool &NeedsPadding) const { |
| 1429 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1430 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1431 | // (HFAs) have already been dealt with at this point. |
| 1432 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1433 | return false; |
| 1434 | |
| 1435 | NeedsPadding = false; |
| 1436 | InReg = !IsMCUABI; |
| 1437 | |
| 1438 | if (!updateFreeRegs(Ty, State)) |
| 1439 | return false; |
| 1440 | |
| 1441 | if (IsMCUABI) |
| 1442 | return true; |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1443 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1444 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1445 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1446 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1447 | NeedsPadding = true; |
| 1448 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
| 1451 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1452 | return true; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1455 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1456 | if (!updateFreeRegs(Ty, State)) |
| 1457 | return false; |
| 1458 | |
| 1459 | if (IsMCUABI) |
| 1460 | return false; |
| 1461 | |
| 1462 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1463 | State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1464 | if (getContext().getTypeSize(Ty) > 32) |
| 1465 | return false; |
| 1466 | |
| 1467 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
| 1468 | Ty->isReferenceType()); |
| 1469 | } |
| 1470 | |
| 1471 | return true; |
| 1472 | } |
| 1473 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1474 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1475 | CCState &State) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1476 | // FIXME: Set alignment on indirect arguments. |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1477 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1478 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1479 | |
| 1480 | // Check with the C++ ABI first. |
| 1481 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1482 | if (RT) { |
| 1483 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1484 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1485 | return getIndirectResult(Ty, false, State); |
| 1486 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1487 | // The field index doesn't matter, we'll fix it up later. |
| 1488 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1493 | // to other targets. |
| 1494 | const Type *Base = nullptr; |
| 1495 | uint64_t NumElts = 0; |
| 1496 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1497 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1498 | if (State.FreeSSERegs >= NumElts) { |
| 1499 | State.FreeSSERegs -= NumElts; |
| 1500 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1501 | return ABIArgInfo::getDirect(); |
| 1502 | return ABIArgInfo::getExpand(); |
| 1503 | } |
| 1504 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1505 | } |
| 1506 | |
| 1507 | if (isAggregateTypeForABI(Ty)) { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1508 | // Structures with flexible arrays are always indirect. |
| 1509 | // FIXME: This should not be byval! |
| 1510 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1511 | return getIndirectResult(Ty, true, State); |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1512 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1513 | // Ignore empty structs/unions on non-Windows. |
| 1514 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1515 | return ABIArgInfo::getIgnore(); |
| 1516 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1517 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1518 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1519 | bool NeedsPadding = false; |
| 1520 | bool InReg; |
| 1521 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1522 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | b9bad79 | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1523 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1524 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1525 | if (InReg) |
| 1526 | return ABIArgInfo::getDirectInReg(Result); |
| 1527 | else |
| 1528 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1529 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1530 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1531 | |
Daniel Dunbar | 53012f4 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1532 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1533 | // of those arguments will match the struct. This is important because the |
| 1534 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1535 | // optimizations. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1536 | // Don't do this for the MCU if there are still free integer registers |
| 1537 | // (see X86_64 ABI for full explanation). |
| 1538 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1539 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1540 | return ABIArgInfo::getExpandWithPadding( |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1541 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1542 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1543 | PaddingType); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1544 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1545 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1548 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 7b73350 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1549 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1550 | // it as an i8/i16/i32/i64. |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1551 | if (IsDarwinVectorABI) { |
| 1552 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1553 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1554 | (Size == 64 && VT->getNumElements() == 1)) |
| 1555 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1556 | Size)); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1557 | } |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1558 | |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1559 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1560 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1561 | |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1562 | return ABIArgInfo::getDirect(); |
| 1563 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1564 | |
| 1565 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1566 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1567 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1568 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1569 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1570 | |
| 1571 | if (Ty->isPromotableIntegerType()) { |
| 1572 | if (InReg) |
| 1573 | return ABIArgInfo::getExtendInReg(); |
| 1574 | return ABIArgInfo::getExtend(); |
| 1575 | } |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1576 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1577 | if (InReg) |
| 1578 | return ABIArgInfo::getDirectInReg(); |
| 1579 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1582 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1583 | CCState State(FI.getCallingConvention()); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1584 | if (IsMCUABI) |
| 1585 | State.FreeRegs = 3; |
| 1586 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1587 | State.FreeRegs = 2; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1588 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1589 | State.FreeRegs = 2; |
| 1590 | State.FreeSSERegs = 6; |
| 1591 | } else if (FI.getHasRegParm()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1592 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1593 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1594 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1595 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1596 | if (!getCXXABI().classifyReturnType(FI)) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1597 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1598 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1599 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1600 | // return value was sret and put it in a register ourselves if appropriate. |
| 1601 | if (State.FreeRegs) { |
| 1602 | --State.FreeRegs; // The sret parameter consumes a register. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1603 | if (!IsMCUABI) |
| 1604 | FI.getReturnInfo().setInReg(true); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1605 | } |
| 1606 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1607 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1608 | // The chain argument effectively gives us another free register. |
| 1609 | if (FI.isChainCall()) |
| 1610 | ++State.FreeRegs; |
| 1611 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1612 | bool UsedInAlloca = false; |
| 1613 | for (auto &I : FI.arguments()) { |
| 1614 | I.info = classifyArgumentType(I.type, State); |
| 1615 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1618 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1619 | // all the memory arguments to use inalloca. |
| 1620 | if (UsedInAlloca) |
| 1621 | rewriteWithInAlloca(FI); |
| 1622 | } |
| 1623 | |
| 1624 | void |
| 1625 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1626 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1627 | QualType Type) const { |
| 1628 | // Arguments are always 4-byte-aligned. |
| 1629 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1630 | |
| 1631 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1632 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1633 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1634 | StackOffset += getContext().getTypeSizeInChars(Type); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1635 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1636 | // Insert padding bytes to respect alignment. |
| 1637 | CharUnits FieldEnd = StackOffset; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1638 | StackOffset = FieldEnd.alignTo(FieldAlign); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1639 | if (StackOffset != FieldEnd) { |
| 1640 | CharUnits NumBytes = StackOffset - FieldEnd; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1641 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1642 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1643 | FrameFields.push_back(Ty); |
| 1644 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1647 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1648 | // Leave ignored and inreg arguments alone. |
| 1649 | switch (Info.getKind()) { |
| 1650 | case ABIArgInfo::InAlloca: |
| 1651 | return true; |
| 1652 | case ABIArgInfo::Indirect: |
| 1653 | assert(Info.getIndirectByVal()); |
| 1654 | return true; |
| 1655 | case ABIArgInfo::Ignore: |
| 1656 | return false; |
| 1657 | case ABIArgInfo::Direct: |
| 1658 | case ABIArgInfo::Extend: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1659 | if (Info.getInReg()) |
| 1660 | return false; |
| 1661 | return true; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1662 | case ABIArgInfo::Expand: |
| 1663 | case ABIArgInfo::CoerceAndExpand: |
| 1664 | // These are aggregate types which are never passed in registers when |
| 1665 | // inalloca is involved. |
| 1666 | return true; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1667 | } |
| 1668 | llvm_unreachable("invalid enum"); |
| 1669 | } |
| 1670 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1671 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1672 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1673 | |
| 1674 | // Build a packed struct type for all of the arguments in memory. |
| 1675 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1676 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1677 | // The stack alignment is always 4. |
| 1678 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1679 | |
| 1680 | CharUnits StackOffset; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1681 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1682 | |
| 1683 | // Put 'this' into the struct before 'sret', if necessary. |
| 1684 | bool IsThisCall = |
| 1685 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1686 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1687 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1688 | isArgInAlloca(I->info)) { |
| 1689 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1690 | ++I; |
| 1691 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1692 | |
| 1693 | // Put the sret parameter into the inalloca struct if it's in memory. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1694 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1695 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1696 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
| 1697 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1698 | Ret.setInAllocaSRet(IsWin32StructABI); |
| 1699 | } |
| 1700 | |
| 1701 | // Skip the 'this' parameter in ecx. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1702 | if (IsThisCall) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1703 | ++I; |
| 1704 | |
| 1705 | // Put arguments passed in memory into the struct. |
| 1706 | for (; I != E; ++I) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1707 | if (isArgInAlloca(I->info)) |
| 1708 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1712 | /*isPacked=*/true), |
| 1713 | StackAlign); |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1716 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1717 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1718 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1719 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 7b1fb81 | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1720 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1721 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1722 | // |
| 1723 | // Just messing with TypeInfo like this works because we never pass |
| 1724 | // anything indirectly. |
| 1725 | TypeInfo.second = CharUnits::fromQuantity( |
| 1726 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 7b1fb81 | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1727 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1728 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1729 | TypeInfo, CharUnits::fromQuantity(4), |
| 1730 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1733 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1734 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1735 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1736 | |
| 1737 | switch (Opts.getStructReturnConvention()) { |
| 1738 | case CodeGenOptions::SRCK_Default: |
| 1739 | break; |
| 1740 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1741 | return false; |
| 1742 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1743 | return true; |
| 1744 | } |
| 1745 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1746 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1747 | return true; |
| 1748 | |
| 1749 | switch (Triple.getOS()) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1750 | case llvm::Triple::DragonFly: |
| 1751 | case llvm::Triple::FreeBSD: |
| 1752 | case llvm::Triple::OpenBSD: |
| 1753 | case llvm::Triple::Bitrig: |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1754 | case llvm::Triple::Win32: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1755 | return true; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1756 | default: |
| 1757 | return false; |
| 1758 | } |
| 1759 | } |
| 1760 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 1761 | void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1762 | llvm::GlobalValue *GV, |
| 1763 | CodeGen::CodeGenModule &CGM) const { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1764 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1765 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1766 | // Get the LLVM function. |
| 1767 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1768 | |
| 1769 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | 0d58339 | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1770 | llvm::AttrBuilder B; |
Bill Wendling | e91e9ec | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1771 | B.addStackAlignmentAttr(16); |
Bill Wendling | 909b6de | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1772 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1773 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1774 | llvm::AttributeSet::FunctionIndex, |
| 1775 | B)); |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1776 | } |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1777 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1778 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1779 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1780 | } |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1784 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1785 | CodeGen::CodeGenFunction &CGF, |
| 1786 | llvm::Value *Address) const { |
| 1787 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1788 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1789 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1790 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1791 | // 0-7 are the eight integer registers; the order is different |
| 1792 | // on Darwin (for EH), but the range is the same. |
| 1793 | // 8 is %eip. |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1794 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1795 | |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1796 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1797 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1798 | // These have size 16, which is sizeof(long double) on |
| 1799 | // platforms with 8-byte alignment for that type. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1800 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1801 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1802 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1803 | } else { |
| 1804 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1805 | // reason. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1806 | Builder.CreateAlignedStore( |
| 1807 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1808 | CharUnits::One()); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1809 | |
| 1810 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1811 | // These have size 12, which is sizeof(long double) on |
| 1812 | // platforms with 4-byte alignment for that type. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1813 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1814 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1815 | } |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1816 | |
| 1817 | return false; |
| 1818 | } |
| 1819 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1820 | //===----------------------------------------------------------------------===// |
| 1821 | // X86-64 ABI Implementation |
| 1822 | //===----------------------------------------------------------------------===// |
| 1823 | |
| 1824 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1825 | namespace { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1826 | /// The AVX ABI level for X86 targets. |
| 1827 | enum class X86AVXABILevel { |
| 1828 | None, |
| 1829 | AVX, |
| 1830 | AVX512 |
| 1831 | }; |
| 1832 | |
| 1833 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1834 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1835 | switch (AVXLevel) { |
| 1836 | case X86AVXABILevel::AVX512: |
| 1837 | return 512; |
| 1838 | case X86AVXABILevel::AVX: |
| 1839 | return 256; |
| 1840 | case X86AVXABILevel::None: |
| 1841 | return 128; |
| 1842 | } |
| 1843 | llvm_unreachable("Unknown AVXLevel"); |
| 1844 | } |
| 1845 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1846 | /// X86_64ABIInfo - The X86_64 ABI information. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1847 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1848 | enum Class { |
| 1849 | Integer = 0, |
| 1850 | SSE, |
| 1851 | SSEUp, |
| 1852 | X87, |
| 1853 | X87Up, |
| 1854 | ComplexX87, |
| 1855 | NoClass, |
| 1856 | Memory |
| 1857 | }; |
| 1858 | |
| 1859 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1860 | /// |
| 1861 | /// Merge an accumulating classification \arg Accum with a field |
| 1862 | /// classification \arg Field. |
| 1863 | /// |
| 1864 | /// \param Accum - The accumulating classification. This should |
| 1865 | /// always be either NoClass or the result of a previous merge |
| 1866 | /// call. In addition, this should never be Memory (the caller |
| 1867 | /// should just return Memory for the aggregate). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1868 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1869 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1870 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1871 | /// |
| 1872 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1873 | /// final MEMORY or SSE classes when necessary. |
| 1874 | /// |
| 1875 | /// \param AggregateSize - The size of the current aggregate in |
| 1876 | /// the classification process. |
| 1877 | /// |
| 1878 | /// \param Lo - The classification for the parts of the type |
| 1879 | /// residing in the low word of the containing object. |
| 1880 | /// |
| 1881 | /// \param Hi - The classification for the parts of the type |
| 1882 | /// residing in the higher words of the containing object. |
| 1883 | /// |
| 1884 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1885 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1886 | /// classify - Determine the x86_64 register classes in which the |
| 1887 | /// given type T should be passed. |
| 1888 | /// |
| 1889 | /// \param Lo - The classification for the parts of the type |
| 1890 | /// residing in the low word of the containing object. |
| 1891 | /// |
| 1892 | /// \param Hi - The classification for the parts of the type |
| 1893 | /// residing in the high word of the containing object. |
| 1894 | /// |
| 1895 | /// \param OffsetBase - The bit offset of this type in the |
| 1896 | /// containing object. Some parameters are classified different |
| 1897 | /// depending on whether they straddle an eightbyte boundary. |
| 1898 | /// |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1899 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1900 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1901 | /// |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1902 | /// If a word is unused its result will be NoClass; if a type should |
| 1903 | /// be passed in Memory then at least the classification of \arg Lo |
| 1904 | /// will be Memory. |
| 1905 | /// |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1906 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1907 | /// |
| 1908 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1909 | /// also be ComplexX87. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1910 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1911 | bool isNamedArg) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1912 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1913 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1914 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1915 | unsigned IROffset, QualType SourceTy, |
| 1916 | unsigned SourceOffset) const; |
| 1917 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1918 | unsigned IROffset, QualType SourceTy, |
| 1919 | unsigned SourceOffset) const; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1920 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1921 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1922 | /// such that the argument will be returned in memory. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1923 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1924 | |
| 1925 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1926 | /// such that the argument will be passed in memory. |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1927 | /// |
| 1928 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1929 | /// available. |
| 1930 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1931 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1932 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1933 | |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1934 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1935 | unsigned freeIntRegs, |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1936 | unsigned &neededInt, |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1937 | unsigned &neededSSE, |
| 1938 | bool isNamedArg) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1939 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1940 | bool IsIllegalVectorType(QualType Ty) const; |
| 1941 | |
John McCall | 67a5773 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1942 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1943 | /// unfortunately in ways that were not always consistent with |
| 1944 | /// certain previous compilers. In particular, platforms which |
| 1945 | /// required strict binary compatibility with older versions of GCC |
| 1946 | /// may need to exempt themselves. |
| 1947 | bool honorsRevision0_98() const { |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1948 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | 67a5773 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1951 | /// GCC classifies <1 x long long> as SSE but compatibility with older clang |
| 1952 | // compilers require us to classify it as INTEGER. |
| 1953 | bool classifyIntegerMMXAsSSE() const { |
| 1954 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 1955 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 1956 | return false; |
| 1957 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 1958 | return false; |
| 1959 | return true; |
| 1960 | } |
| 1961 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1962 | X86AVXABILevel AVXLevel; |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1963 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1964 | // 64-bit hardware. |
| 1965 | bool Has64BitPointers; |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1966 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1967 | public: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1968 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1969 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 90da80c | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1970 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1971 | } |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1972 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1973 | bool isPassedUsingAVXType(QualType type) const { |
| 1974 | unsigned neededInt, neededSSE; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1975 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1976 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1977 | /*isNamedArg*/true); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1978 | if (info.isDirect()) { |
| 1979 | llvm::Type *ty = info.getCoerceToType(); |
| 1980 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1981 | return (vectorTy->getBitWidth() > 128); |
| 1982 | } |
| 1983 | return false; |
| 1984 | } |
| 1985 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1986 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1987 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 1988 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1989 | QualType Ty) const override; |
| 1990 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1991 | QualType Ty) const override; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1992 | |
| 1993 | bool has64BitPointers() const { |
| 1994 | return Has64BitPointers; |
| 1995 | } |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 1996 | |
| 1997 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 1998 | ArrayRef<llvm::Type*> scalars, |
| 1999 | bool asReturnValue) const override { |
| 2000 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2001 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2002 | }; |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2003 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2004 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2005 | class WinX86_64ABIInfo : public ABIInfo { |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2006 | public: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2007 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
| 2008 | : ABIInfo(CGT), |
| 2009 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2010 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2011 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2012 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2013 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2014 | QualType Ty) const override; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2015 | |
| 2016 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2017 | // FIXME: Assumes vectorcall is in use. |
| 2018 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2019 | } |
| 2020 | |
| 2021 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2022 | uint64_t NumMembers) const override { |
| 2023 | // FIXME: Assumes vectorcall is in use. |
| 2024 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2025 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2026 | |
| 2027 | private: |
| 2028 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 2029 | bool IsReturnType) const; |
| 2030 | |
| 2031 | bool IsMingw64; |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2032 | }; |
| 2033 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2034 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2035 | public: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2036 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2037 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2038 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2039 | const X86_64ABIInfo &getABIInfo() const { |
| 2040 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2041 | } |
| 2042 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2043 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2044 | return 7; |
| 2045 | } |
| 2046 | |
| 2047 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2048 | llvm::Value *Address) const override { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2049 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2050 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2051 | // 0-15 are the 16 integer registers. |
| 2052 | // 16 is %rip. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2053 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2054 | return false; |
| 2055 | } |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2056 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2057 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2058 | StringRef Constraint, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2059 | llvm::Type* Ty) const override { |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2060 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2061 | } |
| 2062 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2063 | bool isNoProtoCallVariadic(const CallArgList &args, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2064 | const FunctionNoProtoType *fnType) const override { |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2065 | // The default CC on x86-64 sets %al to the number of SSA |
| 2066 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2067 | // function, so we override the default behavior. However, don't do |
Eli Friedman | 68805fe | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2068 | // that when AVX types are involved: the ABI explicitly states it is |
| 2069 | // undefined, and it doesn't work in practice because of how the ABI |
| 2070 | // defines varargs anyway. |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2071 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2072 | bool HasAVXType = false; |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2073 | for (CallArgList::const_iterator |
| 2074 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2075 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2076 | HasAVXType = true; |
| 2077 | break; |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2078 | } |
| 2079 | } |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2080 | |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2081 | if (!HasAVXType) |
| 2082 | return true; |
| 2083 | } |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2084 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2085 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2088 | llvm::Constant * |
| 2089 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2090 | unsigned Sig; |
| 2091 | if (getABIInfo().has64BitPointers()) |
| 2092 | Sig = (0xeb << 0) | // jmp rel8 |
| 2093 | (0x0a << 8) | // .+0x0c |
| 2094 | ('F' << 16) | |
| 2095 | ('T' << 24); |
| 2096 | else |
| 2097 | Sig = (0xeb << 0) | // jmp rel8 |
| 2098 | (0x06 << 8) | // .+0x08 |
| 2099 | ('F' << 16) | |
| 2100 | ('T' << 24); |
Peter Collingbourne | b914e87 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2101 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2102 | } |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 2103 | |
| 2104 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2105 | CodeGen::CodeGenModule &CGM) const override { |
| 2106 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2107 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2108 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2109 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2110 | } |
| 2111 | } |
| 2112 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2113 | }; |
| 2114 | |
| 2115 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2116 | public: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2117 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2118 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2119 | |
| 2120 | void getDependentLibraryOption(llvm::StringRef Lib, |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 2121 | llvm::SmallString<24> &Opt) const override { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2122 | Opt = "\01"; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2123 | // If the argument contains a space, enclose it in quotes. |
| 2124 | if (Lib.find(" ") != StringRef::npos) |
| 2125 | Opt += "\"" + Lib.str() + "\""; |
| 2126 | else |
| 2127 | Opt += Lib; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2128 | } |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2129 | }; |
| 2130 | |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2131 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2132 | // If the argument does not end in .lib, automatically add the suffix. |
| 2133 | // If the argument contains a space, enclose it in quotes. |
| 2134 | // This matches the behavior of MSVC. |
| 2135 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2136 | std::string ArgStr = Quote ? "\"" : ""; |
| 2137 | ArgStr += Lib; |
Rui Ueyama | 723cead | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 2138 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2139 | ArgStr += ".lib"; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2140 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2141 | return ArgStr; |
| 2142 | } |
| 2143 | |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2144 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2145 | public: |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2146 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2147 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2148 | unsigned NumRegisterParameters) |
| 2149 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
| 2150 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2151 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 2152 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2153 | CodeGen::CodeGenModule &CGM) const override; |
| 2154 | |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2155 | void getDependentLibraryOption(llvm::StringRef Lib, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2156 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2157 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2158 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2159 | } |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2160 | |
| 2161 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2162 | llvm::StringRef Value, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2163 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | 572ac32 | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2164 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2165 | } |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2166 | }; |
| 2167 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2168 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 2169 | llvm::GlobalValue *GV, |
| 2170 | CodeGen::CodeGenModule &CGM) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2171 | if (D && isa<FunctionDecl>(D)) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2172 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 2173 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2174 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 2175 | Fn->addFnAttr("stack-probe-size", |
| 2176 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2177 | } |
| 2178 | } |
| 2179 | } |
| 2180 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 2181 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2182 | llvm::GlobalValue *GV, |
| 2183 | CodeGen::CodeGenModule &CGM) const { |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 2184 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2185 | |
| 2186 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2187 | } |
| 2188 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2189 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 2190 | public: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2191 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2192 | X86AVXABILevel AVXLevel) |
| 2193 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 2194 | |
| 2195 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2196 | CodeGen::CodeGenModule &CGM) const override; |
| 2197 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2198 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2199 | return 7; |
| 2200 | } |
| 2201 | |
| 2202 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2203 | llvm::Value *Address) const override { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2204 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2205 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2206 | // 0-15 are the 16 integer registers. |
| 2207 | // 16 is %rip. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2208 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2209 | return false; |
| 2210 | } |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2211 | |
| 2212 | void getDependentLibraryOption(llvm::StringRef Lib, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2213 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2214 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | 89735b9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2215 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2216 | } |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2217 | |
| 2218 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2219 | llvm::StringRef Value, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2220 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | 572ac32 | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2221 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2222 | } |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2223 | }; |
| 2224 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 2225 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2226 | llvm::GlobalValue *GV, |
| 2227 | CodeGen::CodeGenModule &CGM) const { |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 2228 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2229 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 2230 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2231 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2232 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2233 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2234 | } |
| 2235 | } |
| 2236 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2237 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2238 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2241 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2242 | Class &Hi) const { |
| 2243 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2244 | // |
| 2245 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2246 | // memory. |
| 2247 | // |
| 2248 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2249 | // memory. |
| 2250 | // |
| 2251 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2252 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2253 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2254 | // ABI working for processors that don't support the __m256 type. |
| 2255 | // |
| 2256 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2257 | // |
| 2258 | // Some of these are enforced by the merging logic. Others can arise |
| 2259 | // only with unions; for example: |
| 2260 | // union { _Complex double; unsigned; } |
| 2261 | // |
| 2262 | // Note that clauses (b) and (c) were added in 0.98. |
| 2263 | // |
| 2264 | if (Hi == Memory) |
| 2265 | Lo = Memory; |
| 2266 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2267 | Lo = Memory; |
| 2268 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2269 | Lo = Memory; |
| 2270 | if (Hi == SSEUp && Lo != SSE) |
| 2271 | Hi = SSE; |
| 2272 | } |
| 2273 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2274 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2275 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2276 | // classified recursively so that always two fields are |
| 2277 | // considered. The resulting class is calculated according to |
| 2278 | // the classes of the fields in the eightbyte: |
| 2279 | // |
| 2280 | // (a) If both classes are equal, this is the resulting class. |
| 2281 | // |
| 2282 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2283 | // the other class. |
| 2284 | // |
| 2285 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2286 | // class. |
| 2287 | // |
| 2288 | // (d) If one of the classes is INTEGER, the result is the |
| 2289 | // INTEGER. |
| 2290 | // |
| 2291 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2292 | // MEMORY is used as class. |
| 2293 | // |
| 2294 | // (f) Otherwise class SSE is used. |
| 2295 | |
| 2296 | // Accum should never be memory (we should have returned) or |
| 2297 | // ComplexX87 (because this cannot be passed in a structure). |
| 2298 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2299 | "Invalid accumulated classification during merge."); |
| 2300 | if (Accum == Field || Field == NoClass) |
| 2301 | return Accum; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2302 | if (Field == Memory) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2303 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2304 | if (Accum == NoClass) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2305 | return Field; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2306 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2307 | return Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2308 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2309 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2310 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2311 | return SSE; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
Chris Lattner | bcaedae | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2314 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2315 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2316 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2317 | // Class pairs with appropriate constructor methods for the various |
| 2318 | // situations. |
| 2319 | |
| 2320 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2321 | // shouldn't be passed in registers for example, so there is no chance they |
| 2322 | // can straddle an eightbyte. Verify & simplify. |
| 2323 | |
| 2324 | Lo = Hi = NoClass; |
| 2325 | |
| 2326 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2327 | Current = Memory; |
| 2328 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2329 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2330 | BuiltinType::Kind k = BT->getKind(); |
| 2331 | |
| 2332 | if (k == BuiltinType::Void) { |
| 2333 | Current = NoClass; |
| 2334 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2335 | Lo = Integer; |
| 2336 | Hi = Integer; |
| 2337 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2338 | Current = Integer; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2339 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2340 | Current = SSE; |
| 2341 | } else if (k == BuiltinType::LongDouble) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2342 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2343 | if (LDF == &llvm::APFloat::IEEEquad) { |
| 2344 | Lo = SSE; |
| 2345 | Hi = SSEUp; |
| 2346 | } else if (LDF == &llvm::APFloat::x87DoubleExtended) { |
| 2347 | Lo = X87; |
| 2348 | Hi = X87Up; |
| 2349 | } else if (LDF == &llvm::APFloat::IEEEdouble) { |
| 2350 | Current = SSE; |
| 2351 | } else |
| 2352 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2353 | } |
| 2354 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2355 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2356 | return; |
| 2357 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2358 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2359 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2360 | // Classify the underlying integer type. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2361 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2362 | return; |
| 2363 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2364 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2365 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2366 | Current = Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2367 | return; |
| 2368 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2369 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2370 | if (Ty->isMemberPointerType()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2371 | if (Ty->isMemberFunctionPointerType()) { |
| 2372 | if (Has64BitPointers) { |
| 2373 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2374 | // Lo and Hi now. |
| 2375 | Lo = Hi = Integer; |
| 2376 | } else { |
| 2377 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2378 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2379 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2380 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2381 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2382 | Lo = Hi = Integer; |
| 2383 | } else { |
| 2384 | Current = Integer; |
| 2385 | } |
| 2386 | } |
| 2387 | } else { |
Daniel Dunbar | 67d438d | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2388 | Current = Integer; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2389 | } |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2390 | return; |
| 2391 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2392 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2393 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2394 | uint64_t Size = getContext().getTypeSize(VT); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2395 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2396 | // gcc passes the following as integer: |
| 2397 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2398 | // 2 bytes - <2 x char>, <1 x short> |
| 2399 | // 1 byte - <1 x char> |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2400 | Current = Integer; |
| 2401 | |
| 2402 | // If this type crosses an eightbyte boundary, it should be |
| 2403 | // split. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2404 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2405 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2406 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2407 | Hi = Lo; |
| 2408 | } else if (Size == 64) { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 2409 | QualType ElementType = VT->getElementType(); |
| 2410 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2411 | // gcc passes <1 x double> in memory. :( |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 2412 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2413 | return; |
| 2414 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 2415 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2416 | // pass them as integer. For platforms where clang is the de facto |
| 2417 | // platform compiler, we must continue to use integer. |
| 2418 | if (!classifyIntegerMMXAsSSE() && |
| 2419 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2420 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2421 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2422 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2423 | Current = Integer; |
| 2424 | else |
| 2425 | Current = SSE; |
| 2426 | |
| 2427 | // If this type crosses an eightbyte boundary, it should be |
| 2428 | // split. |
| 2429 | if (OffsetBase && OffsetBase != 64) |
| 2430 | Hi = Lo; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2431 | } else if (Size == 128 || |
| 2432 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2433 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2434 | // least significant one belongs to class SSE and all the others to class |
| 2435 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2436 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2437 | // This design isn't correct for 256-bits, but since there're no cases |
| 2438 | // where the upper parts would need to be inspected, avoid adding |
| 2439 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2440 | // |
| 2441 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2442 | // registers if they are "named", i.e. not part of the "..." of a |
| 2443 | // variadic function. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2444 | // |
| 2445 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2446 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2447 | Lo = SSE; |
| 2448 | Hi = SSEUp; |
| 2449 | } |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2450 | return; |
| 2451 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2452 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2453 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2454 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2455 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2456 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2457 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2458 | if (Size <= 64) |
| 2459 | Current = Integer; |
| 2460 | else if (Size <= 128) |
| 2461 | Lo = Hi = Integer; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2462 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2463 | Current = SSE; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2464 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2465 | Lo = Hi = SSE; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2466 | } else if (ET == getContext().LongDoubleTy) { |
| 2467 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2468 | if (LDF == &llvm::APFloat::IEEEquad) |
| 2469 | Current = Memory; |
| 2470 | else if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 2471 | Current = ComplexX87; |
| 2472 | else if (LDF == &llvm::APFloat::IEEEdouble) |
| 2473 | Lo = Hi = SSE; |
| 2474 | else |
| 2475 | llvm_unreachable("unexpected long double representation!"); |
| 2476 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2477 | |
| 2478 | // If this complex type crosses an eightbyte boundary then it |
| 2479 | // should be split. |
| 2480 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2481 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2482 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2483 | Hi = Lo; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2484 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2485 | return; |
| 2486 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2487 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2488 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2489 | // Arrays are treated like structures. |
| 2490 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2491 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2492 | |
| 2493 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2494 | // than four eightbytes, ..., it has class MEMORY. |
| 2495 | if (Size > 256) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2496 | return; |
| 2497 | |
| 2498 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2499 | // fields, it has class MEMORY. |
| 2500 | // |
| 2501 | // Only need to check alignment of array base. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2502 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2503 | return; |
| 2504 | |
| 2505 | // Otherwise implement simplified merge. We could be smarter about |
| 2506 | // this, but it isn't worth it and would be harder to verify. |
| 2507 | Current = NoClass; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2508 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2509 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 089d892 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2510 | |
| 2511 | // The only case a 256-bit wide vector could be used is when the array |
| 2512 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2513 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2514 | if (Size > 128 && EltSize != 256) |
| 2515 | return; |
| 2516 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2517 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2518 | Class FieldLo, FieldHi; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2519 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2520 | Lo = merge(Lo, FieldLo); |
| 2521 | Hi = merge(Hi, FieldHi); |
| 2522 | if (Lo == Memory || Hi == Memory) |
| 2523 | break; |
| 2524 | } |
| 2525 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2526 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2527 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2528 | return; |
| 2529 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2530 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2531 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2532 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2533 | |
| 2534 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2535 | // than four eightbytes, ..., it has class MEMORY. |
| 2536 | if (Size > 256) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2537 | return; |
| 2538 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2539 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2540 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2541 | // reference. |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2542 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2543 | return; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2544 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2545 | const RecordDecl *RD = RT->getDecl(); |
| 2546 | |
| 2547 | // Assume variable sized types are passed in memory. |
| 2548 | if (RD->hasFlexibleArrayMember()) |
| 2549 | return; |
| 2550 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2551 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2552 | |
| 2553 | // Reset Lo class, this will be recomputed. |
| 2554 | Current = NoClass; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2555 | |
| 2556 | // If this is a C++ record, classify the bases first. |
| 2557 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2558 | for (const auto &I : CXXRD->bases()) { |
| 2559 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2560 | "Unexpected base class!"); |
| 2561 | const CXXRecordDecl *Base = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2562 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2563 | |
| 2564 | // Classify this field. |
| 2565 | // |
| 2566 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2567 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2568 | // initialized to class NO_CLASS. |
| 2569 | Class FieldLo, FieldHi; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2570 | uint64_t Offset = |
| 2571 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2572 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2573 | Lo = merge(Lo, FieldLo); |
| 2574 | Hi = merge(Hi, FieldHi); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2575 | if (Lo == Memory || Hi == Memory) { |
| 2576 | postMerge(Size, Lo, Hi); |
| 2577 | return; |
| 2578 | } |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2583 | unsigned idx = 0; |
Bruno Cardoso Lopes | 548e478 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2584 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2585 | i != e; ++i, ++idx) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2586 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2587 | bool BitField = i->isBitField(); |
| 2588 | |
Bruno Cardoso Lopes | b8981df | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2589 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2590 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2591 | // |
Bruno Cardoso Lopes | b8981df | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2592 | // The only case a 256-bit wide vector could be used is when the struct |
| 2593 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2594 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2595 | // |
| 2596 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2597 | Lo = Memory; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2598 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | b8981df | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2599 | return; |
| 2600 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2601 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2602 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2603 | Lo = Memory; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2604 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2605 | return; |
| 2606 | } |
| 2607 | |
| 2608 | // Classify this field. |
| 2609 | // |
| 2610 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2611 | // exceeds a single eightbyte, each is classified |
| 2612 | // separately. Each eightbyte gets initialized to class |
| 2613 | // NO_CLASS. |
| 2614 | Class FieldLo, FieldHi; |
| 2615 | |
| 2616 | // Bit-fields require special handling, they do not force the |
| 2617 | // structure to be passed in memory even if unaligned, and |
| 2618 | // therefore they can straddle an eightbyte. |
| 2619 | if (BitField) { |
| 2620 | // Ignore padding bit-fields. |
| 2621 | if (i->isUnnamedBitfield()) |
| 2622 | continue; |
| 2623 | |
| 2624 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2625 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2626 | |
| 2627 | uint64_t EB_Lo = Offset / 64; |
| 2628 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 9a6002a | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2629 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2630 | if (EB_Lo) { |
| 2631 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2632 | FieldLo = NoClass; |
| 2633 | FieldHi = Integer; |
| 2634 | } else { |
| 2635 | FieldLo = Integer; |
| 2636 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2637 | } |
| 2638 | } else |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2639 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2640 | Lo = merge(Lo, FieldLo); |
| 2641 | Hi = merge(Hi, FieldHi); |
| 2642 | if (Lo == Memory || Hi == Memory) |
| 2643 | break; |
| 2644 | } |
| 2645 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2646 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2647 | } |
| 2648 | } |
| 2649 | |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2650 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2651 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2652 | // place naturally. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2653 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2654 | // Treat an enum type as its underlying type. |
| 2655 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2656 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2657 | |
| 2658 | return (Ty->isPromotableIntegerType() ? |
| 2659 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2660 | } |
| 2661 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2662 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2665 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2666 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2667 | uint64_t Size = getContext().getTypeSize(VecTy); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2668 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2669 | if (Size <= 64 || Size > LargestVector) |
| 2670 | return true; |
| 2671 | } |
| 2672 | |
| 2673 | return false; |
| 2674 | } |
| 2675 | |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2676 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2677 | unsigned freeIntRegs) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2678 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2679 | // place naturally. |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2680 | // |
| 2681 | // This assumption is optimistic, as there could be free registers available |
| 2682 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2683 | // the argument in the free register. This does not seem to happen currently, |
| 2684 | // but this code would be much safer if we could mark the argument with |
| 2685 | // 'onstack'. See PR12193. |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2686 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2687 | // Treat an enum type as its underlying type. |
| 2688 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2689 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2690 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2691 | return (Ty->isPromotableIntegerType() ? |
| 2692 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2693 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2694 | |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2695 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2696 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2697 | |
Chris Lattner | 855d227 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2698 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2699 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2700 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2701 | |
| 2702 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2703 | // is important for good codegen. |
| 2704 | // |
| 2705 | // We do this by coercing the value into a scalar type which the backend can |
| 2706 | // handle naturally (i.e., without using byval). |
| 2707 | // |
| 2708 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2709 | // free integer registers. Doing this when there are free integer registers |
| 2710 | // would require more care, as we would have to ensure that the coerced value |
| 2711 | // did not claim the unused register. That would require either reording the |
| 2712 | // arguments to the function (so that any subsequent inreg values came first), |
| 2713 | // or only doing this optimization when there were no following arguments that |
| 2714 | // might be inreg. |
| 2715 | // |
| 2716 | // We currently expect it to be rare (particularly in well written code) for |
| 2717 | // arguments to be passed on the stack when there are still free integer |
| 2718 | // registers available (this would typically imply large structs being passed |
| 2719 | // by value), so this seems like a fair tradeoff for now. |
| 2720 | // |
| 2721 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2722 | // attributes. See PR12193. |
| 2723 | if (freeIntRegs == 0) { |
| 2724 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2725 | |
| 2726 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2727 | // type, which will end up on the stack (with alignment 8). |
| 2728 | if (Align == 8 && Size <= 64) |
| 2729 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2730 | Size)); |
| 2731 | } |
| 2732 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2733 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2736 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2737 | /// register. Pick an LLVM IR type that will be passed as a vector register. |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2738 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2739 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2740 | // vectors; strip them off if present. |
| 2741 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2742 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2743 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2744 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2745 | if (isa<llvm::VectorType>(IRType) || |
| 2746 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 2747 | return IRType; |
| 2748 | |
| 2749 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2750 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2751 | assert((Size == 128 || Size == 256) && "Invalid type found!"); |
| 2752 | |
| 2753 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2754 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2755 | Size / 64); |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2758 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2759 | /// is known to either be off the end of the specified type or being in |
| 2760 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2761 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2762 | /// classification that put one of the two halves in the INTEGER class. |
| 2763 | /// |
| 2764 | /// It is conservatively correct to return false. |
| 2765 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2766 | unsigned EndBit, ASTContext &Context) { |
| 2767 | // If the bytes being queried are off the end of the type, there is no user |
| 2768 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2769 | // types that don't contain interesting padding. |
| 2770 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2771 | if (TySize <= StartBit) |
| 2772 | return true; |
| 2773 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2774 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2775 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2776 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2777 | |
| 2778 | // Check each element to see if the element overlaps with the queried range. |
| 2779 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2780 | // If the element is after the span we care about, then we're done.. |
| 2781 | unsigned EltOffset = i*EltSize; |
| 2782 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2783 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2784 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2785 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2786 | EndBit-EltOffset, Context)) |
| 2787 | return false; |
| 2788 | } |
| 2789 | // If it overlaps no elements, then it is safe to process as padding. |
| 2790 | return true; |
| 2791 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2792 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2793 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2794 | const RecordDecl *RD = RT->getDecl(); |
| 2795 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2796 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2797 | // If this is a C++ record, check the bases first. |
| 2798 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2799 | for (const auto &I : CXXRD->bases()) { |
| 2800 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2801 | "Unexpected base class!"); |
| 2802 | const CXXRecordDecl *Base = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2803 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2804 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2805 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2806 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2807 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2808 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2809 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2810 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2811 | EndBit-BaseOffset, Context)) |
| 2812 | return false; |
| 2813 | } |
| 2814 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2815 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2816 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2817 | // this could be sped up a lot by being smarter about queried fields, |
| 2818 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2819 | // much. |
| 2820 | unsigned idx = 0; |
| 2821 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2822 | i != e; ++i, ++idx) { |
| 2823 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2824 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2825 | // If we found a field after the region we care about, then we're done. |
| 2826 | if (FieldOffset >= EndBit) break; |
| 2827 | |
| 2828 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2829 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2830 | Context)) |
| 2831 | return false; |
| 2832 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2833 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2834 | // If nothing in this record overlapped the area of interest, then we're |
| 2835 | // clean. |
| 2836 | return true; |
| 2837 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2838 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2839 | return false; |
| 2840 | } |
| 2841 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2842 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2843 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2844 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2845 | /// false. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2846 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2847 | const llvm::DataLayout &TD) { |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2848 | // Base case if we find a float. |
| 2849 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2850 | return true; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2851 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2852 | // If this is a struct, recurse into the field at the specified offset. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2853 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2854 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2855 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2856 | IROffset -= SL->getElementOffset(Elt); |
| 2857 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2858 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2859 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2860 | // If this is an array, recurse into the field at the specified offset. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2861 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2862 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2863 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2864 | IROffset -= IROffset/EltSize*EltSize; |
| 2865 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2866 | } |
| 2867 | |
| 2868 | return false; |
| 2869 | } |
| 2870 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2871 | |
| 2872 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2873 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2874 | llvm::Type *X86_64ABIInfo:: |
| 2875 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2876 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | cba8d31 | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2877 | // The only three choices we have are either double, <2 x float>, or float. We |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2878 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2879 | // structs that contain 3 floats. |
| 2880 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2881 | SourceOffset*8+64, getContext())) |
| 2882 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2883 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2884 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2885 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2886 | // case. |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2887 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2888 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 22fd4ba | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2889 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2890 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2891 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2892 | } |
| 2893 | |
| 2894 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2895 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2896 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2897 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2898 | /// the best LLVM IR type to represent this, which may be i64 or may be anything |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2899 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2900 | /// etc). |
| 2901 | /// |
| 2902 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2903 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2904 | /// the 8-byte value references. PrefType may be null. |
| 2905 | /// |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2906 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2907 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2908 | /// |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2909 | llvm::Type *X86_64ABIInfo:: |
| 2910 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2911 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2912 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2913 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2914 | if (IROffset == 0) { |
| 2915 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2916 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2917 | IRType->isIntegerTy(64)) |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2918 | return IRType; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2919 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2920 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2921 | // goodness in the source type is just tail padding. This is allowed to |
| 2922 | // kick in for struct {double,int} on the int, but not on |
| 2923 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2924 | // have to do this analysis on the source type because we can't depend on |
| 2925 | // unions being lowered a specific way etc. |
| 2926 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2927 | IRType->isIntegerTy(32) || |
| 2928 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2929 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2930 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2931 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2932 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2933 | SourceOffset*8+64, getContext())) |
| 2934 | return IRType; |
| 2935 | } |
| 2936 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2937 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2938 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2939 | // If this is a struct, recurse into the field at the specified offset. |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2940 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2941 | if (IROffset < SL->getSizeInBytes()) { |
| 2942 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2943 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2944 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2945 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2946 | SourceTy, SourceOffset); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2947 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2948 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2949 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2950 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2951 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2952 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2953 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2954 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2955 | SourceOffset); |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2956 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2957 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2958 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2959 | // integer register that isn't too big to fit the rest of the struct. |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2960 | unsigned TySizeInBytes = |
| 2961 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2962 | |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2963 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2964 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2965 | // It is always safe to classify this as an integer type up to i64 that |
| 2966 | // isn't larger than the structure. |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2967 | return llvm::IntegerType::get(getVMContext(), |
| 2968 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2971 | |
| 2972 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2973 | /// be used as elements of a two register pair to pass or return, return a |
| 2974 | /// first class aggregate to represent them. For example, if the low part of |
| 2975 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2976 | /// return {i32*, float}. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2977 | static llvm::Type * |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2978 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2979 | const llvm::DataLayout &TD) { |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2980 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2981 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2982 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2983 | // the second element at offset 8. Check for this: |
| 2984 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2985 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 2986 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2987 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2988 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2989 | // To handle this, we have to increase the size of the low part so that the |
| 2990 | // second element will start at an 8 byte offset. We can't increase the size |
| 2991 | // of the second element because it might make us access off the end of the |
| 2992 | // struct. |
| 2993 | if (HiStart != 8) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 2994 | // There are usually two sorts of types the ABI generation code can produce |
| 2995 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 2996 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 2997 | // NaCl). |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2998 | // Promote these to a larger type. |
| 2999 | if (Lo->isFloatTy()) |
| 3000 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3001 | else { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3002 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3003 | && "Invalid/unknown lo type"); |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3004 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3005 | } |
| 3006 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3007 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3008 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3009 | |
| 3010 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3011 | // Verify that the second element is at an 8-byte offset. |
| 3012 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3013 | "Invalid x86-64 argument pair!"); |
| 3014 | return Result; |
| 3015 | } |
| 3016 | |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3017 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3018 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3019 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3020 | // classification algorithm. |
| 3021 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3022 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3023 | |
| 3024 | // Check some invariants. |
| 3025 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3026 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3027 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3028 | llvm::Type *ResType = nullptr; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3029 | switch (Lo) { |
| 3030 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3031 | if (Hi == NoClass) |
| 3032 | return ABIArgInfo::getIgnore(); |
| 3033 | // If the low part is just padding, it takes no register, leave ResType |
| 3034 | // null. |
| 3035 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3036 | "Unknown missing lo part"); |
| 3037 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3038 | |
| 3039 | case SSEUp: |
| 3040 | case X87Up: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3041 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3042 | |
| 3043 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3044 | // hidden argument. |
| 3045 | case Memory: |
| 3046 | return getIndirectReturnResult(RetTy); |
| 3047 | |
| 3048 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3049 | // available register of the sequence %rax, %rdx is used. |
| 3050 | case Integer: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3051 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3052 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3053 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3054 | // so that the parameter gets the right LLVM IR attributes. |
| 3055 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3056 | // Treat an enum type as its underlying type. |
| 3057 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3058 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3059 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3060 | if (RetTy->isIntegralOrEnumerationType() && |
| 3061 | RetTy->isPromotableIntegerType()) |
| 3062 | return ABIArgInfo::getExtend(); |
| 3063 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3064 | break; |
| 3065 | |
| 3066 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3067 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3068 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3069 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3070 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3071 | |
| 3072 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3073 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3074 | case X87: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3075 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3076 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3077 | |
| 3078 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3079 | // part of the value is returned in %st0 and the imaginary part in |
| 3080 | // %st1. |
| 3081 | case ComplexX87: |
| 3082 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3083 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3084 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3085 | nullptr); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3086 | break; |
| 3087 | } |
| 3088 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3089 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3090 | switch (Hi) { |
| 3091 | // Memory was handled previously and X87 should |
| 3092 | // never occur as a hi class. |
| 3093 | case Memory: |
| 3094 | case X87: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3095 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3096 | |
| 3097 | case ComplexX87: // Previously handled. |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3098 | case NoClass: |
| 3099 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3100 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3101 | case Integer: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3102 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3103 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3104 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3105 | break; |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3106 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3107 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3108 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3109 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3110 | break; |
| 3111 | |
| 3112 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3113 | // is passed in the next available eightbyte chunk if the last used |
| 3114 | // vector register. |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3115 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3116 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3117 | case SSEUp: |
| 3118 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3119 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3120 | break; |
| 3121 | |
| 3122 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3123 | // returned together with the previous X87 value in %st0. |
| 3124 | case X87Up: |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3125 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3126 | // anything. However, in some cases with unions it may not be |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3127 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3128 | // extra bits in an SSE reg. |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3129 | if (Lo != X87) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3130 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3131 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3132 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3133 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3134 | break; |
| 3135 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3136 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3137 | // If a high part was specified, merge it together with the low part. It is |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3138 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3139 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3140 | if (HighPart) |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3141 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3142 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3143 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3146 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3147 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3148 | bool isNamedArg) |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3149 | const |
| 3150 | { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3151 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3152 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3153 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3154 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3155 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3156 | // Check some invariants. |
| 3157 | // FIXME: Enforce these by construction. |
| 3158 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3159 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3160 | |
| 3161 | neededInt = 0; |
| 3162 | neededSSE = 0; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3163 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3164 | switch (Lo) { |
| 3165 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3166 | if (Hi == NoClass) |
| 3167 | return ABIArgInfo::getIgnore(); |
| 3168 | // If the low part is just padding, it takes no register, leave ResType |
| 3169 | // null. |
| 3170 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3171 | "Unknown missing lo part"); |
| 3172 | break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3173 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3174 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3175 | // on the stack. |
| 3176 | case Memory: |
| 3177 | |
| 3178 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3179 | // COMPLEX_X87, it is passed in memory. |
| 3180 | case X87: |
| 3181 | case ComplexX87: |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3182 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | ded137f | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3183 | ++neededInt; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3184 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3185 | |
| 3186 | case SSEUp: |
| 3187 | case X87Up: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3188 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3189 | |
| 3190 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3191 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3192 | // and %r9 is used. |
| 3193 | case Integer: |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3194 | ++neededInt; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3195 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3196 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3197 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3198 | |
| 3199 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3200 | // so that the parameter gets the right LLVM IR attributes. |
| 3201 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3202 | // Treat an enum type as its underlying type. |
| 3203 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3204 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3205 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3206 | if (Ty->isIntegralOrEnumerationType() && |
| 3207 | Ty->isPromotableIntegerType()) |
| 3208 | return ABIArgInfo::getExtend(); |
| 3209 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3210 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3211 | break; |
| 3212 | |
| 3213 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3214 | // available SSE register is used, the registers are taken in the |
| 3215 | // order from %xmm0 to %xmm7. |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3216 | case SSE: { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3217 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 14508ff | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3218 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3219 | ++neededSSE; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3220 | break; |
| 3221 | } |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3222 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3223 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3224 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3225 | switch (Hi) { |
| 3226 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3227 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3228 | // which is passed in memory. |
| 3229 | case Memory: |
| 3230 | case X87: |
| 3231 | case ComplexX87: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3232 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3233 | |
| 3234 | case NoClass: break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3235 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3236 | case Integer: |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3237 | ++neededInt; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3238 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3239 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3240 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3241 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3242 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3243 | break; |
| 3244 | |
| 3245 | // X87Up generally doesn't occur here (long double is passed in |
| 3246 | // memory), except in situations involving unions. |
| 3247 | case X87Up: |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3248 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3249 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3250 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3251 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3252 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3253 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3254 | ++neededSSE; |
| 3255 | break; |
| 3256 | |
| 3257 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3258 | // eightbyte is passed in the upper half of the last used SSE |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3259 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3260 | case SSEUp: |
Chris Lattner | ab5722e | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3261 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3262 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3263 | break; |
| 3264 | } |
| 3265 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3266 | // If a high part was specified, merge it together with the low part. It is |
| 3267 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3268 | // first class struct aggregate with the high and low part: {low, high} |
| 3269 | if (HighPart) |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3270 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3271 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3272 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3275 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3276 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3277 | if (!getCXXABI().classifyReturnType(FI)) |
| 3278 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3279 | |
| 3280 | // Keep track of the number of assigned registers. |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3281 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3282 | |
| 3283 | // If the return value is indirect, then the hidden argument is consuming one |
| 3284 | // integer register. |
| 3285 | if (FI.getReturnInfo().isIndirect()) |
| 3286 | --freeIntRegs; |
| 3287 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3288 | // The chain argument effectively gives us another free register. |
| 3289 | if (FI.isChainCall()) |
| 3290 | ++freeIntRegs; |
| 3291 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3292 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3293 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3294 | // get assigned (in left-to-right order) for passing as follows... |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3295 | unsigned ArgNo = 0; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3296 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3297 | it != ie; ++it, ++ArgNo) { |
| 3298 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3299 | |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3300 | unsigned neededInt, neededSSE; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3301 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3302 | neededSSE, IsNamedArg); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3303 | |
| 3304 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3305 | // eightbyte of an argument, the whole argument is passed on the |
| 3306 | // stack. If registers have already been assigned for some |
| 3307 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3308 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3309 | freeIntRegs -= neededInt; |
| 3310 | freeSSERegs -= neededSSE; |
| 3311 | } else { |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3312 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3313 | } |
| 3314 | } |
| 3315 | } |
| 3316 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3317 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3318 | Address VAListAddr, QualType Ty) { |
| 3319 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3320 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3321 | llvm::Value *overflow_arg_area = |
| 3322 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3323 | |
| 3324 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3325 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3326 | // It isn't stated explicitly in the standard, but in practice we use |
| 3327 | // alignment greater than 16 where necessary. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3328 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3329 | if (Align > CharUnits::fromQuantity(8)) { |
| 3330 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3331 | Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3335 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3336 | llvm::Value *Res = |
| 3337 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3338 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3339 | |
| 3340 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3341 | // l->overflow_arg_area + sizeof(type). |
| 3342 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3343 | // an 8 byte boundary. |
| 3344 | |
| 3345 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3346 | llvm::Value *Offset = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3347 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3348 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3349 | "overflow_arg_area.next"); |
| 3350 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3351 | |
| 3352 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3353 | return Address(Res, Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3356 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3357 | QualType Ty) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3358 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3359 | // struct { |
| 3360 | // i32 gp_offset; |
| 3361 | // i32 fp_offset; |
| 3362 | // i8* overflow_arg_area; |
| 3363 | // i8* reg_save_area; |
| 3364 | // }; |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3365 | unsigned neededInt, neededSSE; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3366 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3367 | Ty = getContext().getCanonicalType(Ty); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 3368 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 7a1b586 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3369 | /*isNamedArg*/false); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3370 | |
| 3371 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3372 | // in the registers. If not go to step 7. |
| 3373 | if (!neededInt && !neededSSE) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3374 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3375 | |
| 3376 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3377 | // general purpose registers needed to pass type and num_fp to hold |
| 3378 | // the number of floating point registers needed. |
| 3379 | |
| 3380 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3381 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3382 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3383 | // |
| 3384 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3385 | // register save space). |
| 3386 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3387 | llvm::Value *InRegs = nullptr; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3388 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3389 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3390 | if (neededInt) { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 3391 | gp_offset_p = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3392 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3393 | "gp_offset_p"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3394 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3395 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3396 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | if (neededSSE) { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 3400 | fp_offset_p = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3401 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3402 | "fp_offset_p"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3403 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3404 | llvm::Value *FitsInFP = |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3405 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3406 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3407 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3408 | } |
| 3409 | |
| 3410 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3411 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3412 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3413 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3414 | |
| 3415 | // Emit code to load the value if it was passed in registers. |
| 3416 | |
| 3417 | CGF.EmitBlock(InRegBlock); |
| 3418 | |
| 3419 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3420 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3421 | // copying to a temporary location in case the parameter is passed |
| 3422 | // in different register classes or requires an alignment greater |
| 3423 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3424 | // |
| 3425 | // FIXME: This really results in shameful code when we end up needing to |
| 3426 | // collect arguments from different places; often what should result in a |
| 3427 | // simple assembling of a structure from scattered addresses has many more |
| 3428 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3429 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3430 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3431 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3432 | "reg_save_area"); |
| 3433 | |
| 3434 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3435 | if (neededInt && neededSSE) { |
| 3436 | // FIXME: Cleanup. |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3437 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3438 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3439 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3440 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3441 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3442 | llvm::Type *TyLo = ST->getElementType(0); |
| 3443 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | a8b7a7d | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3444 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3445 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3446 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3447 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3448 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3449 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3450 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3451 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3452 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3453 | // Copy the first element. |
| 3454 | llvm::Value *V = |
| 3455 | CGF.Builder.CreateDefaultAlignedLoad( |
| 3456 | CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 3457 | CGF.Builder.CreateStore(V, |
| 3458 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3459 | |
| 3460 | // Copy the second element. |
| 3461 | V = CGF.Builder.CreateDefaultAlignedLoad( |
| 3462 | CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 3463 | CharUnits Offset = CharUnits::fromQuantity( |
| 3464 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3465 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3466 | |
| 3467 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3468 | } else if (neededInt) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3469 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3470 | CharUnits::fromQuantity(8)); |
| 3471 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | eeb0062 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3472 | |
| 3473 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3474 | std::pair<CharUnits, CharUnits> SizeAlign = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3475 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | eeb0062 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3476 | uint64_t TySize = SizeAlign.first.getQuantity(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3477 | CharUnits TyAlign = SizeAlign.second; |
| 3478 | |
| 3479 | // Copy into a temporary if the type is more aligned than the |
| 3480 | // register save area. |
| 3481 | if (TyAlign.getQuantity() > 8) { |
| 3482 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3483 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | eeb0062 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3484 | RegAddr = Tmp; |
| 3485 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3486 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3487 | } else if (neededSSE == 1) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3488 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3489 | CharUnits::fromQuantity(16)); |
| 3490 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3491 | } else { |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3492 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3493 | // SSE registers are spaced 16 bytes apart in the register save |
| 3494 | // area, we need to collect the two eightbytes together. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3495 | // The ABI isn't explicit about this, but it seems reasonable |
| 3496 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3497 | // naturally 16-byte aligned and the prologue is expected to store |
| 3498 | // all the SSE registers to the RSA. |
| 3499 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3500 | CharUnits::fromQuantity(16)); |
| 3501 | Address RegAddrHi = |
| 3502 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3503 | CharUnits::fromQuantity(16)); |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3504 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3505 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3506 | llvm::Value *V; |
| 3507 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3508 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3509 | V = CGF.Builder.CreateLoad( |
| 3510 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3511 | CGF.Builder.CreateStore(V, |
| 3512 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3513 | V = CGF.Builder.CreateLoad( |
| 3514 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3515 | CGF.Builder.CreateStore(V, |
| 3516 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3517 | |
| 3518 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3522 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3523 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3524 | if (neededInt) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3525 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3526 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3527 | gp_offset_p); |
| 3528 | } |
| 3529 | if (neededSSE) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3530 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3531 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3532 | fp_offset_p); |
| 3533 | } |
| 3534 | CGF.EmitBranch(ContBlock); |
| 3535 | |
| 3536 | // Emit code to load the value if it was passed in memory. |
| 3537 | |
| 3538 | CGF.EmitBlock(InMemBlock); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3539 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3540 | |
| 3541 | // Return the appropriate result. |
| 3542 | |
| 3543 | CGF.EmitBlock(ContBlock); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3544 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3545 | "vaarg.addr"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3546 | return ResAddr; |
| 3547 | } |
| 3548 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3549 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3550 | QualType Ty) const { |
| 3551 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3552 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3553 | CharUnits::fromQuantity(8), |
| 3554 | /*allowHigherAlign*/ false); |
| 3555 | } |
| 3556 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3557 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 3558 | bool IsReturnType) const { |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3559 | |
| 3560 | if (Ty->isVoidType()) |
| 3561 | return ABIArgInfo::getIgnore(); |
| 3562 | |
| 3563 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3564 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3565 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3566 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3567 | uint64_t Width = Info.Width; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3568 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3569 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3570 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3571 | if (RT) { |
| 3572 | if (!IsReturnType) { |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3573 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3574 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3578 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3579 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3580 | } |
NAKAMURA Takumi | 6f17433 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3581 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3582 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3583 | // other targets. |
| 3584 | const Type *Base = nullptr; |
| 3585 | uint64_t NumElts = 0; |
| 3586 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3587 | if (FreeSSERegs >= NumElts) { |
| 3588 | FreeSSERegs -= NumElts; |
| 3589 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3590 | return ABIArgInfo::getDirect(); |
| 3591 | return ABIArgInfo::getExpand(); |
| 3592 | } |
| 3593 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3594 | } |
| 3595 | |
| 3596 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3597 | if (Ty->isMemberPointerType()) { |
| 3598 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3599 | // directly. |
| 3600 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3601 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3602 | return ABIArgInfo::getDirect(); |
| 3603 | } |
| 3604 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3605 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | 6f17433 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3606 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3607 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3608 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3609 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3610 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3611 | // Otherwise, coerce it to a small integer. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3612 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3615 | // Bool type is always extended to the ABI, other builtin types are not |
| 3616 | // extended. |
| 3617 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3618 | if (BT && BT->getKind() == BuiltinType::Bool) |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3619 | return ABIArgInfo::getExtend(); |
| 3620 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3621 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3622 | // passes them indirectly through memory. |
| 3623 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3624 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 3625 | if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 3626 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3627 | } |
| 3628 | |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3629 | return ABIArgInfo::getDirect(); |
| 3630 | } |
| 3631 | |
| 3632 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3633 | bool IsVectorCall = |
| 3634 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3635 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3636 | // We can use up to 4 SSE return registers with vectorcall. |
| 3637 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3638 | if (!getCXXABI().classifyReturnType(FI)) |
| 3639 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3640 | |
| 3641 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3642 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3643 | for (auto &I : FI.arguments()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3644 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3647 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3648 | QualType Ty) const { |
| 3649 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3650 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3651 | CharUnits::fromQuantity(8), |
| 3652 | /*allowHigherAlign*/ false); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3653 | } |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3654 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3655 | // PowerPC-32 |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3656 | namespace { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3657 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3658 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3659 | bool IsSoftFloatABI; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3660 | public: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3661 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 3662 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3663 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3664 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3665 | QualType Ty) const override; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3666 | }; |
| 3667 | |
| 3668 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3669 | public: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3670 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 3671 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3672 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3673 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3674 | // This is recovered from gcc output. |
| 3675 | return 1; // r1 is the dedicated stack pointer |
| 3676 | } |
| 3677 | |
| 3678 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3679 | llvm::Value *Address) const override; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3680 | }; |
| 3681 | |
| 3682 | } |
| 3683 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 3684 | // TODO: this implementation is now likely redundant with |
| 3685 | // DefaultABIInfo::EmitVAArg. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3686 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 3687 | QualType Ty) const { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 3688 | const unsigned OverflowLimit = 8; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3689 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3690 | // TODO: Implement this. For now ignore. |
| 3691 | (void)CTy; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 3692 | return Address::invalid(); // FIXME? |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3693 | } |
| 3694 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3695 | // struct __va_list_tag { |
| 3696 | // unsigned char gpr; |
| 3697 | // unsigned char fpr; |
| 3698 | // unsigned short reserved; |
| 3699 | // void *overflow_arg_area; |
| 3700 | // void *reg_save_area; |
| 3701 | // }; |
| 3702 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3703 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 3704 | bool isInt = |
| 3705 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3706 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
| 3707 | |
| 3708 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 3709 | // with the argument-lowering code. |
| 3710 | bool isIndirect = Ty->isAggregateType(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3711 | |
| 3712 | CGBuilderTy &Builder = CGF.Builder; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3713 | |
| 3714 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 3715 | Address NumRegsAddr = Address::invalid(); |
| 3716 | if (isInt || IsSoftFloatABI) { |
| 3717 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 3718 | } else { |
| 3719 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3720 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3721 | |
| 3722 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 3723 | |
| 3724 | // "Align" the register count when TY is i64. |
| 3725 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
| 3726 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 3727 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 3728 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3729 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 3730 | llvm::Value *CC = |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 3731 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3732 | |
| 3733 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3734 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3735 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3736 | |
| 3737 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3738 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3739 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 3740 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3741 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3742 | // Case 1: consume registers. |
| 3743 | Address RegAddr = Address::invalid(); |
| 3744 | { |
| 3745 | CGF.EmitBlock(UsingRegs); |
| 3746 | |
| 3747 | Address RegSaveAreaPtr = |
| 3748 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 3749 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 3750 | CharUnits::fromQuantity(8)); |
| 3751 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 3752 | |
| 3753 | // Floating-point registers start after the general-purpose registers. |
| 3754 | if (!(isInt || IsSoftFloatABI)) { |
| 3755 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 3756 | CharUnits::fromQuantity(32)); |
| 3757 | } |
| 3758 | |
| 3759 | // Get the address of the saved value by scaling the number of |
| 3760 | // registers we've used by the number of |
| 3761 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
| 3762 | llvm::Value *RegOffset = |
| 3763 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 3764 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 3765 | RegAddr.getPointer(), RegOffset), |
| 3766 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 3767 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 3768 | |
| 3769 | // Increase the used-register count. |
| 3770 | NumRegs = |
| 3771 | Builder.CreateAdd(NumRegs, |
| 3772 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
| 3773 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 3774 | |
| 3775 | CGF.EmitBranch(Cont); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3776 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3777 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3778 | // Case 2: consume space in the overflow area. |
| 3779 | Address MemAddr = Address::invalid(); |
| 3780 | { |
| 3781 | CGF.EmitBlock(UsingOverflow); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3782 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 3783 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 3784 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3785 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 3786 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 3787 | |
| 3788 | CharUnits Size; |
| 3789 | if (!isIndirect) { |
| 3790 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 3791 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3792 | } else { |
| 3793 | Size = CGF.getPointerSize(); |
| 3794 | } |
| 3795 | |
| 3796 | Address OverflowAreaAddr = |
| 3797 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
| 3798 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
| 3799 | OverflowAreaAlign); |
| 3800 | // Round up address of argument to alignment |
| 3801 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3802 | if (Align > OverflowAreaAlign) { |
| 3803 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 3804 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 3805 | Align); |
| 3806 | } |
| 3807 | |
| 3808 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 3809 | |
| 3810 | // Increase the overflow area. |
| 3811 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 3812 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 3813 | CGF.EmitBranch(Cont); |
| 3814 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3815 | |
| 3816 | CGF.EmitBlock(Cont); |
| 3817 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3818 | // Merge the cases with a phi. |
| 3819 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 3820 | "vaarg.addr"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3821 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3822 | // Load the pointer if the argument was passed indirectly. |
| 3823 | if (isIndirect) { |
| 3824 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 3825 | getContext().getTypeAlignInChars(Ty)); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | return Result; |
| 3829 | } |
| 3830 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3831 | bool |
| 3832 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3833 | llvm::Value *Address) const { |
| 3834 | // This is calculated from the LLVM and GCC tables and verified |
| 3835 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3836 | |
| 3837 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3838 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3839 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3840 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3841 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3842 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3843 | |
| 3844 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3845 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3846 | |
| 3847 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3848 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3849 | |
| 3850 | // 64-76 are various 4-byte special-purpose registers: |
| 3851 | // 64: mq |
| 3852 | // 65: lr |
| 3853 | // 66: ctr |
| 3854 | // 67: ap |
| 3855 | // 68-75 cr0-7 |
| 3856 | // 76: xer |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3857 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3858 | |
| 3859 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3860 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3861 | |
| 3862 | // 109: vrsave |
| 3863 | // 110: vscr |
| 3864 | // 111: spe_acc |
| 3865 | // 112: spefscr |
| 3866 | // 113: sfp |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3867 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3868 | |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3869 | return false; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3872 | // PowerPC-64 |
| 3873 | |
| 3874 | namespace { |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3875 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 3876 | class PPC64_SVR4_ABIInfo : public ABIInfo { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3877 | public: |
| 3878 | enum ABIKind { |
| 3879 | ELFv1 = 0, |
| 3880 | ELFv2 |
| 3881 | }; |
| 3882 | |
| 3883 | private: |
| 3884 | static const unsigned GPRBits = 64; |
| 3885 | ABIKind Kind; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 3886 | bool HasQPX; |
| 3887 | |
| 3888 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3889 | // will be passed in a QPX register. |
| 3890 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3891 | if (!HasQPX) |
| 3892 | return false; |
| 3893 | |
| 3894 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3895 | unsigned NumElements = VT->getNumElements(); |
| 3896 | if (NumElements == 1) |
| 3897 | return false; |
| 3898 | |
| 3899 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3900 | if (getContext().getTypeSize(Ty) <= 256) |
| 3901 | return true; |
| 3902 | } else if (VT->getElementType()-> |
| 3903 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3904 | if (getContext().getTypeSize(Ty) <= 128) |
| 3905 | return true; |
| 3906 | } |
| 3907 | } |
| 3908 | |
| 3909 | return false; |
| 3910 | } |
| 3911 | |
| 3912 | bool IsQPXVectorTy(QualType Ty) const { |
| 3913 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3914 | } |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3915 | |
| 3916 | public: |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 3917 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 3918 | : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3919 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3920 | bool isPromotableTypeForABI(QualType Ty) const; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3921 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3922 | |
| 3923 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3924 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3925 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3926 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3927 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3928 | uint64_t Members) const override; |
| 3929 | |
Bill Schmidt | b1f5fe0 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3930 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3931 | // Example: For aggregate arguments that fit in a register, we could |
| 3932 | // use getDirectInReg (as is done below for structs containing a single |
| 3933 | // floating-point value) to avoid pushing them to memory on function |
| 3934 | // entry. This would require changing the logic in PPCISelLowering |
| 3935 | // when lowering the parameters in the caller and args in the callee. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3936 | void computeInfo(CGFunctionInfo &FI) const override { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3937 | if (!getCXXABI().classifyReturnType(FI)) |
| 3938 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3939 | for (auto &I : FI.arguments()) { |
Bill Schmidt | b1f5fe0 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3940 | // We rely on the default argument classification for the most part. |
| 3941 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | b199310 | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3942 | // or vector item must be passed in a register if one is available. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3943 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | b1f5fe0 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3944 | if (T) { |
| 3945 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 3946 | if (IsQPXVectorTy(T) || |
| 3947 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3948 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | b1f5fe0 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3949 | QualType QT(T, 0); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3950 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | b1f5fe0 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3951 | continue; |
| 3952 | } |
| 3953 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3954 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | b1f5fe0 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3955 | } |
| 3956 | } |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3957 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3958 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3959 | QualType Ty) const override; |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3960 | }; |
| 3961 | |
| 3962 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 3963 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3964 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3965 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 3966 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 3967 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {} |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3968 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3969 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3970 | // This is recovered from gcc output. |
| 3971 | return 1; // r1 is the dedicated stack pointer |
| 3972 | } |
| 3973 | |
| 3974 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3975 | llvm::Value *Address) const override; |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3976 | }; |
| 3977 | |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3978 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3979 | public: |
| 3980 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3981 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3982 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3983 | // This is recovered from gcc output. |
| 3984 | return 1; // r1 is the dedicated stack pointer |
| 3985 | } |
| 3986 | |
| 3987 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3988 | llvm::Value *Address) const override; |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3989 | }; |
| 3990 | |
| 3991 | } |
| 3992 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3993 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3994 | // extended to 64 bits. |
| 3995 | bool |
| 3996 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3997 | // Treat an enum type as its underlying type. |
| 3998 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3999 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4000 | |
| 4001 | // Promotable integer types are required to be promoted by the ABI. |
| 4002 | if (Ty->isPromotableIntegerType()) |
| 4003 | return true; |
| 4004 | |
| 4005 | // In addition to the usual promotable integer types, we also need to |
| 4006 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4007 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4008 | switch (BT->getKind()) { |
| 4009 | case BuiltinType::Int: |
| 4010 | case BuiltinType::UInt: |
| 4011 | return true; |
| 4012 | default: |
| 4013 | break; |
| 4014 | } |
| 4015 | |
| 4016 | return false; |
| 4017 | } |
| 4018 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4019 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4020 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4021 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4022 | // Complex types are passed just like their elements. |
| 4023 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4024 | Ty = CTy->getElementType(); |
| 4025 | |
| 4026 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4027 | // passed via reference, smaller types are not aligned). |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4028 | if (IsQPXVectorTy(Ty)) { |
| 4029 | if (getContext().getTypeSize(Ty) > 128) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4030 | return CharUnits::fromQuantity(32); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4031 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4032 | return CharUnits::fromQuantity(16); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4033 | } else if (Ty->isVectorType()) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4034 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4035 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4036 | |
| 4037 | // For single-element float/vector structs, we consider the whole type |
| 4038 | // to have the same alignment requirements as its single element. |
| 4039 | const Type *AlignAsType = nullptr; |
| 4040 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4041 | if (EltType) { |
| 4042 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4043 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4044 | getContext().getTypeSize(EltType) == 128) || |
| 4045 | (BT && BT->isFloatingPoint())) |
| 4046 | AlignAsType = EltType; |
| 4047 | } |
| 4048 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4049 | // Likewise for ELFv2 homogeneous aggregates. |
| 4050 | const Type *Base = nullptr; |
| 4051 | uint64_t Members = 0; |
| 4052 | if (!AlignAsType && Kind == ELFv2 && |
| 4053 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4054 | AlignAsType = Base; |
| 4055 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4056 | // With special case aggregates, only vector base types need alignment. |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4057 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4058 | if (getContext().getTypeSize(AlignAsType) > 128) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4059 | return CharUnits::fromQuantity(32); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4060 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4061 | return CharUnits::fromQuantity(16); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4062 | } else if (AlignAsType) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4063 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4064 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4065 | |
| 4066 | // Otherwise, we only need alignment for any aggregate type that |
| 4067 | // has an alignment requirement of >= 16 bytes. |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4068 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4069 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4070 | return CharUnits::fromQuantity(32); |
| 4071 | return CharUnits::fromQuantity(16); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4072 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4073 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4074 | return CharUnits::fromQuantity(8); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4075 | } |
| 4076 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4077 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4078 | /// aggregate. Base is set to the base element type, and Members is set |
| 4079 | /// to the number of base elements. |
| 4080 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4081 | uint64_t &Members) const { |
| 4082 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4083 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4084 | if (NElements == 0) |
| 4085 | return false; |
| 4086 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4087 | return false; |
| 4088 | Members *= NElements; |
| 4089 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4090 | const RecordDecl *RD = RT->getDecl(); |
| 4091 | if (RD->hasFlexibleArrayMember()) |
| 4092 | return false; |
| 4093 | |
| 4094 | Members = 0; |
| 4095 | |
| 4096 | // If this is a C++ record, check the bases first. |
| 4097 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4098 | for (const auto &I : CXXRD->bases()) { |
| 4099 | // Ignore empty records. |
| 4100 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4101 | continue; |
| 4102 | |
| 4103 | uint64_t FldMembers; |
| 4104 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4105 | return false; |
| 4106 | |
| 4107 | Members += FldMembers; |
| 4108 | } |
| 4109 | } |
| 4110 | |
| 4111 | for (const auto *FD : RD->fields()) { |
| 4112 | // Ignore (non-zero arrays of) empty records. |
| 4113 | QualType FT = FD->getType(); |
| 4114 | while (const ConstantArrayType *AT = |
| 4115 | getContext().getAsConstantArrayType(FT)) { |
| 4116 | if (AT->getSize().getZExtValue() == 0) |
| 4117 | return false; |
| 4118 | FT = AT->getElementType(); |
| 4119 | } |
| 4120 | if (isEmptyRecord(getContext(), FT, true)) |
| 4121 | continue; |
| 4122 | |
| 4123 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4124 | if (getContext().getLangOpts().CPlusPlus && |
| 4125 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 4126 | continue; |
| 4127 | |
| 4128 | uint64_t FldMembers; |
| 4129 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4130 | return false; |
| 4131 | |
| 4132 | Members = (RD->isUnion() ? |
| 4133 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4134 | } |
| 4135 | |
| 4136 | if (!Base) |
| 4137 | return false; |
| 4138 | |
| 4139 | // Ensure there is no padding. |
| 4140 | if (getContext().getTypeSize(Base) * Members != |
| 4141 | getContext().getTypeSize(Ty)) |
| 4142 | return false; |
| 4143 | } else { |
| 4144 | Members = 1; |
| 4145 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4146 | Members = 2; |
| 4147 | Ty = CT->getElementType(); |
| 4148 | } |
| 4149 | |
| 4150 | // Most ABIs only support float, double, and some vector type widths. |
| 4151 | if (!isHomogeneousAggregateBaseType(Ty)) |
| 4152 | return false; |
| 4153 | |
| 4154 | // The base type must be the same for all members. Types that |
| 4155 | // agree in both total size and mode (float vs. vector) are |
| 4156 | // treated as being equivalent here. |
| 4157 | const Type *TyPtr = Ty.getTypePtr(); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4158 | if (!Base) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4159 | Base = TyPtr; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4160 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4161 | // so make sure to widen it explicitly. |
| 4162 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4163 | QualType EltTy = VT->getElementType(); |
| 4164 | unsigned NumElements = |
| 4165 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4166 | Base = getContext() |
| 4167 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4168 | .getTypePtr(); |
| 4169 | } |
| 4170 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4171 | |
| 4172 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4173 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4174 | return false; |
| 4175 | } |
| 4176 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4177 | } |
| 4178 | |
| 4179 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4180 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4181 | // double, long double, or 128-bit vectors. |
| 4182 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4183 | if (BT->getKind() == BuiltinType::Float || |
| 4184 | BT->getKind() == BuiltinType::Double || |
| 4185 | BT->getKind() == BuiltinType::LongDouble) |
| 4186 | return true; |
| 4187 | } |
| 4188 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4189 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4190 | return true; |
| 4191 | } |
| 4192 | return false; |
| 4193 | } |
| 4194 | |
| 4195 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4196 | const Type *Base, uint64_t Members) const { |
| 4197 | // Vector types require one register, floating point types require one |
| 4198 | // or two registers depending on their size. |
| 4199 | uint32_t NumRegs = |
| 4200 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
| 4201 | |
| 4202 | // Homogeneous Aggregates may occupy at most 8 registers. |
| 4203 | return Members * NumRegs <= 8; |
| 4204 | } |
| 4205 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4206 | ABIArgInfo |
| 4207 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4208 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4209 | |
Bill Schmidt | c9715fc | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4210 | if (Ty->isAnyComplexType()) |
| 4211 | return ABIArgInfo::getDirect(); |
| 4212 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4213 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4214 | // or via reference (larger than 16 bytes). |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4215 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4216 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4217 | if (Size > 128) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4218 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4219 | else if (Size < 128) { |
| 4220 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4221 | return ABIArgInfo::getDirect(CoerceTy); |
| 4222 | } |
| 4223 | } |
| 4224 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4225 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4226 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4227 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4228 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4229 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4230 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4231 | |
| 4232 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4233 | const Type *Base = nullptr; |
| 4234 | uint64_t Members = 0; |
| 4235 | if (Kind == ELFv2 && |
| 4236 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4237 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4238 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4239 | return ABIArgInfo::getDirect(CoerceTy); |
| 4240 | } |
| 4241 | |
| 4242 | // If an aggregate may end up fully in registers, we do not |
| 4243 | // use the ByVal method, but pass the aggregate as array. |
| 4244 | // This is usually beneficial since we avoid forcing the |
| 4245 | // back-end to store the argument to memory. |
| 4246 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4247 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4248 | llvm::Type *CoerceTy; |
| 4249 | |
| 4250 | // Types up to 8 bytes are passed as integer type (which will be |
| 4251 | // properly aligned in the argument save area doubleword). |
| 4252 | if (Bits <= GPRBits) |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4253 | CoerceTy = |
| 4254 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4255 | // Larger types are passed as arrays, with the base type selected |
| 4256 | // according to the required alignment in the save area. |
| 4257 | else { |
| 4258 | uint64_t RegBits = ABIAlign * 8; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4259 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4260 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4261 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4262 | } |
| 4263 | |
| 4264 | return ABIArgInfo::getDirect(CoerceTy); |
| 4265 | } |
| 4266 | |
| 4267 | // All other aggregates are passed ByVal. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4268 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4269 | /*ByVal=*/true, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4270 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
| 4273 | return (isPromotableTypeForABI(Ty) ? |
| 4274 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4275 | } |
| 4276 | |
| 4277 | ABIArgInfo |
| 4278 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4279 | if (RetTy->isVoidType()) |
| 4280 | return ABIArgInfo::getIgnore(); |
| 4281 | |
Bill Schmidt | 9e6111a | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4282 | if (RetTy->isAnyComplexType()) |
| 4283 | return ABIArgInfo::getDirect(); |
| 4284 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4285 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4286 | // or via reference (larger than 16 bytes). |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 4287 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4288 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4289 | if (Size > 128) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4290 | return getNaturalAlignIndirect(RetTy); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4291 | else if (Size < 128) { |
| 4292 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4293 | return ABIArgInfo::getDirect(CoerceTy); |
| 4294 | } |
| 4295 | } |
| 4296 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4297 | if (isAggregateTypeForABI(RetTy)) { |
| 4298 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4299 | const Type *Base = nullptr; |
| 4300 | uint64_t Members = 0; |
| 4301 | if (Kind == ELFv2 && |
| 4302 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4303 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4304 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4305 | return ABIArgInfo::getDirect(CoerceTy); |
| 4306 | } |
| 4307 | |
| 4308 | // ELFv2 small aggregates are returned in up to two registers. |
| 4309 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4310 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4311 | if (Bits == 0) |
| 4312 | return ABIArgInfo::getIgnore(); |
| 4313 | |
| 4314 | llvm::Type *CoerceTy; |
| 4315 | if (Bits > GPRBits) { |
| 4316 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4317 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4318 | } else |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4319 | CoerceTy = |
| 4320 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4321 | return ABIArgInfo::getDirect(CoerceTy); |
| 4322 | } |
| 4323 | |
| 4324 | // All other aggregates are returned indirectly. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4325 | return getNaturalAlignIndirect(RetTy); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4326 | } |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4327 | |
| 4328 | return (isPromotableTypeForABI(RetTy) ? |
| 4329 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4330 | } |
| 4331 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4332 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4333 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4334 | QualType Ty) const { |
| 4335 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4336 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4337 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4338 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4339 | |
Bill Schmidt | 19f8e85 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4340 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4341 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4342 | // in separate doublewords. However, Clang expects us to produce a |
| 4343 | // pointer to a structure with the two parts packed tightly. So generate |
| 4344 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4345 | // and store them to a temporary structure. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4346 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4347 | CharUnits EltSize = TypeInfo.first / 2; |
| 4348 | if (EltSize < SlotSize) { |
| 4349 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4350 | SlotSize * 2, SlotSize, |
| 4351 | SlotSize, /*AllowHigher*/ true); |
| 4352 | |
| 4353 | Address RealAddr = Addr; |
| 4354 | Address ImagAddr = RealAddr; |
| 4355 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4356 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4357 | SlotSize - EltSize); |
| 4358 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4359 | 2 * SlotSize - EltSize); |
| 4360 | } else { |
| 4361 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4362 | } |
| 4363 | |
| 4364 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4365 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4366 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4367 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4368 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4369 | |
| 4370 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4371 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4372 | /*init*/ true); |
| 4373 | return Temp; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4374 | } |
Bill Schmidt | 19f8e85 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4377 | // Otherwise, just use the general rule. |
| 4378 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4379 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | static bool |
| 4383 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4384 | llvm::Value *Address) { |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4385 | // This is calculated from the LLVM and GCC tables and verified |
| 4386 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4387 | |
| 4388 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4389 | |
| 4390 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4391 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4392 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4393 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4394 | |
| 4395 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4396 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4397 | |
| 4398 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4399 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4400 | |
| 4401 | // 64-76 are various 4-byte special-purpose registers: |
| 4402 | // 64: mq |
| 4403 | // 65: lr |
| 4404 | // 66: ctr |
| 4405 | // 67: ap |
| 4406 | // 68-75 cr0-7 |
| 4407 | // 76: xer |
| 4408 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 4409 | |
| 4410 | // 77-108: v0-31, the 16-byte vector registers |
| 4411 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4412 | |
| 4413 | // 109: vrsave |
| 4414 | // 110: vscr |
| 4415 | // 111: spe_acc |
| 4416 | // 112: spefscr |
| 4417 | // 113: sfp |
| 4418 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 4419 | |
| 4420 | return false; |
| 4421 | } |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4422 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4423 | bool |
| 4424 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4425 | CodeGen::CodeGenFunction &CGF, |
| 4426 | llvm::Value *Address) const { |
| 4427 | |
| 4428 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4429 | } |
| 4430 | |
| 4431 | bool |
| 4432 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4433 | llvm::Value *Address) const { |
| 4434 | |
| 4435 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4436 | } |
| 4437 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4438 | //===----------------------------------------------------------------------===// |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4439 | // AArch64 ABI Implementation |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4440 | //===----------------------------------------------------------------------===// |
| 4441 | |
| 4442 | namespace { |
| 4443 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4444 | class AArch64ABIInfo : public SwiftABIInfo { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4445 | public: |
| 4446 | enum ABIKind { |
| 4447 | AAPCS = 0, |
| 4448 | DarwinPCS |
| 4449 | }; |
| 4450 | |
| 4451 | private: |
| 4452 | ABIKind Kind; |
| 4453 | |
| 4454 | public: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4455 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4456 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4457 | |
| 4458 | private: |
| 4459 | ABIKind getABIKind() const { return Kind; } |
| 4460 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4461 | |
| 4462 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4463 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4464 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4465 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4466 | uint64_t Members) const override; |
| 4467 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4468 | bool isIllegalVectorType(QualType Ty) const; |
| 4469 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4470 | void computeInfo(CGFunctionInfo &FI) const override { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4471 | if (!getCXXABI().classifyReturnType(FI)) |
| 4472 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4473 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4474 | for (auto &it : FI.arguments()) |
| 4475 | it.info = classifyArgumentType(it.type); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4476 | } |
| 4477 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4478 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4479 | CodeGenFunction &CGF) const; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4480 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4481 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4482 | CodeGenFunction &CGF) const; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4483 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4484 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4485 | QualType Ty) const override { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4486 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4487 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 4488 | } |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4489 | |
| 4490 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 4491 | ArrayRef<llvm::Type*> scalars, |
| 4492 | bool asReturnValue) const override { |
| 4493 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4494 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4495 | }; |
| 4496 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4497 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4498 | public: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4499 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4500 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4501 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 4502 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4503 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 4504 | } |
| 4505 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 4506 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4507 | return 31; |
| 4508 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4509 | |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 4510 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4511 | }; |
| 4512 | } |
| 4513 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4514 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4515 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4516 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4517 | // Handle illegal vector types here. |
| 4518 | if (isIllegalVectorType(Ty)) { |
| 4519 | uint64_t Size = getContext().getTypeSize(Ty); |
Tim Murray | 9212d4f | 2014-08-15 16:00:15 -0700 | [diff] [blame] | 4520 | // Android promotes <2 x i8> to i16, not i32 |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4521 | if (isAndroid() && (Size <= 16)) { |
Tim Murray | 9212d4f | 2014-08-15 16:00:15 -0700 | [diff] [blame] | 4522 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
Tim Murray | 9212d4f | 2014-08-15 16:00:15 -0700 | [diff] [blame] | 4523 | return ABIArgInfo::getDirect(ResType); |
| 4524 | } |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4525 | if (Size <= 32) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4526 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4527 | return ABIArgInfo::getDirect(ResType); |
| 4528 | } |
| 4529 | if (Size == 64) { |
| 4530 | llvm::Type *ResType = |
| 4531 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4532 | return ABIArgInfo::getDirect(ResType); |
| 4533 | } |
| 4534 | if (Size == 128) { |
| 4535 | llvm::Type *ResType = |
| 4536 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4537 | return ABIArgInfo::getDirect(ResType); |
| 4538 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4539 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4540 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4541 | |
| 4542 | if (!isAggregateTypeForABI(Ty)) { |
| 4543 | // Treat an enum type as its underlying type. |
| 4544 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4545 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4546 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4547 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4548 | ? ABIArgInfo::getExtend() |
| 4549 | : ABIArgInfo::getDirect()); |
| 4550 | } |
| 4551 | |
| 4552 | // Structures with either a non-trivial destructor or a non-trivial |
| 4553 | // copy constructor are always indirect. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4554 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4555 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4556 | CGCXXABI::RAA_DirectInMemory); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4560 | // elsewhere for GNU compatibility. |
| 4561 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4562 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4563 | return ABIArgInfo::getIgnore(); |
| 4564 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4565 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4566 | } |
| 4567 | |
| 4568 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4569 | const Type *Base = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4570 | uint64_t Members = 0; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4571 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4572 | return ABIArgInfo::getDirect( |
| 4573 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4577 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4578 | if (Size <= 128) { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4579 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 4580 | // same size and alignment. |
| 4581 | if (getTarget().isRenderScriptTarget()) { |
Matt Wala | 1d15151 | 2015-08-10 15:58:40 -0700 | [diff] [blame] | 4582 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 4583 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4584 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4585 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4586 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4587 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4588 | // For aggregates with 16-byte alignment, we use i128. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4589 | if (Alignment < 128 && Size == 128) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4590 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4591 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4592 | } |
| 4593 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4594 | } |
| 4595 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4596 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4597 | } |
| 4598 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4599 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4600 | if (RetTy->isVoidType()) |
| 4601 | return ABIArgInfo::getIgnore(); |
| 4602 | |
| 4603 | // Large vector types should be returned via memory. |
| 4604 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4605 | return getNaturalAlignIndirect(RetTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4606 | |
| 4607 | if (!isAggregateTypeForABI(RetTy)) { |
| 4608 | // Treat an enum type as its underlying type. |
| 4609 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4610 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4611 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4612 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4613 | ? ABIArgInfo::getExtend() |
| 4614 | : ABIArgInfo::getDirect()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4615 | } |
| 4616 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4617 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4618 | return ABIArgInfo::getIgnore(); |
| 4619 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4620 | const Type *Base = nullptr; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4621 | uint64_t Members = 0; |
| 4622 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4623 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4624 | return ABIArgInfo::getDirect(); |
| 4625 | |
| 4626 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4627 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4628 | if (Size <= 128) { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4629 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 4630 | // same size and alignment. |
| 4631 | if (getTarget().isRenderScriptTarget()) { |
Matt Wala | 1d15151 | 2015-08-10 15:58:40 -0700 | [diff] [blame] | 4632 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 4633 | } |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 4634 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4635 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 4636 | |
| 4637 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4638 | // For aggregates with 16-byte alignment, we use i128. |
| 4639 | if (Alignment < 128 && Size == 128) { |
| 4640 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4641 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4642 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4643 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4644 | } |
| 4645 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4646 | return getNaturalAlignIndirect(RetTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4647 | } |
| 4648 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4649 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4650 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4651 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4652 | // Check whether VT is legal. |
| 4653 | unsigned NumElements = VT->getNumElements(); |
| 4654 | uint64_t Size = getContext().getTypeSize(VT); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4655 | // NumElements should be power of 2. |
| 4656 | if (!llvm::isPowerOf2_32(NumElements)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4657 | return true; |
| 4658 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4659 | } |
| 4660 | return false; |
| 4661 | } |
| 4662 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4663 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4664 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4665 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4666 | // but with the difference that any floating-point type is allowed, |
| 4667 | // including __fp16. |
| 4668 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4669 | if (BT->isFloatingPoint()) |
| 4670 | return true; |
| 4671 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4672 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4673 | if (VecSize == 64 || VecSize == 128) |
| 4674 | return true; |
| 4675 | } |
| 4676 | return false; |
| 4677 | } |
| 4678 | |
| 4679 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4680 | uint64_t Members) const { |
| 4681 | return Members <= 4; |
| 4682 | } |
| 4683 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4684 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4685 | QualType Ty, |
| 4686 | CodeGenFunction &CGF) const { |
| 4687 | ABIArgInfo AI = classifyArgumentType(Ty); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4688 | bool IsIndirect = AI.isIndirect(); |
| 4689 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4690 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4691 | if (IsIndirect) |
| 4692 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4693 | else if (AI.getCoerceToType()) |
| 4694 | BaseTy = AI.getCoerceToType(); |
| 4695 | |
| 4696 | unsigned NumRegs = 1; |
| 4697 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4698 | BaseTy = ArrTy->getElementType(); |
| 4699 | NumRegs = ArrTy->getNumElements(); |
| 4700 | } |
| 4701 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4702 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4703 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4704 | // Standard, section B.4: |
| 4705 | // |
| 4706 | // struct { |
| 4707 | // void *__stack; |
| 4708 | // void *__gr_top; |
| 4709 | // void *__vr_top; |
| 4710 | // int __gr_offs; |
| 4711 | // int __vr_offs; |
| 4712 | // }; |
| 4713 | |
| 4714 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4715 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4716 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4717 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4718 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4719 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4720 | CharUnits TyAlign = TyInfo.second; |
| 4721 | |
| 4722 | Address reg_offs_p = Address::invalid(); |
| 4723 | llvm::Value *reg_offs = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4724 | int reg_top_index; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4725 | CharUnits reg_top_offset; |
| 4726 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4727 | if (!IsFPR) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4728 | // 3 is the field number of __gr_offs |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 4729 | reg_offs_p = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4730 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 4731 | "gr_offs_p"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4732 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4733 | reg_top_index = 1; // field number for __gr_top |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4734 | reg_top_offset = CharUnits::fromQuantity(8); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4735 | RegSize = llvm::alignTo(RegSize, 8); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4736 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4737 | // 4 is the field number of __vr_offs. |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 4738 | reg_offs_p = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4739 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 4740 | "vr_offs_p"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4741 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4742 | reg_top_index = 2; // field number for __vr_top |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4743 | reg_top_offset = CharUnits::fromQuantity(16); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4744 | RegSize = 16 * NumRegs; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | //======================================= |
| 4748 | // Find out where argument was passed |
| 4749 | //======================================= |
| 4750 | |
| 4751 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4752 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4753 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4754 | // whatever they get). |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4755 | llvm::Value *UsingStack = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4756 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4757 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4758 | |
| 4759 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4760 | |
| 4761 | // Otherwise, at least some kind of argument could go in these registers, the |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4762 | // question is whether this particular type is too big. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4763 | CGF.EmitBlock(MaybeRegBlock); |
| 4764 | |
| 4765 | // Integer arguments may need to correct register alignment (for example a |
| 4766 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4767 | // align __gr_offs to calculate the potential address. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4768 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 4769 | int Align = TyAlign.getQuantity(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4770 | |
| 4771 | reg_offs = CGF.Builder.CreateAdd( |
| 4772 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4773 | "align_regoffs"); |
| 4774 | reg_offs = CGF.Builder.CreateAnd( |
| 4775 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4776 | "aligned_regoffs"); |
| 4777 | } |
| 4778 | |
| 4779 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4780 | // The fact that this is done unconditionally reflects the fact that |
| 4781 | // allocating an argument to the stack also uses up all the remaining |
| 4782 | // registers of the appropriate kind. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4783 | llvm::Value *NewOffset = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4784 | NewOffset = CGF.Builder.CreateAdd( |
| 4785 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4786 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4787 | |
| 4788 | // Now we're in a position to decide whether this argument really was in |
| 4789 | // registers or not. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4790 | llvm::Value *InRegs = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4791 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4792 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4793 | |
| 4794 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4795 | |
| 4796 | //======================================= |
| 4797 | // Argument was in registers |
| 4798 | //======================================= |
| 4799 | |
| 4800 | // Now we emit the code for if the argument was originally passed in |
| 4801 | // registers. First start the appropriate block: |
| 4802 | CGF.EmitBlock(InRegBlock); |
| 4803 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4804 | llvm::Value *reg_top = nullptr; |
| 4805 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 4806 | reg_top_offset, "reg_top_p"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4807 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4808 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 4809 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 4810 | Address RegAddr = Address::invalid(); |
| 4811 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4812 | |
| 4813 | if (IsIndirect) { |
| 4814 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4815 | // stored registers or on the stack will actually be a struct **. |
| 4816 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4817 | } |
| 4818 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4819 | const Type *Base = nullptr; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4820 | uint64_t NumMembers = 0; |
| 4821 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4822 | if (IsHFA && NumMembers > 1) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4823 | // Homogeneous aggregates passed in registers will have their elements split |
| 4824 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4825 | // qN+1, ...). We reload and store into a temporary local variable |
| 4826 | // contiguously. |
| 4827 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4828 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4829 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4830 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4831 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 4832 | std::max(TyAlign, BaseTyInfo.second)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4833 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4834 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 4835 | int Offset = 0; |
| 4836 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4837 | BaseTyInfo.first.getQuantity() < 16) |
| 4838 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 4839 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4840 | for (unsigned i = 0; i < NumMembers; ++i) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4841 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 4842 | Address LoadAddr = |
| 4843 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 4844 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 4845 | |
| 4846 | Address StoreAddr = |
| 4847 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4848 | |
| 4849 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4850 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4851 | } |
| 4852 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4853 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4854 | } else { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4855 | // Otherwise the object is contiguous in memory. |
| 4856 | |
| 4857 | // It might be right-aligned in its slot. |
| 4858 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 4859 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4860 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4861 | TyInfo.first < SlotSize) { |
| 4862 | CharUnits Offset = SlotSize - TyInfo.first; |
| 4863 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4864 | } |
| 4865 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4866 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4867 | } |
| 4868 | |
| 4869 | CGF.EmitBranch(ContBlock); |
| 4870 | |
| 4871 | //======================================= |
| 4872 | // Argument was on the stack |
| 4873 | //======================================= |
| 4874 | CGF.EmitBlock(OnStackBlock); |
| 4875 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4876 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 4877 | CharUnits::Zero(), "stack_p"); |
| 4878 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4879 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4880 | // Again, stack arguments may need realignment. In this case both integer and |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4881 | // floating-point ones might be affected. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4882 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 4883 | int Align = TyAlign.getQuantity(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4884 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4885 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4886 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4887 | OnStackPtr = CGF.Builder.CreateAdd( |
| 4888 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4889 | "align_stack"); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4890 | OnStackPtr = CGF.Builder.CreateAnd( |
| 4891 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4892 | "align_stack"); |
| 4893 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4894 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4895 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4896 | Address OnStackAddr(OnStackPtr, |
| 4897 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4898 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4899 | // All stack slots are multiples of 8 bytes. |
| 4900 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 4901 | CharUnits StackSize; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4902 | if (IsIndirect) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4903 | StackSize = StackSlotSize; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4904 | else |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4905 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4906 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4907 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4908 | llvm::Value *NewStack = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4909 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4910 | |
| 4911 | // Write the new value of __stack for the next call to va_arg |
| 4912 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4913 | |
| 4914 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4915 | TyInfo.first < StackSlotSize) { |
| 4916 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 4917 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4918 | } |
| 4919 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4920 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4921 | |
| 4922 | CGF.EmitBranch(ContBlock); |
| 4923 | |
| 4924 | //======================================= |
| 4925 | // Tidy up |
| 4926 | //======================================= |
| 4927 | CGF.EmitBlock(ContBlock); |
| 4928 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4929 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 4930 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4931 | |
| 4932 | if (IsIndirect) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4933 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 4934 | TyInfo.second); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4935 | |
| 4936 | return ResAddr; |
| 4937 | } |
| 4938 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4939 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4940 | CodeGenFunction &CGF) const { |
| 4941 | // The backend's lowering doesn't support va_arg for aggregates or |
| 4942 | // illegal vector types. Lower VAArg here for these cases and use |
| 4943 | // the LLVM va_arg instruction for everything else. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4944 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4945 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4946 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4947 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4948 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4949 | // Empty records are ignored for parameter passing purposes. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4950 | if (isEmptyRecord(getContext(), Ty, true)) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4951 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 4952 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 4953 | return Addr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4954 | } |
| 4955 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4956 | // The size of the actual thing passed, which might end up just |
| 4957 | // being a pointer for indirect types. |
| 4958 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4959 | |
| 4960 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 4961 | // aggregates should be passed indirectly. |
| 4962 | bool IsIndirect = false; |
| 4963 | if (TyInfo.first.getQuantity() > 16) { |
| 4964 | const Type *Base = nullptr; |
| 4965 | uint64_t Members = 0; |
| 4966 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4967 | } |
| 4968 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4969 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 4970 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4971 | } |
| 4972 | |
| 4973 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4974 | // ARM ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4975 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4976 | |
| 4977 | namespace { |
| 4978 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4979 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4980 | public: |
| 4981 | enum ABIKind { |
| 4982 | APCS = 0, |
| 4983 | AAPCS = 1, |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 4984 | AAPCS_VFP = 2, |
| 4985 | AAPCS16_VFP = 3, |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4986 | }; |
| 4987 | |
| 4988 | private: |
| 4989 | ABIKind Kind; |
| 4990 | |
| 4991 | public: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 4992 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 4993 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4994 | setCCs(); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4995 | } |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4996 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4997 | bool isEABI() const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4998 | switch (getTarget().getTriple().getEnvironment()) { |
| 4999 | case llvm::Triple::Android: |
| 5000 | case llvm::Triple::EABI: |
| 5001 | case llvm::Triple::EABIHF: |
| 5002 | case llvm::Triple::GNUEABI: |
| 5003 | case llvm::Triple::GNUEABIHF: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 5004 | case llvm::Triple::MuslEABI: |
| 5005 | case llvm::Triple::MuslEABIHF: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5006 | return true; |
| 5007 | default: |
| 5008 | return false; |
| 5009 | } |
| 5010 | } |
| 5011 | |
| 5012 | bool isEABIHF() const { |
| 5013 | switch (getTarget().getTriple().getEnvironment()) { |
| 5014 | case llvm::Triple::EABIHF: |
| 5015 | case llvm::Triple::GNUEABIHF: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 5016 | case llvm::Triple::MuslEABIHF: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5017 | return true; |
| 5018 | default: |
| 5019 | return false; |
| 5020 | } |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5023 | ABIKind getABIKind() const { return Kind; } |
| 5024 | |
Tim Northover | 64eac85 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5025 | private: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5026 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5027 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5028 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5029 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5030 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5031 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5032 | uint64_t Members) const override; |
| 5033 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5034 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5035 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5036 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5037 | QualType Ty) const override; |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5038 | |
| 5039 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5040 | llvm::CallingConv::ID getABIDefaultCC() const; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5041 | void setCCs(); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 5042 | |
| 5043 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 5044 | ArrayRef<llvm::Type*> scalars, |
| 5045 | bool asReturnValue) const override { |
| 5046 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5047 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5048 | }; |
| 5049 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5050 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5051 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5052 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5053 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5054 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5055 | const ARMABIInfo &getABIInfo() const { |
| 5056 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5057 | } |
| 5058 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5059 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5060 | return 13; |
| 5061 | } |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5062 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5063 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5064 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 5065 | } |
| 5066 | |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5067 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5068 | llvm::Value *Address) const override { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5069 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5070 | |
| 5071 | // 0-15 are the 16 integer registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5072 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5073 | return false; |
| 5074 | } |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5075 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5076 | unsigned getSizeOfUnwindException() const override { |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5077 | if (getABIInfo().isEABI()) return 88; |
| 5078 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5079 | } |
Tim Northover | 64eac85 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5080 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5081 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5082 | CodeGen::CodeGenModule &CGM) const override { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5083 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | 64eac85 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5084 | if (!FD) |
| 5085 | return; |
| 5086 | |
| 5087 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5088 | if (!Attr) |
| 5089 | return; |
| 5090 | |
| 5091 | const char *Kind; |
| 5092 | switch (Attr->getInterrupt()) { |
| 5093 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5094 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5095 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5096 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5097 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5098 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5099 | } |
| 5100 | |
| 5101 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5102 | |
| 5103 | Fn->addFnAttr("interrupt", Kind); |
| 5104 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5105 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5106 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | 64eac85 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5107 | return; |
| 5108 | |
| 5109 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5110 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5111 | // the backend to perform a realignment as part of the function prologue. |
| 5112 | llvm::AttrBuilder B; |
| 5113 | B.addStackAlignmentAttr(8); |
| 5114 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 5115 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 5116 | llvm::AttributeSet::FunctionIndex, |
| 5117 | B)); |
| 5118 | } |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5119 | }; |
| 5120 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5121 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5122 | public: |
| 5123 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5124 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5125 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5126 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5127 | CodeGen::CodeGenModule &CGM) const override; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 5128 | |
| 5129 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5130 | llvm::SmallString<24> &Opt) const override { |
| 5131 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5132 | } |
| 5133 | |
| 5134 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5135 | llvm::SmallString<32> &Opt) const override { |
| 5136 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5137 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5138 | }; |
| 5139 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5140 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5141 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5142 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5143 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 5144 | } |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5147 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5148 | if (!getCXXABI().classifyReturnType(FI)) |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5149 | FI.getReturnInfo() = |
| 5150 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5151 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5152 | for (auto &I : FI.arguments()) |
| 5153 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5154 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5155 | // Always honor user-specified calling convention. |
| 5156 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5157 | return; |
| 5158 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5159 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5160 | if (cc != llvm::CallingConv::C) |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5161 | FI.setEffectiveCallingConvention(cc); |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5162 | } |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5163 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5164 | /// Return the default calling convention that LLVM will use. |
| 5165 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5166 | // The default calling convention that LLVM will infer. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 5167 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5168 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5169 | else if (isEABI()) |
| 5170 | return llvm::CallingConv::ARM_AAPCS; |
| 5171 | else |
| 5172 | return llvm::CallingConv::ARM_APCS; |
| 5173 | } |
| 5174 | |
| 5175 | /// Return the calling convention that our ABI would like us to use |
| 5176 | /// as the C calling convention. |
| 5177 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5178 | switch (getABIKind()) { |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5179 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5180 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5181 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5182 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5183 | } |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5184 | llvm_unreachable("bad ABI kind"); |
| 5185 | } |
| 5186 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5187 | void ARMABIInfo::setCCs() { |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5188 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5189 | |
| 5190 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5191 | // they'd just match what LLVM will infer from the triple. |
| 5192 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5193 | if (abiCC != getLLVMDefaultCC()) |
| 5194 | RuntimeCC = abiCC; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5195 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5196 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 5197 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 5198 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
| 5199 | switch (getABIKind()) { |
| 5200 | case APCS: |
| 5201 | case AAPCS16_VFP: |
| 5202 | if (abiCC != getLLVMDefaultCC()) |
| 5203 | BuiltinCC = abiCC; |
| 5204 | break; |
| 5205 | case AAPCS: |
| 5206 | case AAPCS_VFP: |
| 5207 | BuiltinCC = llvm::CallingConv::ARM_AAPCS; |
| 5208 | break; |
| 5209 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5210 | } |
| 5211 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5212 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5213 | bool isVariadic) const { |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5214 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5215 | // A single-precision floating-point type (including promoted |
| 5216 | // half-precision types); A double-precision floating-point type; |
| 5217 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5218 | // with a Base Type of a single- or double-precision floating-point type, |
| 5219 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5220 | // to four Elements. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5221 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
| 5222 | |
| 5223 | Ty = useFirstFieldIfTransparentUnion(Ty); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5224 | |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5225 | // Handle illegal vector types here. |
| 5226 | if (isIllegalVectorType(Ty)) { |
| 5227 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5228 | if (Size <= 32) { |
| 5229 | llvm::Type *ResType = |
| 5230 | llvm::Type::getInt32Ty(getVMContext()); |
| 5231 | return ABIArgInfo::getDirect(ResType); |
| 5232 | } |
| 5233 | if (Size == 64) { |
| 5234 | llvm::Type *ResType = llvm::VectorType::get( |
| 5235 | llvm::Type::getInt32Ty(getVMContext()), 2); |
| 5236 | return ABIArgInfo::getDirect(ResType); |
| 5237 | } |
| 5238 | if (Size == 128) { |
| 5239 | llvm::Type *ResType = llvm::VectorType::get( |
| 5240 | llvm::Type::getInt32Ty(getVMContext()), 4); |
| 5241 | return ABIArgInfo::getDirect(ResType); |
| 5242 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5243 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 5244 | } |
| 5245 | |
| 5246 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 5247 | // unspecified. This is not done for OpenCL as it handles the half type |
| 5248 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 1567b30 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5249 | if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5250 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5251 | llvm::Type::getFloatTy(getVMContext()) : |
| 5252 | llvm::Type::getInt32Ty(getVMContext()); |
| 5253 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5254 | } |
| 5255 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5256 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5257 | // Treat an enum type as its underlying type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5258 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5259 | Ty = EnumTy->getDecl()->getIntegerType(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5260 | } |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5261 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5262 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5263 | : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5264 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5265 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5266 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5267 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5268 | } |
Tim Northover | f5c3a25 | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5269 | |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5270 | // Ignore empty records. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5271 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5272 | return ABIArgInfo::getIgnore(); |
| 5273 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5274 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5275 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5276 | // into VFP registers. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5277 | const Type *Base = nullptr; |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5278 | uint64_t Members = 0; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5279 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5280 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5281 | // Base can be a floating-point or a vector. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5282 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5283 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5284 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5285 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5286 | // this convention even for a variadic function: the backend will use GPRs |
| 5287 | // if needed. |
| 5288 | const Type *Base = nullptr; |
| 5289 | uint64_t Members = 0; |
| 5290 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5291 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5292 | llvm::Type *Ty = |
| 5293 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5294 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5295 | } |
| 5296 | } |
| 5297 | |
| 5298 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5299 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5300 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5301 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5302 | // and a pointer is passed. |
| 5303 | return ABIArgInfo::getIndirect( |
| 5304 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5305 | } |
| 5306 | |
Manman Ren | 634b3d2 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5307 | // Support byval for ARM. |
Manman Ren | cb489dd | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5308 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5309 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5310 | // than ABI alignment. |
Manman Ren | fd1ba91 | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5311 | uint64_t ABIAlign = 4; |
| 5312 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5313 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 5314 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | fd1ba91 | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5315 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 5316 | |
Manman Ren | 885ad69 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5317 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5318 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
| 5319 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5320 | /*ByVal=*/true, |
| 5321 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | 79f3098 | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5322 | } |
| 5323 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 5324 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5325 | // same size and alignment. |
| 5326 | if (getTarget().isRenderScriptTarget()) { |
Matt Wala | 1d15151 | 2015-08-10 15:58:40 -0700 | [diff] [blame] | 5327 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5328 | } |
| 5329 | |
Daniel Dunbar | 8aa87c7 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5330 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5331 | llvm::Type* ElemTy; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5332 | unsigned SizeRegs; |
Eli Friedman | 79f3098 | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5333 | // FIXME: Try to match the types of the arguments more accurately where |
| 5334 | // we can. |
| 5335 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 53fc1a6 | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5336 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5337 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 78eb76e | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5338 | } else { |
Manman Ren | 78eb76e | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5339 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5340 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | 67d097e | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5341 | } |
Stuart Hastings | b7f62d0 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5342 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5343 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5346 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5347 | llvm::LLVMContext &VMContext) { |
| 5348 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5349 | // is called integer-like if its size is less than or equal to one word, and |
| 5350 | // the offset of each of its addressable sub-fields is zero. |
| 5351 | |
| 5352 | uint64_t Size = Context.getTypeSize(Ty); |
| 5353 | |
| 5354 | // Check that the type fits in a word. |
| 5355 | if (Size > 32) |
| 5356 | return false; |
| 5357 | |
| 5358 | // FIXME: Handle vector types! |
| 5359 | if (Ty->isVectorType()) |
| 5360 | return false; |
| 5361 | |
Daniel Dunbar | b0d5819 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5362 | // Float types are never treated as "integer like". |
| 5363 | if (Ty->isRealFloatingType()) |
| 5364 | return false; |
| 5365 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5366 | // If this is a builtin or pointer type then it is ok. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5367 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5368 | return true; |
| 5369 | |
Daniel Dunbar | 4581581 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5370 | // Small complex integer types are "integer like". |
| 5371 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5372 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5373 | |
| 5374 | // Single element and zero sized arrays should be allowed, by the definition |
| 5375 | // above, but they are not. |
| 5376 | |
| 5377 | // Otherwise, it must be a record type. |
| 5378 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5379 | if (!RT) return false; |
| 5380 | |
| 5381 | // Ignore records with flexible arrays. |
| 5382 | const RecordDecl *RD = RT->getDecl(); |
| 5383 | if (RD->hasFlexibleArrayMember()) |
| 5384 | return false; |
| 5385 | |
| 5386 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5387 | // like". |
| 5388 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5389 | |
| 5390 | bool HadField = false; |
| 5391 | unsigned idx = 0; |
| 5392 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5393 | i != e; ++i, ++idx) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5394 | const FieldDecl *FD = *i; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5395 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5396 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5397 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5398 | // struct { int : 0; int x } |
| 5399 | // is non-integer like according to gcc. |
| 5400 | if (FD->isBitField()) { |
| 5401 | if (!RD->isUnion()) |
| 5402 | HadField = true; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5403 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5404 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5405 | return false; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5406 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5407 | continue; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5410 | // Check if this field is at offset 0. |
| 5411 | if (Layout.getFieldOffset(idx) != 0) |
| 5412 | return false; |
| 5413 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5414 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5415 | return false; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5416 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5417 | // Only allow at most one field in a structure. This doesn't match the |
| 5418 | // wording above, but follows gcc in situations with a field following an |
| 5419 | // empty structure. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5420 | if (!RD->isUnion()) { |
| 5421 | if (HadField) |
| 5422 | return false; |
| 5423 | |
| 5424 | HadField = true; |
| 5425 | } |
| 5426 | } |
| 5427 | |
| 5428 | return true; |
| 5429 | } |
| 5430 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5431 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5432 | bool isVariadic) const { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5433 | bool IsEffectivelyAAPCS_VFP = |
| 5434 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5435 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5436 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5437 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5438 | |
Daniel Dunbar | f554b1c | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5439 | // Large vector types should be returned via memory. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5440 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5441 | return getNaturalAlignIndirect(RetTy); |
| 5442 | } |
| 5443 | |
| 5444 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5445 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5446 | // natively, and does not need to interwork with AAPCS code. |
Pirama Arumuga Nainar | 1567b30 | 2016-03-18 16:58:36 +0000 | [diff] [blame] | 5447 | if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5448 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5449 | llvm::Type::getFloatTy(getVMContext()) : |
| 5450 | llvm::Type::getInt32Ty(getVMContext()); |
| 5451 | return ABIArgInfo::getDirect(ResType); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5452 | } |
Daniel Dunbar | f554b1c | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5453 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5454 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5455 | // Treat an enum type as its underlying type. |
| 5456 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5457 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5458 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5459 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5460 | : ABIArgInfo::getDirect(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5461 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5462 | |
| 5463 | // Are we following APCS? |
| 5464 | if (getABIKind() == APCS) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5465 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5466 | return ABIArgInfo::getIgnore(); |
| 5467 | |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5468 | // Complex types are all returned as packed integers. |
| 5469 | // |
| 5470 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5471 | // correctly. |
| 5472 | if (RetTy->isAnyComplexType()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5473 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5474 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5475 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5476 | // Integer like structures are returned in r0. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5477 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5478 | // Return in the smallest viable integer type. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5479 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5480 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5481 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5482 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5483 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5484 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5485 | } |
| 5486 | |
| 5487 | // Otherwise return in memory. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5488 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5489 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5490 | |
| 5491 | // Otherwise this is an AAPCS variant. |
| 5492 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5493 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5494 | return ABIArgInfo::getIgnore(); |
| 5495 | |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5496 | // Check for homogeneous aggregates with AAPCS-VFP. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5497 | if (IsEffectivelyAAPCS_VFP) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5498 | const Type *Base = nullptr; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5499 | uint64_t Members = 0; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5500 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5501 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5502 | // Homogeneous Aggregates are returned directly. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5503 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5504 | } |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5507 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5508 | // are returned indirectly. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5509 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5510 | if (Size <= 32) { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 5511 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 5512 | // same size and alignment. |
| 5513 | if (getTarget().isRenderScriptTarget()) { |
Matt Wala | 1d15151 | 2015-08-10 15:58:40 -0700 | [diff] [blame] | 5514 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5515 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 5516 | if (getDataLayout().isBigEndian()) |
| 5517 | // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4) |
| 5518 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5519 | |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5520 | // Return in the smallest viable integer type. |
| 5521 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5522 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5523 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5524 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5525 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5526 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5527 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5528 | llvm::Type *CoerceTy = |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 5529 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5530 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5531 | } |
| 5532 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5533 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5536 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5537 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5538 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 5539 | if (isAndroid()) { |
| 5540 | // Android shipped using Clang 3.1, which supported a slightly different |
| 5541 | // vector ABI. The primary differences were that 3-element vector types |
| 5542 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 5543 | // accepts that legacy behavior for Android only. |
| 5544 | // Check whether VT is legal. |
| 5545 | unsigned NumElements = VT->getNumElements(); |
| 5546 | // NumElements should be power of 2 or equal to 3. |
| 5547 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 5548 | return true; |
| 5549 | } else { |
| 5550 | // Check whether VT is legal. |
| 5551 | unsigned NumElements = VT->getNumElements(); |
| 5552 | uint64_t Size = getContext().getTypeSize(VT); |
| 5553 | // NumElements should be power of 2. |
| 5554 | if (!llvm::isPowerOf2_32(NumElements)) |
| 5555 | return true; |
| 5556 | // Size should be greater than 32 bits. |
| 5557 | return Size <= 32; |
| 5558 | } |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5559 | } |
| 5560 | return false; |
| 5561 | } |
| 5562 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5563 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5564 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 5565 | // double, or 64-bit or 128-bit vectors. |
| 5566 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5567 | if (BT->getKind() == BuiltinType::Float || |
| 5568 | BT->getKind() == BuiltinType::Double || |
| 5569 | BT->getKind() == BuiltinType::LongDouble) |
| 5570 | return true; |
| 5571 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5572 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5573 | if (VecSize == 64 || VecSize == 128) |
| 5574 | return true; |
| 5575 | } |
| 5576 | return false; |
| 5577 | } |
| 5578 | |
| 5579 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5580 | uint64_t Members) const { |
| 5581 | return Members <= 4; |
| 5582 | } |
| 5583 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5584 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5585 | QualType Ty) const { |
| 5586 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5587 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5588 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 373ac0a | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5589 | if (isEmptyRecord(getContext(), Ty, true)) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5590 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 5591 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5592 | return Addr; |
Tim Northover | 373ac0a | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5595 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5596 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5597 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5598 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 5599 | bool IsIndirect = false; |
| 5600 | const Type *Base = nullptr; |
| 5601 | uint64_t Members = 0; |
| 5602 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 5603 | IsIndirect = true; |
| 5604 | |
| 5605 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 5606 | // allocated by the caller. |
| 5607 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 5608 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5609 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 5610 | IsIndirect = true; |
| 5611 | |
| 5612 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5613 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 5614 | // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5615 | // Our callers should be prepared to handle an under-aligned address. |
| 5616 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 5617 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5618 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5619 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
| 5620 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5621 | // ARMv7k allows type alignment up to 16 bytes. |
| 5622 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5623 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
| 5624 | } else { |
| 5625 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5626 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5627 | TyInfo.second = TyAlignForABI; |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5628 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5629 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 5630 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5631 | } |
| 5632 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5633 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5634 | // NVPTX ABI Implementation |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5635 | //===----------------------------------------------------------------------===// |
| 5636 | |
| 5637 | namespace { |
| 5638 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5639 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5640 | public: |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5641 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5642 | |
| 5643 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5644 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5645 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5646 | void computeInfo(CGFunctionInfo &FI) const override; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5647 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5648 | QualType Ty) const override; |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5649 | }; |
| 5650 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5651 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5652 | public: |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5653 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5654 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5655 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5656 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5657 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5658 | private: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5659 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5660 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5661 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5662 | }; |
| 5663 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5664 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5665 | if (RetTy->isVoidType()) |
| 5666 | return ABIArgInfo::getIgnore(); |
Bill Wendling | 846ff9f | 2013-11-21 23:31:45 +0000 | [diff] [blame] | 5667 | |
| 5668 | // note: this is different from default ABI |
| 5669 | if (!RetTy->isScalarType()) |
| 5670 | return ABIArgInfo::getDirect(); |
| 5671 | |
| 5672 | // Treat an enum type as its underlying type. |
| 5673 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5674 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5675 | |
| 5676 | return (RetTy->isPromotableIntegerType() ? |
| 5677 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5678 | } |
| 5679 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5680 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Bill Wendling | 846ff9f | 2013-11-21 23:31:45 +0000 | [diff] [blame] | 5681 | // Treat an enum type as its underlying type. |
| 5682 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5683 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5684 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5685 | // Return aggregates type as indirect by value |
| 5686 | if (isAggregateTypeForABI(Ty)) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5687 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 5688 | |
Bill Wendling | 846ff9f | 2013-11-21 23:31:45 +0000 | [diff] [blame] | 5689 | return (Ty->isPromotableIntegerType() ? |
| 5690 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5691 | } |
| 5692 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5693 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5694 | if (!getCXXABI().classifyReturnType(FI)) |
| 5695 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5696 | for (auto &I : FI.arguments()) |
| 5697 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5698 | |
| 5699 | // Always honor user-specified calling convention. |
| 5700 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5701 | return; |
| 5702 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5703 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5704 | } |
| 5705 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5706 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5707 | QualType Ty) const { |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5708 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5709 | } |
| 5710 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5711 | void NVPTXTargetCodeGenInfo:: |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5712 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5713 | CodeGen::CodeGenModule &M) const{ |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5714 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5715 | if (!FD) return; |
| 5716 | |
| 5717 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5718 | |
| 5719 | // Perform special handling in OpenCL mode |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5720 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5721 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5722 | // By default, all functions are device functions |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5723 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5724 | // OpenCL __kernel functions get kernel metadata |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5725 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5726 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5727 | // And kernel functions are not subject to inlining |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5728 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5729 | } |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5730 | } |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5731 | |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5732 | // Perform special handling in CUDA mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5733 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5734 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5735 | // __global__ functions cannot be called from the device, we do not |
| 5736 | // need to set the noinline attribute. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5737 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5738 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5739 | addNVVMMetadata(F, "kernel", 1); |
| 5740 | } |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5741 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5742 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5743 | llvm::APSInt MaxThreads(32); |
| 5744 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5745 | if (MaxThreads > 0) |
| 5746 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5747 | |
| 5748 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5749 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5750 | // we don't have to add a PTX directive. |
| 5751 | if (Attr->getMinBlocks()) { |
| 5752 | llvm::APSInt MinBlocks(32); |
| 5753 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5754 | if (MinBlocks > 0) |
| 5755 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5756 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5757 | } |
| 5758 | } |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5759 | } |
| 5760 | } |
| 5761 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5762 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5763 | int Operand) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5764 | llvm::Module *M = F->getParent(); |
| 5765 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5766 | |
| 5767 | // Get "nvvm.annotations" metadata node |
| 5768 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5769 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 5770 | llvm::Metadata *MDVals[] = { |
| 5771 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5772 | llvm::ConstantAsMetadata::get( |
| 5773 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5774 | // Append metadata to nvvm.annotations |
| 5775 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5776 | } |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
| 5779 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5780 | // SystemZ ABI Implementation |
| 5781 | //===----------------------------------------------------------------------===// |
| 5782 | |
| 5783 | namespace { |
| 5784 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 5785 | class SystemZABIInfo : public SwiftABIInfo { |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5786 | bool HasVector; |
| 5787 | |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5788 | public: |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5789 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 5790 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5791 | |
| 5792 | bool isPromotableIntegerType(QualType Ty) const; |
| 5793 | bool isCompoundType(QualType Ty) const; |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5794 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5795 | bool isFPArgumentType(QualType Ty) const; |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5796 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5797 | |
| 5798 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5799 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5800 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5801 | void computeInfo(CGFunctionInfo &FI) const override { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 5802 | if (!getCXXABI().classifyReturnType(FI)) |
| 5803 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5804 | for (auto &I : FI.arguments()) |
| 5805 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5806 | } |
| 5807 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5808 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5809 | QualType Ty) const override; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 5810 | |
| 5811 | bool shouldPassIndirectlyForSwift(CharUnits totalSize, |
| 5812 | ArrayRef<llvm::Type*> scalars, |
| 5813 | bool asReturnValue) const override { |
| 5814 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5815 | } |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5816 | }; |
| 5817 | |
| 5818 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5819 | public: |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5820 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5821 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5822 | }; |
| 5823 | |
| 5824 | } |
| 5825 | |
| 5826 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5827 | // Treat an enum type as its underlying type. |
| 5828 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5829 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5830 | |
| 5831 | // Promotable integer types are required to be promoted by the ABI. |
| 5832 | if (Ty->isPromotableIntegerType()) |
| 5833 | return true; |
| 5834 | |
| 5835 | // 32-bit values must also be promoted. |
| 5836 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5837 | switch (BT->getKind()) { |
| 5838 | case BuiltinType::Int: |
| 5839 | case BuiltinType::UInt: |
| 5840 | return true; |
| 5841 | default: |
| 5842 | return false; |
| 5843 | } |
| 5844 | return false; |
| 5845 | } |
| 5846 | |
| 5847 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 5848 | return (Ty->isAnyComplexType() || |
| 5849 | Ty->isVectorType() || |
| 5850 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5851 | } |
| 5852 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5853 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5854 | return (HasVector && |
| 5855 | Ty->isVectorType() && |
| 5856 | getContext().getTypeSize(Ty) <= 128); |
| 5857 | } |
| 5858 | |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5859 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5860 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5861 | switch (BT->getKind()) { |
| 5862 | case BuiltinType::Float: |
| 5863 | case BuiltinType::Double: |
| 5864 | return true; |
| 5865 | default: |
| 5866 | return false; |
| 5867 | } |
| 5868 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5869 | return false; |
| 5870 | } |
| 5871 | |
| 5872 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5873 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5874 | const RecordDecl *RD = RT->getDecl(); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5875 | QualType Found; |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5876 | |
| 5877 | // If this is a C++ record, check the bases first. |
| 5878 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5879 | for (const auto &I : CXXRD->bases()) { |
| 5880 | QualType Base = I.getType(); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5881 | |
| 5882 | // Empty bases don't affect things either way. |
| 5883 | if (isEmptyRecord(getContext(), Base, true)) |
| 5884 | continue; |
| 5885 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5886 | if (!Found.isNull()) |
| 5887 | return Ty; |
| 5888 | Found = GetSingleElementType(Base); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5889 | } |
| 5890 | |
| 5891 | // Check the fields. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5892 | for (const auto *FD : RD->fields()) { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 5893 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5894 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5895 | // do count. So do anonymous bitfields that aren't zero-sized. |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 5896 | if (getContext().getLangOpts().CPlusPlus && |
| 5897 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5898 | continue; |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5899 | |
| 5900 | // Unlike isSingleElementStruct(), arrays do not count. |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5901 | // Nested structures still do though. |
| 5902 | if (!Found.isNull()) |
| 5903 | return Ty; |
| 5904 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5905 | } |
| 5906 | |
| 5907 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5908 | // An 8-byte aligned struct s { float f; } is passed as a double. |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5909 | if (!Found.isNull()) |
| 5910 | return Found; |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5911 | } |
| 5912 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5913 | return Ty; |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5914 | } |
| 5915 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5916 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5917 | QualType Ty) const { |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5918 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5919 | // struct { |
| 5920 | // i64 __gpr; |
| 5921 | // i64 __fpr; |
| 5922 | // i8 *__overflow_arg_area; |
| 5923 | // i8 *__reg_save_area; |
| 5924 | // }; |
| 5925 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5926 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5927 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5928 | // always passed on the stack. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5929 | Ty = getContext().getCanonicalType(Ty); |
| 5930 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 5931 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5932 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5933 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5934 | bool IsIndirect = AI.isIndirect(); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 5935 | bool InFPRs = false; |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5936 | bool IsVector = false; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5937 | CharUnits UnpaddedSize; |
| 5938 | CharUnits DirectAlign; |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5939 | if (IsIndirect) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5940 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 5941 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 5942 | } else { |
| 5943 | if (AI.getCoerceToType()) |
| 5944 | ArgTy = AI.getCoerceToType(); |
| 5945 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5946 | IsVector = ArgTy->isVectorTy(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5947 | UnpaddedSize = TyInfo.first; |
| 5948 | DirectAlign = TyInfo.second; |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 5949 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5950 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 5951 | if (IsVector && UnpaddedSize > PaddedSize) |
| 5952 | PaddedSize = CharUnits::fromQuantity(16); |
| 5953 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5954 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5955 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5956 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5957 | llvm::Type *IndexTy = CGF.Int64Ty; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5958 | llvm::Value *PaddedSizeV = |
| 5959 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5960 | |
| 5961 | if (IsVector) { |
| 5962 | // Work out the address of a vector argument on the stack. |
| 5963 | // Vector arguments are always passed in the high bits of a |
| 5964 | // single (8 byte) or double (16 byte) stack slot. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5965 | Address OverflowArgAreaPtr = |
| 5966 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5967 | "overflow_arg_area_ptr"); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5968 | Address OverflowArgArea = |
| 5969 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5970 | TyInfo.second); |
| 5971 | Address MemAddr = |
| 5972 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5973 | |
| 5974 | // Update overflow_arg_area_ptr pointer |
| 5975 | llvm::Value *NewOverflowArgArea = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5976 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5977 | "overflow_arg_area"); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 5978 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5979 | |
| 5980 | return MemAddr; |
| 5981 | } |
| 5982 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5983 | assert(PaddedSize.getQuantity() == 8); |
| 5984 | |
| 5985 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 5986 | CharUnits RegPadding; |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5987 | if (InFPRs) { |
| 5988 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5989 | RegCountField = 1; // __fpr |
| 5990 | RegSaveIndex = 16; // save offset for f0 |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5991 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5992 | } else { |
| 5993 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5994 | RegCountField = 0; // __gpr |
| 5995 | RegSaveIndex = 2; // save offset for r2 |
| 5996 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5997 | } |
| 5998 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 5999 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 6000 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 6001 | "reg_count_ptr"); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6002 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6003 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6004 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6005 | "fits_in_regs"); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6006 | |
| 6007 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6008 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6009 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6010 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6011 | |
| 6012 | // Emit code to load the value if it was passed in registers. |
| 6013 | CGF.EmitBlock(InRegBlock); |
| 6014 | |
| 6015 | // Work out the address of an argument register. |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6016 | llvm::Value *ScaledRegCount = |
| 6017 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6018 | llvm::Value *RegBase = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6019 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6020 | + RegPadding.getQuantity()); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6021 | llvm::Value *RegOffset = |
| 6022 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6023 | Address RegSaveAreaPtr = |
| 6024 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 6025 | "reg_save_area_ptr"); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6026 | llvm::Value *RegSaveArea = |
| 6027 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6028 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6029 | "raw_reg_addr"), |
| 6030 | PaddedSize); |
| 6031 | Address RegAddr = |
| 6032 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6033 | |
| 6034 | // Update the register count |
| 6035 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6036 | llvm::Value *NewRegCount = |
| 6037 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6038 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6039 | CGF.EmitBranch(ContBlock); |
| 6040 | |
| 6041 | // Emit code to load the value if it was passed in memory. |
| 6042 | CGF.EmitBlock(InMemBlock); |
| 6043 | |
| 6044 | // Work out the address of a stack argument. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6045 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 6046 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 6047 | Address OverflowArgArea = |
| 6048 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6049 | PaddedSize); |
| 6050 | Address RawMemAddr = |
| 6051 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6052 | Address MemAddr = |
| 6053 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6054 | |
| 6055 | // Update overflow_arg_area_ptr pointer |
| 6056 | llvm::Value *NewOverflowArgArea = |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6057 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6058 | "overflow_arg_area"); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6059 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6060 | CGF.EmitBranch(ContBlock); |
| 6061 | |
| 6062 | // Return the appropriate result. |
| 6063 | CGF.EmitBlock(ContBlock); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6064 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6065 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6066 | |
| 6067 | if (IsIndirect) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6068 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6069 | TyInfo.second); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6070 | |
| 6071 | return ResAddr; |
| 6072 | } |
| 6073 | |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6074 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6075 | if (RetTy->isVoidType()) |
| 6076 | return ABIArgInfo::getIgnore(); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6077 | if (isVectorArgumentType(RetTy)) |
| 6078 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6079 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6080 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6081 | return (isPromotableIntegerType(RetTy) ? |
| 6082 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6083 | } |
| 6084 | |
| 6085 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6086 | // Handle the generic C++ ABI. |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6087 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6088 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6089 | |
| 6090 | // Integers and enums are extended to full register width. |
| 6091 | if (isPromotableIntegerType(Ty)) |
| 6092 | return ABIArgInfo::getExtend(); |
| 6093 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6094 | // Handle vector types and vector-like structure types. Note that |
| 6095 | // as opposed to float-like structure types, we do not allow any |
| 6096 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6097 | uint64_t Size = getContext().getTypeSize(Ty); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6098 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6099 | if (isVectorArgumentType(SingleElementTy) && |
| 6100 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6101 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6102 | |
| 6103 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6104 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6105 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6106 | |
| 6107 | // Handle small structures. |
| 6108 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6109 | // Structures with flexible arrays have variable length, so really |
| 6110 | // fail the size test above. |
| 6111 | const RecordDecl *RD = RT->getDecl(); |
| 6112 | if (RD->hasFlexibleArrayMember()) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6113 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6114 | |
| 6115 | // The structure is passed as an unextended integer, a float, or a double. |
| 6116 | llvm::Type *PassTy; |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6117 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6118 | assert(Size == 32 || Size == 64); |
| 6119 | if (Size == 32) |
| 6120 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6121 | else |
| 6122 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6123 | } else |
| 6124 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6125 | return ABIArgInfo::getDirect(PassTy); |
| 6126 | } |
| 6127 | |
| 6128 | // Non-structure compounds are passed indirectly. |
| 6129 | if (isCompoundType(Ty)) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6130 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6131 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6132 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
| 6135 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6136 | // MSP430 ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6137 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6138 | |
| 6139 | namespace { |
| 6140 | |
| 6141 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6142 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6143 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6144 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6145 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6146 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6147 | }; |
| 6148 | |
| 6149 | } |
| 6150 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6151 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6152 | llvm::GlobalValue *GV, |
| 6153 | CodeGen::CodeGenModule &M) const { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6154 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6155 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 6156 | // Handle 'interrupt' attribute: |
| 6157 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6158 | |
| 6159 | // Step 1: Set ISR calling convention. |
| 6160 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 6161 | |
| 6162 | // Step 2: Add attributes goodness. |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6163 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6164 | |
| 6165 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | f419a85 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 6166 | unsigned Num = attr->getNumber() / 2; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6167 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 6168 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6169 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6170 | } |
| 6171 | } |
| 6172 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6173 | //===----------------------------------------------------------------------===// |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6174 | // MIPS ABI Implementation. This works for both little-endian and |
| 6175 | // big-endian variants. |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6176 | //===----------------------------------------------------------------------===// |
| 6177 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6178 | namespace { |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6179 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6180 | bool IsO32; |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6181 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6182 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6183 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6184 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6185 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6186 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6187 | public: |
Akira Hatanaka | b551dd3 | 2011-11-03 00:05:50 +0000 | [diff] [blame] | 6188 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6189 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
| 6190 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6191 | |
| 6192 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6193 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6194 | void computeInfo(CGFunctionInfo &FI) const override; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6195 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6196 | QualType Ty) const override; |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6197 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6198 | }; |
| 6199 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6200 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6201 | unsigned SizeOfUnwindException; |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6202 | public: |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6203 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6204 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
| 6205 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6206 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6207 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6208 | return 29; |
| 6209 | } |
| 6210 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6211 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6212 | CodeGen::CodeGenModule &CGM) const override { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6213 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6214 | if (!FD) return; |
Rafael Espindola | d8e6d6d | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6215 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6216 | if (FD->hasAttr<Mips16Attr>()) { |
| 6217 | Fn->addFnAttr("mips16"); |
| 6218 | } |
| 6219 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6220 | Fn->addFnAttr("nomips16"); |
| 6221 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6222 | |
| 6223 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6224 | if (!Attr) |
| 6225 | return; |
| 6226 | |
| 6227 | const char *Kind; |
| 6228 | switch (Attr->getInterrupt()) { |
| 6229 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6230 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6231 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6232 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6233 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6234 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6235 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6236 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6237 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6238 | } |
| 6239 | |
| 6240 | Fn->addFnAttr("interrupt", Kind); |
| 6241 | |
Reed Kotler | 7dfd182 | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6242 | } |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6243 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6244 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6245 | llvm::Value *Address) const override; |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6246 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6247 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6248 | return SizeOfUnwindException; |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6249 | } |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6250 | }; |
| 6251 | } |
| 6252 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6253 | void MipsABIInfo::CoerceToIntArgs( |
| 6254 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6255 | llvm::IntegerType *IntTy = |
| 6256 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6257 | |
| 6258 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6259 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6260 | ArgList.push_back(IntTy); |
| 6261 | |
| 6262 | // If necessary, add one more integer type to ArgList. |
| 6263 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6264 | |
| 6265 | if (R) |
| 6266 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6267 | } |
| 6268 | |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6269 | // In N32/64, an aligned double precision floating point field is passed in |
| 6270 | // a register. |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6271 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6272 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6273 | |
| 6274 | if (IsO32) { |
| 6275 | CoerceToIntArgs(TySize, ArgList); |
| 6276 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6277 | } |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6278 | |
Akira Hatanaka | 2afd23d | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6279 | if (Ty->isComplexType()) |
| 6280 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 6d1080f | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6281 | |
Akira Hatanaka | a34e921 | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6282 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6283 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6284 | // Unions/vectors are passed in integer registers. |
| 6285 | if (!RT || !RT->isStructureOrClassType()) { |
| 6286 | CoerceToIntArgs(TySize, ArgList); |
| 6287 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6288 | } |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6289 | |
| 6290 | const RecordDecl *RD = RT->getDecl(); |
| 6291 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6292 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6293 | |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6294 | uint64_t LastOffset = 0; |
| 6295 | unsigned idx = 0; |
| 6296 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6297 | |
Akira Hatanaka | a34e921 | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6298 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6299 | // double fields. |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6300 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6301 | i != e; ++i, ++idx) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6302 | const QualType Ty = i->getType(); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6303 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6304 | |
| 6305 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6306 | continue; |
| 6307 | |
| 6308 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6309 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6310 | continue; |
| 6311 | |
| 6312 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6313 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6314 | ArgList.push_back(I64); |
| 6315 | |
| 6316 | // Add double type. |
| 6317 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6318 | LastOffset = Offset + 64; |
| 6319 | } |
| 6320 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6321 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6322 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6323 | |
| 6324 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6325 | } |
| 6326 | |
Akira Hatanaka | 7ebd953 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6327 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6328 | uint64_t Offset) const { |
| 6329 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6330 | return nullptr; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6331 | |
Akira Hatanaka | 7ebd953 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6332 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6333 | } |
Akira Hatanaka | 9659d59 | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6334 | |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6335 | ABIArgInfo |
| 6336 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6337 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6338 | |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6339 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6340 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6341 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6342 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6343 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6344 | (uint64_t)StackAlignInBytes); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 6345 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6346 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6347 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6348 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6349 | // Ignore empty aggregates. |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6350 | if (TySize == 0) |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6351 | return ABIArgInfo::getIgnore(); |
| 6352 | |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6353 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6354 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6355 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6356 | } |
Akira Hatanaka | 511949b | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6357 | |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6358 | // If we have reached here, aggregates are passed directly by coercing to |
| 6359 | // another structure type. Padding is inserted if the offset of the |
| 6360 | // aggregate is unaligned. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6361 | ABIArgInfo ArgInfo = |
| 6362 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6363 | getPaddingType(OrigOffset, CurrOffset)); |
| 6364 | ArgInfo.setInReg(true); |
| 6365 | return ArgInfo; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6366 | } |
| 6367 | |
| 6368 | // Treat an enum type as its underlying type. |
| 6369 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6370 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6371 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6372 | // All integral types are promoted to the GPR width. |
| 6373 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6374 | return ABIArgInfo::getExtend(); |
| 6375 | |
Akira Hatanaka | 7ebd953 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6376 | return ABIArgInfo::getDirect( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6377 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6378 | } |
| 6379 | |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6380 | llvm::Type* |
| 6381 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6382 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6383 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6384 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6385 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6386 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6387 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6388 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6389 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6390 | // N32/64 returns struct/classes in floating point registers if the |
| 6391 | // following conditions are met: |
| 6392 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6393 | // 2. The struct/class has one or two fields all of which are floating |
| 6394 | // point types. |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6395 | // 3. The offset of the first field is zero (this follows what gcc does). |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6396 | // |
| 6397 | // Any other composite results are returned in integer registers. |
| 6398 | // |
| 6399 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6400 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6401 | for (; b != e; ++b) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6402 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6403 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6404 | if (!BT || !BT->isFloatingPoint()) |
| 6405 | break; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6406 | |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6407 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6408 | } |
| 6409 | |
| 6410 | if (b == e) |
| 6411 | return llvm::StructType::get(getVMContext(), RTList, |
| 6412 | RD->hasAttr<PackedAttr>()); |
| 6413 | |
| 6414 | RTList.clear(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6415 | } |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6418 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6419 | return llvm::StructType::get(getVMContext(), RTList); |
| 6420 | } |
| 6421 | |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6422 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | a8536c0 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6423 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6424 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6425 | if (RetTy->isVoidType()) |
| 6426 | return ABIArgInfo::getIgnore(); |
| 6427 | |
| 6428 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6429 | // However, N32/N64 ignores zero sized return values. |
| 6430 | if (!IsO32 && Size == 0) |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6431 | return ABIArgInfo::getIgnore(); |
| 6432 | |
Akira Hatanaka | 8aeb147 | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6433 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6434 | if (Size <= 128) { |
| 6435 | if (RetTy->isAnyComplexType()) |
| 6436 | return ABIArgInfo::getDirect(); |
| 6437 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6438 | // O32 returns integer vectors in registers and N32/N64 returns all small |
| 6439 | // aggregates in registers. |
| 6440 | if (!IsO32 || |
| 6441 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6442 | ABIArgInfo ArgInfo = |
| 6443 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6444 | ArgInfo.setInReg(true); |
| 6445 | return ArgInfo; |
| 6446 | } |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6447 | } |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6448 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6449 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6450 | } |
| 6451 | |
| 6452 | // Treat an enum type as its underlying type. |
| 6453 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6454 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6455 | |
| 6456 | return (RetTy->isPromotableIntegerType() ? |
| 6457 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6458 | } |
| 6459 | |
| 6460 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | cc66254 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6461 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6462 | if (!getCXXABI().classifyReturnType(FI)) |
| 6463 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | cc66254 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6464 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6465 | // Check if a pointer to an aggregate is passed as a hidden argument. |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6466 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | cc66254 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6467 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6468 | for (auto &I : FI.arguments()) |
| 6469 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6470 | } |
| 6471 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6472 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6473 | QualType OrigTy) const { |
| 6474 | QualType Ty = OrigTy; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6475 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6476 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6477 | // Pointers are also promoted in the same way but this only matters for N32. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6478 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6479 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6480 | bool DidPromote = false; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6481 | if ((Ty->isIntegerType() && |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6482 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6483 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6484 | DidPromote = true; |
| 6485 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6486 | Ty->isSignedIntegerType()); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 6487 | } |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6488 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6489 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 6490 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6491 | // The alignment of things in the argument area is never larger than |
| 6492 | // StackAlignInBytes. |
| 6493 | TyInfo.second = |
| 6494 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6495 | |
| 6496 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6497 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6498 | |
| 6499 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6500 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6501 | |
| 6502 | |
| 6503 | // If there was a promotion, "unpromote" into a temporary. |
| 6504 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6505 | if (DidPromote) { |
| 6506 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6507 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6508 | |
| 6509 | // Truncate down to the right width. |
| 6510 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6511 | : CGF.IntPtrTy); |
| 6512 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6513 | if (OrigTy->isPointerType()) |
| 6514 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6515 | |
| 6516 | CGF.Builder.CreateStore(V, Temp); |
| 6517 | Addr = Temp; |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 6518 | } |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 6519 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6520 | return Addr; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6521 | } |
| 6522 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6523 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6524 | int TySize = getContext().getTypeSize(Ty); |
| 6525 | |
| 6526 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 6527 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 6528 | return true; |
| 6529 | |
| 6530 | return false; |
| 6531 | } |
| 6532 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6533 | bool |
| 6534 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6535 | llvm::Value *Address) const { |
| 6536 | // This information comes from gcc's implementation, which seems to |
| 6537 | // as canonical as it gets. |
| 6538 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6539 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 6540 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6541 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6542 | |
| 6543 | // 0-31 are the general purpose registers, $0 - $31. |
| 6544 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 6545 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 6546 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6547 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6548 | |
| 6549 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 6550 | // They are one bit wide and ignored here. |
| 6551 | |
| 6552 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 6553 | // (coprocessor 1 is the FP unit) |
| 6554 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 6555 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 6556 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6557 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6558 | return false; |
| 6559 | } |
| 6560 | |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6561 | //===----------------------------------------------------------------------===// |
| 6562 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6563 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6564 | // handling. |
| 6565 | //===----------------------------------------------------------------------===// |
| 6566 | |
| 6567 | namespace { |
| 6568 | |
| 6569 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 6570 | public: |
| 6571 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 6572 | : DefaultTargetCodeGenInfo(CGT) {} |
| 6573 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6574 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6575 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6576 | }; |
| 6577 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6578 | void TCETargetCodeGenInfo::setTargetAttributes( |
| 6579 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6580 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6581 | if (!FD) return; |
| 6582 | |
| 6583 | llvm::Function *F = cast<llvm::Function>(GV); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6584 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6585 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6586 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 6587 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6588 | F->addFnAttr(llvm::Attribute::NoInline); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6589 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 6590 | if (Attr) { |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6591 | // Convert the reqd_work_group_size() attributes to metadata. |
| 6592 | llvm::LLVMContext &Context = F->getContext(); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6593 | llvm::NamedMDNode *OpenCLMetadata = |
| 6594 | M.getModule().getOrInsertNamedMetadata( |
| 6595 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6596 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6597 | SmallVector<llvm::Metadata *, 5> Operands; |
| 6598 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6599 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6600 | Operands.push_back( |
| 6601 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6602 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 6603 | Operands.push_back( |
| 6604 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6605 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 6606 | Operands.push_back( |
| 6607 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6608 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6609 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6610 | // Add a boolean constant operand for "required" (true) or "hint" |
| 6611 | // (false) for implementing the work_group_size_hint attr later. |
| 6612 | // Currently always true as the hint is not yet implemented. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6613 | Operands.push_back( |
| 6614 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6615 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 6616 | } |
| 6617 | } |
| 6618 | } |
| 6619 | } |
| 6620 | |
| 6621 | } |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6622 | |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6623 | //===----------------------------------------------------------------------===// |
| 6624 | // Hexagon ABI Implementation |
| 6625 | //===----------------------------------------------------------------------===// |
| 6626 | |
| 6627 | namespace { |
| 6628 | |
| 6629 | class HexagonABIInfo : public ABIInfo { |
| 6630 | |
| 6631 | |
| 6632 | public: |
| 6633 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6634 | |
| 6635 | private: |
| 6636 | |
| 6637 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6638 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 6639 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6640 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6641 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6642 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6643 | QualType Ty) const override; |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6644 | }; |
| 6645 | |
| 6646 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6647 | public: |
| 6648 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6649 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 6650 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6651 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6652 | return 29; |
| 6653 | } |
| 6654 | }; |
| 6655 | |
| 6656 | } |
| 6657 | |
| 6658 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 6659 | if (!getCXXABI().classifyReturnType(FI)) |
| 6660 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6661 | for (auto &I : FI.arguments()) |
| 6662 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6663 | } |
| 6664 | |
| 6665 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 6666 | if (!isAggregateTypeForABI(Ty)) { |
| 6667 | // Treat an enum type as its underlying type. |
| 6668 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6669 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6670 | |
| 6671 | return (Ty->isPromotableIntegerType() ? |
| 6672 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6673 | } |
| 6674 | |
| 6675 | // Ignore empty records. |
| 6676 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6677 | return ABIArgInfo::getIgnore(); |
| 6678 | |
Mark Lacey | 2363072 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6679 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6680 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6681 | |
| 6682 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6683 | if (Size > 64) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6684 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6685 | // Pass in the smallest viable integer type. |
| 6686 | else if (Size > 32) |
| 6687 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6688 | else if (Size > 16) |
| 6689 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6690 | else if (Size > 8) |
| 6691 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6692 | else |
| 6693 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6694 | } |
| 6695 | |
| 6696 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6697 | if (RetTy->isVoidType()) |
| 6698 | return ABIArgInfo::getIgnore(); |
| 6699 | |
| 6700 | // Large vector types should be returned via memory. |
| 6701 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6702 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6703 | |
| 6704 | if (!isAggregateTypeForABI(RetTy)) { |
| 6705 | // Treat an enum type as its underlying type. |
| 6706 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6707 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6708 | |
| 6709 | return (RetTy->isPromotableIntegerType() ? |
| 6710 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6711 | } |
| 6712 | |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6713 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6714 | return ABIArgInfo::getIgnore(); |
| 6715 | |
| 6716 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6717 | // are returned indirectly. |
| 6718 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6719 | if (Size <= 64) { |
| 6720 | // Return in the smallest viable integer type. |
| 6721 | if (Size <= 8) |
| 6722 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6723 | if (Size <= 16) |
| 6724 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6725 | if (Size <= 32) |
| 6726 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6727 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6728 | } |
| 6729 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6730 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6733 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6734 | QualType Ty) const { |
| 6735 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 6736 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6737 | getContext().getTypeInfoInChars(Ty), |
| 6738 | CharUnits::fromQuantity(4), |
| 6739 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6740 | } |
| 6741 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6742 | //===----------------------------------------------------------------------===// |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 6743 | // Lanai ABI Implementation |
| 6744 | //===----------------------------------------------------------------------===// |
| 6745 | |
| 6746 | namespace { |
| 6747 | class LanaiABIInfo : public DefaultABIInfo { |
| 6748 | public: |
| 6749 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 6750 | |
| 6751 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 6752 | |
| 6753 | void computeInfo(CGFunctionInfo &FI) const override { |
| 6754 | CCState State(FI.getCallingConvention()); |
| 6755 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 6756 | // regparm attribute set. |
| 6757 | if (FI.getHasRegParm()) { |
| 6758 | State.FreeRegs = FI.getRegParm(); |
| 6759 | } else { |
| 6760 | State.FreeRegs = 4; |
| 6761 | } |
| 6762 | |
| 6763 | if (!getCXXABI().classifyReturnType(FI)) |
| 6764 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 6765 | for (auto &I : FI.arguments()) |
| 6766 | I.info = classifyArgumentType(I.type, State); |
| 6767 | } |
| 6768 | |
| 6769 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 6770 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 6771 | }; |
| 6772 | } // end anonymous namespace |
| 6773 | |
| 6774 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 6775 | unsigned Size = getContext().getTypeSize(Ty); |
| 6776 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 6777 | |
| 6778 | if (SizeInRegs == 0) |
| 6779 | return false; |
| 6780 | |
| 6781 | if (SizeInRegs > State.FreeRegs) { |
| 6782 | State.FreeRegs = 0; |
| 6783 | return false; |
| 6784 | } |
| 6785 | |
| 6786 | State.FreeRegs -= SizeInRegs; |
| 6787 | |
| 6788 | return true; |
| 6789 | } |
| 6790 | |
| 6791 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 6792 | CCState &State) const { |
| 6793 | if (!ByVal) { |
| 6794 | if (State.FreeRegs) { |
| 6795 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 6796 | return getNaturalAlignIndirectInReg(Ty); |
| 6797 | } |
| 6798 | return getNaturalAlignIndirect(Ty, false); |
| 6799 | } |
| 6800 | |
| 6801 | // Compute the byval alignment. |
| 6802 | const unsigned MinABIStackAlignInBytes = 4; |
| 6803 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 6804 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 6805 | /*Realign=*/TypeAlign > |
| 6806 | MinABIStackAlignInBytes); |
| 6807 | } |
| 6808 | |
| 6809 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 6810 | CCState &State) const { |
| 6811 | // Check with the C++ ABI first. |
| 6812 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 6813 | if (RT) { |
| 6814 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 6815 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 6816 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 6817 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 6818 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 6819 | } |
| 6820 | } |
| 6821 | |
| 6822 | if (isAggregateTypeForABI(Ty)) { |
| 6823 | // Structures with flexible arrays are always indirect. |
| 6824 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 6825 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 6826 | |
| 6827 | // Ignore empty structs/unions. |
| 6828 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6829 | return ABIArgInfo::getIgnore(); |
| 6830 | |
| 6831 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 6832 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 6833 | if (SizeInRegs <= State.FreeRegs) { |
| 6834 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 6835 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 6836 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 6837 | State.FreeRegs -= SizeInRegs; |
| 6838 | return ABIArgInfo::getDirectInReg(Result); |
| 6839 | } else { |
| 6840 | State.FreeRegs = 0; |
| 6841 | } |
| 6842 | return getIndirectResult(Ty, true, State); |
| 6843 | } |
| 6844 | |
| 6845 | // Treat an enum type as its underlying type. |
| 6846 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 6847 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6848 | |
| 6849 | bool InReg = shouldUseInReg(Ty, State); |
| 6850 | if (Ty->isPromotableIntegerType()) { |
| 6851 | if (InReg) |
| 6852 | return ABIArgInfo::getDirectInReg(); |
| 6853 | return ABIArgInfo::getExtend(); |
| 6854 | } |
| 6855 | if (InReg) |
| 6856 | return ABIArgInfo::getDirectInReg(); |
| 6857 | return ABIArgInfo::getDirect(); |
| 6858 | } |
| 6859 | |
| 6860 | namespace { |
| 6861 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6862 | public: |
| 6863 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 6864 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 6865 | }; |
| 6866 | } |
| 6867 | |
| 6868 | //===----------------------------------------------------------------------===// |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6869 | // AMDGPU ABI Implementation |
| 6870 | //===----------------------------------------------------------------------===// |
| 6871 | |
| 6872 | namespace { |
| 6873 | |
| 6874 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6875 | public: |
| 6876 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6877 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6878 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6879 | CodeGen::CodeGenModule &M) const override; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 6880 | unsigned getOpenCLKernelCallingConv() const override; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6881 | }; |
| 6882 | |
| 6883 | } |
| 6884 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 6885 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6886 | const Decl *D, |
| 6887 | llvm::GlobalValue *GV, |
| 6888 | CodeGen::CodeGenModule &M) const { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6889 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 6890 | if (!FD) |
| 6891 | return; |
| 6892 | |
| 6893 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6894 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6895 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6896 | if (NumVGPR != 0) |
| 6897 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6898 | } |
| 6899 | |
| 6900 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6901 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6902 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6903 | if (NumSGPR != 0) |
| 6904 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6905 | } |
| 6906 | } |
| 6907 | |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6908 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 6909 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 6910 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 6911 | } |
| 6912 | |
| 6913 | //===----------------------------------------------------------------------===// |
| 6914 | // SPARC v8 ABI Implementation. |
| 6915 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6916 | // |
| 6917 | // Ensures that complex values are passed in registers. |
| 6918 | // |
| 6919 | namespace { |
| 6920 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 6921 | public: |
| 6922 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 6923 | |
| 6924 | private: |
| 6925 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6926 | void computeInfo(CGFunctionInfo &FI) const override; |
| 6927 | }; |
| 6928 | } // end anonymous namespace |
| 6929 | |
| 6930 | |
| 6931 | ABIArgInfo |
| 6932 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 6933 | if (Ty->isAnyComplexType()) { |
| 6934 | return ABIArgInfo::getDirect(); |
| 6935 | } |
| 6936 | else { |
| 6937 | return DefaultABIInfo::classifyReturnType(Ty); |
| 6938 | } |
| 6939 | } |
| 6940 | |
| 6941 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6942 | |
| 6943 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 6944 | for (auto &Arg : FI.arguments()) |
| 6945 | Arg.info = classifyArgumentType(Arg.type); |
| 6946 | } |
| 6947 | |
| 6948 | namespace { |
| 6949 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6950 | public: |
| 6951 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6952 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 6953 | }; |
| 6954 | } // end anonymous namespace |
| 6955 | |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6956 | //===----------------------------------------------------------------------===// |
| 6957 | // SPARC v9 ABI Implementation. |
| 6958 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6959 | // |
| 6960 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6961 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6962 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6963 | // |
| 6964 | // One case requires special care: |
| 6965 | // |
| 6966 | // struct mixed { |
| 6967 | // int i; |
| 6968 | // float f; |
| 6969 | // }; |
| 6970 | // |
| 6971 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6972 | // parameter array, but the int is passed in an integer register, and the float |
| 6973 | // is passed in a floating point register. This is represented as two arguments |
| 6974 | // with the LLVM IR inreg attribute: |
| 6975 | // |
| 6976 | // declare void f(i32 inreg %i, float inreg %f) |
| 6977 | // |
| 6978 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6979 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6980 | // bytes. |
| 6981 | // |
| 6982 | namespace { |
| 6983 | class SparcV9ABIInfo : public ABIInfo { |
| 6984 | public: |
| 6985 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6986 | |
| 6987 | private: |
| 6988 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6989 | void computeInfo(CGFunctionInfo &FI) const override; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 6990 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6991 | QualType Ty) const override; |
Jakob Stoklund Olesen | fc782fb | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6992 | |
| 6993 | // Coercion type builder for structs passed in registers. The coercion type |
| 6994 | // serves two purposes: |
| 6995 | // |
| 6996 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6997 | // in registers. |
| 6998 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6999 | // code generator knows to pass them in floating point registers. |
| 7000 | // |
| 7001 | // We also compute the InReg flag which indicates that the struct contains |
| 7002 | // aligned 32-bit floats. |
| 7003 | // |
| 7004 | struct CoerceBuilder { |
| 7005 | llvm::LLVMContext &Context; |
| 7006 | const llvm::DataLayout &DL; |
| 7007 | SmallVector<llvm::Type*, 8> Elems; |
| 7008 | uint64_t Size; |
| 7009 | bool InReg; |
| 7010 | |
| 7011 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 7012 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 7013 | |
| 7014 | // Pad Elems with integers until Size is ToSize. |
| 7015 | void pad(uint64_t ToSize) { |
| 7016 | assert(ToSize >= Size && "Cannot remove elements"); |
| 7017 | if (ToSize == Size) |
| 7018 | return; |
| 7019 | |
| 7020 | // Finish the current 64-bit word. |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7021 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | fc782fb | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7022 | if (Aligned > Size && Aligned <= ToSize) { |
| 7023 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 7024 | Size = Aligned; |
| 7025 | } |
| 7026 | |
| 7027 | // Add whole 64-bit words. |
| 7028 | while (Size + 64 <= ToSize) { |
| 7029 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 7030 | Size += 64; |
| 7031 | } |
| 7032 | |
| 7033 | // Final in-word padding. |
| 7034 | if (Size < ToSize) { |
| 7035 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 7036 | Size = ToSize; |
| 7037 | } |
| 7038 | } |
| 7039 | |
| 7040 | // Add a floating point element at Offset. |
| 7041 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 7042 | // Unaligned floats are treated as integers. |
| 7043 | if (Offset % Bits) |
| 7044 | return; |
| 7045 | // The InReg flag is only required if there are any floats < 64 bits. |
| 7046 | if (Bits < 64) |
| 7047 | InReg = true; |
| 7048 | pad(Offset); |
| 7049 | Elems.push_back(Ty); |
| 7050 | Size = Offset + Bits; |
| 7051 | } |
| 7052 | |
| 7053 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 7054 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 7055 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 7056 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 7057 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 7058 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 7059 | switch (ElemTy->getTypeID()) { |
| 7060 | case llvm::Type::StructTyID: |
| 7061 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 7062 | break; |
| 7063 | case llvm::Type::FloatTyID: |
| 7064 | addFloat(ElemOffset, ElemTy, 32); |
| 7065 | break; |
| 7066 | case llvm::Type::DoubleTyID: |
| 7067 | addFloat(ElemOffset, ElemTy, 64); |
| 7068 | break; |
| 7069 | case llvm::Type::FP128TyID: |
| 7070 | addFloat(ElemOffset, ElemTy, 128); |
| 7071 | break; |
| 7072 | case llvm::Type::PointerTyID: |
| 7073 | if (ElemOffset % 64 == 0) { |
| 7074 | pad(ElemOffset); |
| 7075 | Elems.push_back(ElemTy); |
| 7076 | Size += 64; |
| 7077 | } |
| 7078 | break; |
| 7079 | default: |
| 7080 | break; |
| 7081 | } |
| 7082 | } |
| 7083 | } |
| 7084 | |
| 7085 | // Check if Ty is a usable substitute for the coercion type. |
| 7086 | bool isUsableType(llvm::StructType *Ty) const { |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 7087 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | fc782fb | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7088 | } |
| 7089 | |
| 7090 | // Get the coercion type as a literal struct type. |
| 7091 | llvm::Type *getType() const { |
| 7092 | if (Elems.size() == 1) |
| 7093 | return Elems.front(); |
| 7094 | else |
| 7095 | return llvm::StructType::get(Context, Elems); |
| 7096 | } |
| 7097 | }; |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7098 | }; |
| 7099 | } // end anonymous namespace |
| 7100 | |
| 7101 | ABIArgInfo |
| 7102 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 7103 | if (Ty->isVoidType()) |
| 7104 | return ABIArgInfo::getIgnore(); |
| 7105 | |
| 7106 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7107 | |
| 7108 | // Anything too big to fit in registers is passed with an explicit indirect |
| 7109 | // pointer / sret pointer. |
| 7110 | if (Size > SizeLimit) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7111 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7112 | |
| 7113 | // Treat an enum type as its underlying type. |
| 7114 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7115 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7116 | |
| 7117 | // Integer types smaller than a register are extended. |
| 7118 | if (Size < 64 && Ty->isIntegerType()) |
| 7119 | return ABIArgInfo::getExtend(); |
| 7120 | |
| 7121 | // Other non-aggregates go in registers. |
| 7122 | if (!isAggregateTypeForABI(Ty)) |
| 7123 | return ABIArgInfo::getDirect(); |
| 7124 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7125 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 7126 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 7127 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7128 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7129 | |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7130 | // This is a small aggregate type that should be passed in registers. |
Jakob Stoklund Olesen | fc782fb | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7131 | // Build a coercion type from the LLVM struct type. |
| 7132 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 7133 | if (!StrTy) |
| 7134 | return ABIArgInfo::getDirect(); |
| 7135 | |
| 7136 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 7137 | CB.addStruct(0, StrTy); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7138 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | fc782fb | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7139 | |
| 7140 | // Try to use the original type for coercion. |
| 7141 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 7142 | |
| 7143 | if (CB.InReg) |
| 7144 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 7145 | else |
| 7146 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7147 | } |
| 7148 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7149 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7150 | QualType Ty) const { |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7151 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 7152 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7153 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7154 | AI.setCoerceToType(ArgTy); |
| 7155 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7156 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7157 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7158 | CGBuilderTy &Builder = CGF.Builder; |
| 7159 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 7160 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 7161 | |
| 7162 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 7163 | |
| 7164 | Address ArgAddr = Address::invalid(); |
| 7165 | CharUnits Stride; |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7166 | switch (AI.getKind()) { |
| 7167 | case ABIArgInfo::Expand: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7168 | case ABIArgInfo::CoerceAndExpand: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7169 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7170 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 7171 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7172 | case ABIArgInfo::Extend: { |
| 7173 | Stride = SlotSize; |
| 7174 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 7175 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7176 | break; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7177 | } |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7178 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7179 | case ABIArgInfo::Direct: { |
| 7180 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7181 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7182 | ArgAddr = Addr; |
| 7183 | break; |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7184 | } |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7185 | |
| 7186 | case ABIArgInfo::Indirect: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7187 | Stride = SlotSize; |
| 7188 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 7189 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 7190 | TypeInfo.second); |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7191 | break; |
| 7192 | |
| 7193 | case ABIArgInfo::Ignore: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7194 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7195 | } |
| 7196 | |
| 7197 | // Update VAList. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7198 | llvm::Value *NextPtr = |
| 7199 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 7200 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | a4b56d3 | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 7201 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7202 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7203 | } |
| 7204 | |
| 7205 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7206 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7207 | for (auto &I : FI.arguments()) |
| 7208 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7209 | } |
| 7210 | |
| 7211 | namespace { |
| 7212 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7213 | public: |
| 7214 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7215 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7216 | |
| 7217 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 7218 | return 14; |
| 7219 | } |
| 7220 | |
| 7221 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7222 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7223 | }; |
| 7224 | } // end anonymous namespace |
| 7225 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7226 | bool |
| 7227 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7228 | llvm::Value *Address) const { |
| 7229 | // This is calculated from the LLVM and GCC tables and verified |
| 7230 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 7231 | |
| 7232 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 7233 | |
| 7234 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 7235 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 7236 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 7237 | |
| 7238 | // 0-31: the 8-byte general-purpose registers |
| 7239 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 7240 | |
| 7241 | // 32-63: f0-31, the 4-byte floating-point registers |
| 7242 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 7243 | |
| 7244 | // Y = 64 |
| 7245 | // PSR = 65 |
| 7246 | // WIM = 66 |
| 7247 | // TBR = 67 |
| 7248 | // PC = 68 |
| 7249 | // NPC = 69 |
| 7250 | // FSR = 70 |
| 7251 | // CSR = 71 |
| 7252 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 7253 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7254 | // 72-87: d0-15, the 8-byte floating-point registers |
| 7255 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 7256 | |
| 7257 | return false; |
| 7258 | } |
| 7259 | |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7260 | |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7261 | //===----------------------------------------------------------------------===// |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7262 | // XCore ABI Implementation |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7263 | //===----------------------------------------------------------------------===// |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7264 | |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7265 | namespace { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7266 | |
| 7267 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 7268 | /// it by reference between functions that append to it. |
| 7269 | typedef llvm::SmallString<128> SmallStringEnc; |
| 7270 | |
| 7271 | /// TypeStringCache caches the meta encodings of Types. |
| 7272 | /// |
| 7273 | /// The reason for caching TypeStrings is two fold: |
| 7274 | /// 1. To cache a type's encoding for later uses; |
| 7275 | /// 2. As a means to break recursive member type inclusion. |
| 7276 | /// |
| 7277 | /// A cache Entry can have a Status of: |
| 7278 | /// NonRecursive: The type encoding is not recursive; |
| 7279 | /// Recursive: The type encoding is recursive; |
| 7280 | /// Incomplete: An incomplete TypeString; |
| 7281 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 7282 | /// Recursive type encoding. |
| 7283 | /// |
| 7284 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 7285 | /// as possible. Whilst it may contain types which are recursive, the type |
| 7286 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 7287 | /// the type is encountered. |
| 7288 | /// |
| 7289 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 7290 | /// possible. The type itself is recursive and it may contain other types which |
| 7291 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 7292 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 7293 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 7294 | /// |
| 7295 | /// An Incomplete entry is always a RecordType and only encodes its |
| 7296 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 7297 | /// are placed into the cache during type expansion as a means to identify and |
| 7298 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 7299 | /// the entry becomes IncompleteUsed. |
| 7300 | /// |
| 7301 | /// During the expansion of a RecordType's members: |
| 7302 | /// |
| 7303 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 7304 | /// cached encoding is used; |
| 7305 | /// |
| 7306 | /// If the cache contains a Recursive encoding for the member type, the |
| 7307 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 7308 | /// |
| 7309 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 7310 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 7311 | /// |
| 7312 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 7313 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 7314 | /// it is swapped back in; |
| 7315 | /// |
| 7316 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 7317 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 7318 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 7319 | /// |
| 7320 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 7321 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 7322 | /// Else the member is part of a recursive type and thus the recursion has |
| 7323 | /// been exited too soon for the encoding to be correct for the member. |
| 7324 | /// |
| 7325 | class TypeStringCache { |
| 7326 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 7327 | struct Entry { |
| 7328 | std::string Str; // The encoded TypeString for the type. |
| 7329 | enum Status State; // Information about the encoding in 'Str'. |
| 7330 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 7331 | // during the expansion of RecordType's members. |
| 7332 | }; |
| 7333 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 7334 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 7335 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 7336 | public: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7337 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7338 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 7339 | bool removeIncomplete(const IdentifierInfo *ID); |
| 7340 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7341 | bool IsRecursive); |
| 7342 | StringRef lookupStr(const IdentifierInfo *ID); |
| 7343 | }; |
| 7344 | |
| 7345 | /// TypeString encodings for enum & union fields must be order. |
| 7346 | /// FieldEncoding is a helper for this ordering process. |
| 7347 | class FieldEncoding { |
| 7348 | bool HasName; |
| 7349 | std::string Enc; |
| 7350 | public: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7351 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
| 7352 | StringRef str() {return Enc.c_str();} |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7353 | bool operator<(const FieldEncoding &rhs) const { |
| 7354 | if (HasName != rhs.HasName) return HasName; |
| 7355 | return Enc < rhs.Enc; |
| 7356 | } |
| 7357 | }; |
| 7358 | |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7359 | class XCoreABIInfo : public DefaultABIInfo { |
| 7360 | public: |
| 7361 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7362 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7363 | QualType Ty) const override; |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7364 | }; |
| 7365 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7366 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7367 | mutable TypeStringCache TSC; |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7368 | public: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7369 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7370 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7371 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7372 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7373 | }; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7374 | |
Robert Lytton | 645e6fd | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7375 | } // End anonymous namespace. |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7376 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7377 | // TODO: this implementation is likely now redundant with the default |
| 7378 | // EmitVAArg. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7379 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7380 | QualType Ty) const { |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7381 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7382 | |
Robert Lytton | 645e6fd | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7383 | // Get the VAList. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7384 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 7385 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7386 | |
Robert Lytton | 645e6fd | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7387 | // Handle the argument. |
| 7388 | ABIArgInfo AI = classifyArgumentType(Ty); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7389 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 645e6fd | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7390 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 7391 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 7392 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7393 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7394 | |
| 7395 | Address Val = Address::invalid(); |
| 7396 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7397 | switch (AI.getKind()) { |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7398 | case ABIArgInfo::Expand: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7399 | case ABIArgInfo::CoerceAndExpand: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7400 | case ABIArgInfo::InAlloca: |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7401 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 7402 | case ABIArgInfo::Ignore: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7403 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 7404 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 645e6fd | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7405 | break; |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7406 | case ABIArgInfo::Extend: |
| 7407 | case ABIArgInfo::Direct: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7408 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 7409 | ArgSize = CharUnits::fromQuantity( |
| 7410 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7411 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 645e6fd | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7412 | break; |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7413 | case ABIArgInfo::Indirect: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7414 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 7415 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 7416 | ArgSize = SlotSize; |
Robert Lytton | 645e6fd | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7417 | break; |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7418 | } |
Robert Lytton | 645e6fd | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7419 | |
| 7420 | // Increment the VAList. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7421 | if (!ArgSize.isZero()) { |
| 7422 | llvm::Value *APN = |
| 7423 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 7424 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 645e6fd | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7425 | } |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7426 | |
Robert Lytton | 645e6fd | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7427 | return Val; |
Robert Lytton | 276c289 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7428 | } |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7429 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7430 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 7431 | /// into the cache as a means to identify and break recursion. |
| 7432 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 7433 | /// be reinserted by removeIncomplete(). |
| 7434 | /// All other types of encoding should have been used rather than arriving here. |
| 7435 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 7436 | std::string StubEnc) { |
| 7437 | if (!ID) |
| 7438 | return; |
| 7439 | Entry &E = Map[ID]; |
| 7440 | assert( (E.Str.empty() || E.State == Recursive) && |
| 7441 | "Incorrectly use of addIncomplete"); |
| 7442 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 7443 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 7444 | E.Str.swap(StubEnc); |
| 7445 | E.State = Incomplete; |
| 7446 | ++IncompleteCount; |
| 7447 | } |
| 7448 | |
| 7449 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 7450 | /// must be removed from the cache. |
| 7451 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 7452 | /// Returns true if the RecordType was defined recursively. |
| 7453 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 7454 | if (!ID) |
| 7455 | return false; |
| 7456 | auto I = Map.find(ID); |
| 7457 | assert(I != Map.end() && "Entry not present"); |
| 7458 | Entry &E = I->second; |
| 7459 | assert( (E.State == Incomplete || |
| 7460 | E.State == IncompleteUsed) && |
| 7461 | "Entry must be an incomplete type"); |
| 7462 | bool IsRecursive = false; |
| 7463 | if (E.State == IncompleteUsed) { |
| 7464 | // We made use of our Incomplete encoding, thus we are recursive. |
| 7465 | IsRecursive = true; |
| 7466 | --IncompleteUsedCount; |
| 7467 | } |
| 7468 | if (E.Swapped.empty()) |
| 7469 | Map.erase(I); |
| 7470 | else { |
| 7471 | // Swap the Recursive back. |
| 7472 | E.Swapped.swap(E.Str); |
| 7473 | E.Swapped.clear(); |
| 7474 | E.State = Recursive; |
| 7475 | } |
| 7476 | --IncompleteCount; |
| 7477 | return IsRecursive; |
| 7478 | } |
| 7479 | |
| 7480 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 7481 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 7482 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7483 | bool IsRecursive) { |
| 7484 | if (!ID || IncompleteUsedCount) |
| 7485 | return; // No key or it is is an incomplete sub-type so don't add. |
| 7486 | Entry &E = Map[ID]; |
| 7487 | if (IsRecursive && !E.Str.empty()) { |
| 7488 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 7489 | "This is not the same Recursive entry"); |
| 7490 | // The parent container was not recursive after all, so we could have used |
| 7491 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 7492 | // we started viz: IncompleteCount!=0. |
| 7493 | return; |
| 7494 | } |
| 7495 | assert(E.Str.empty() && "Entry already present"); |
| 7496 | E.Str = Str.str(); |
| 7497 | E.State = IsRecursive? Recursive : NonRecursive; |
| 7498 | } |
| 7499 | |
| 7500 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 7501 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 7502 | /// encoding is Recursive, return an empty StringRef. |
| 7503 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 7504 | if (!ID) |
| 7505 | return StringRef(); // We have no key. |
| 7506 | auto I = Map.find(ID); |
| 7507 | if (I == Map.end()) |
| 7508 | return StringRef(); // We have no encoding. |
| 7509 | Entry &E = I->second; |
| 7510 | if (E.State == Recursive && IncompleteCount) |
| 7511 | return StringRef(); // We don't use Recursive encodings for member types. |
| 7512 | |
| 7513 | if (E.State == Incomplete) { |
| 7514 | // The incomplete type is being used to break out of recursion. |
| 7515 | E.State = IncompleteUsed; |
| 7516 | ++IncompleteUsedCount; |
| 7517 | } |
| 7518 | return E.Str.c_str(); |
| 7519 | } |
| 7520 | |
| 7521 | /// The XCore ABI includes a type information section that communicates symbol |
| 7522 | /// type information to the linker. The linker uses this information to verify |
| 7523 | /// safety/correctness of things such as array bound and pointers et al. |
| 7524 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 7525 | /// This type information (TypeString) is emitted into meta data for all global |
| 7526 | /// symbols: definitions, declarations, functions & variables. |
| 7527 | /// |
| 7528 | /// The TypeString carries type, qualifier, name, size & value details. |
| 7529 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 7530 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7531 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 7532 | /// |
| 7533 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7534 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 7535 | |
| 7536 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 7537 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7538 | CodeGen::CodeGenModule &CGM) const { |
| 7539 | SmallStringEnc Enc; |
| 7540 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 7541 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7542 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 7543 | llvm::MDString::get(Ctx, Enc.str())}; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7544 | llvm::NamedMDNode *MD = |
| 7545 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 7546 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 7547 | } |
| 7548 | } |
| 7549 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7550 | //===----------------------------------------------------------------------===// |
| 7551 | // SPIR ABI Implementation |
| 7552 | //===----------------------------------------------------------------------===// |
| 7553 | |
| 7554 | namespace { |
| 7555 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7556 | public: |
| 7557 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7558 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
| 7559 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7560 | CodeGen::CodeGenModule &M) const override; |
| 7561 | unsigned getOpenCLKernelCallingConv() const override; |
| 7562 | }; |
| 7563 | } // End anonymous namespace. |
| 7564 | |
| 7565 | /// Emit SPIR specific metadata: OpenCL and SPIR version. |
| 7566 | void SPIRTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7567 | CodeGen::CodeGenModule &CGM) const { |
| 7568 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
| 7569 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx); |
| 7570 | llvm::Module &M = CGM.getModule(); |
| 7571 | // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the |
| 7572 | // opencl.spir.version named metadata. |
| 7573 | llvm::Metadata *SPIRVerElts[] = { |
| 7574 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 2)), |
| 7575 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 0))}; |
| 7576 | llvm::NamedMDNode *SPIRVerMD = |
| 7577 | M.getOrInsertNamedMetadata("opencl.spir.version"); |
| 7578 | SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts)); |
| 7579 | // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the |
| 7580 | // opencl.ocl.version named metadata node. |
| 7581 | llvm::Metadata *OCLVerElts[] = { |
| 7582 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 7583 | Int32Ty, CGM.getLangOpts().OpenCLVersion / 100)), |
| 7584 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 7585 | Int32Ty, (CGM.getLangOpts().OpenCLVersion % 100) / 10))}; |
| 7586 | llvm::NamedMDNode *OCLVerMD = |
| 7587 | M.getOrInsertNamedMetadata("opencl.ocl.version"); |
| 7588 | OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts)); |
| 7589 | } |
| 7590 | |
| 7591 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7592 | return llvm::CallingConv::SPIR_KERNEL; |
| 7593 | } |
| 7594 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7595 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7596 | const CodeGen::CodeGenModule &CGM, |
| 7597 | TypeStringCache &TSC); |
| 7598 | |
| 7599 | /// Helper function for appendRecordType(). |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 7600 | /// Builds a SmallVector containing the encoded field types in declaration |
| 7601 | /// order. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7602 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 7603 | const RecordDecl *RD, |
| 7604 | const CodeGen::CodeGenModule &CGM, |
| 7605 | TypeStringCache &TSC) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7606 | for (const auto *Field : RD->fields()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7607 | SmallStringEnc Enc; |
| 7608 | Enc += "m("; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7609 | Enc += Field->getName(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7610 | Enc += "){"; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7611 | if (Field->isBitField()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7612 | Enc += "b("; |
| 7613 | llvm::raw_svector_ostream OS(Enc); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7614 | OS << Field->getBitWidthValue(CGM.getContext()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7615 | Enc += ':'; |
| 7616 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7617 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7618 | return false; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7619 | if (Field->isBitField()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7620 | Enc += ')'; |
| 7621 | Enc += '}'; |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 7622 | FE.emplace_back(!Field->getName().empty(), Enc); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7623 | } |
| 7624 | return true; |
| 7625 | } |
| 7626 | |
| 7627 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 7628 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 7629 | /// Union types have their fields ordered according to the ABI. |
| 7630 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 7631 | const CodeGen::CodeGenModule &CGM, |
| 7632 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 7633 | // Append the cached TypeString if we have one. |
| 7634 | StringRef TypeString = TSC.lookupStr(ID); |
| 7635 | if (!TypeString.empty()) { |
| 7636 | Enc += TypeString; |
| 7637 | return true; |
| 7638 | } |
| 7639 | |
| 7640 | // Start to emit an incomplete TypeString. |
| 7641 | size_t Start = Enc.size(); |
| 7642 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 7643 | Enc += '('; |
| 7644 | if (ID) |
| 7645 | Enc += ID->getName(); |
| 7646 | Enc += "){"; |
| 7647 | |
| 7648 | // We collect all encoded fields and order as necessary. |
| 7649 | bool IsRecursive = false; |
| 7650 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 7651 | if (RD && !RD->field_empty()) { |
| 7652 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 7653 | // so that recursive calls to this RecordType will use it whilst building a |
| 7654 | // complete TypeString for this RecordType. |
| 7655 | SmallVector<FieldEncoding, 16> FE; |
| 7656 | std::string StubEnc(Enc.substr(Start).str()); |
| 7657 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 7658 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 7659 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 7660 | (void) TSC.removeIncomplete(ID); |
| 7661 | return false; |
| 7662 | } |
| 7663 | IsRecursive = TSC.removeIncomplete(ID); |
| 7664 | // The ABI requires unions to be sorted but not structures. |
| 7665 | // See FieldEncoding::operator< for sort algorithm. |
| 7666 | if (RT->isUnionType()) |
| 7667 | std::sort(FE.begin(), FE.end()); |
| 7668 | // We can now complete the TypeString. |
| 7669 | unsigned E = FE.size(); |
| 7670 | for (unsigned I = 0; I != E; ++I) { |
| 7671 | if (I) |
| 7672 | Enc += ','; |
| 7673 | Enc += FE[I].str(); |
| 7674 | } |
| 7675 | } |
| 7676 | Enc += '}'; |
| 7677 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 7678 | return true; |
| 7679 | } |
| 7680 | |
| 7681 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 7682 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 7683 | TypeStringCache &TSC, |
| 7684 | const IdentifierInfo *ID) { |
| 7685 | // Append the cached TypeString if we have one. |
| 7686 | StringRef TypeString = TSC.lookupStr(ID); |
| 7687 | if (!TypeString.empty()) { |
| 7688 | Enc += TypeString; |
| 7689 | return true; |
| 7690 | } |
| 7691 | |
| 7692 | size_t Start = Enc.size(); |
| 7693 | Enc += "e("; |
| 7694 | if (ID) |
| 7695 | Enc += ID->getName(); |
| 7696 | Enc += "){"; |
| 7697 | |
| 7698 | // We collect all encoded enumerations and order them alphanumerically. |
| 7699 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
| 7700 | SmallVector<FieldEncoding, 16> FE; |
| 7701 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 7702 | ++I) { |
| 7703 | SmallStringEnc EnumEnc; |
| 7704 | EnumEnc += "m("; |
| 7705 | EnumEnc += I->getName(); |
| 7706 | EnumEnc += "){"; |
| 7707 | I->getInitVal().toString(EnumEnc); |
| 7708 | EnumEnc += '}'; |
| 7709 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 7710 | } |
| 7711 | std::sort(FE.begin(), FE.end()); |
| 7712 | unsigned E = FE.size(); |
| 7713 | for (unsigned I = 0; I != E; ++I) { |
| 7714 | if (I) |
| 7715 | Enc += ','; |
| 7716 | Enc += FE[I].str(); |
| 7717 | } |
| 7718 | } |
| 7719 | Enc += '}'; |
| 7720 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 7721 | return true; |
| 7722 | } |
| 7723 | |
| 7724 | /// Appends type's qualifier to Enc. |
| 7725 | /// This is done prior to appending the type's encoding. |
| 7726 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 7727 | // Qualifiers are emitted in alphabetical order. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7728 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7729 | int Lookup = 0; |
| 7730 | if (QT.isConstQualified()) |
| 7731 | Lookup += 1<<0; |
| 7732 | if (QT.isRestrictQualified()) |
| 7733 | Lookup += 1<<1; |
| 7734 | if (QT.isVolatileQualified()) |
| 7735 | Lookup += 1<<2; |
| 7736 | Enc += Table[Lookup]; |
| 7737 | } |
| 7738 | |
| 7739 | /// Appends built-in types to Enc. |
| 7740 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 7741 | const char *EncType; |
| 7742 | switch (BT->getKind()) { |
| 7743 | case BuiltinType::Void: |
| 7744 | EncType = "0"; |
| 7745 | break; |
| 7746 | case BuiltinType::Bool: |
| 7747 | EncType = "b"; |
| 7748 | break; |
| 7749 | case BuiltinType::Char_U: |
| 7750 | EncType = "uc"; |
| 7751 | break; |
| 7752 | case BuiltinType::UChar: |
| 7753 | EncType = "uc"; |
| 7754 | break; |
| 7755 | case BuiltinType::SChar: |
| 7756 | EncType = "sc"; |
| 7757 | break; |
| 7758 | case BuiltinType::UShort: |
| 7759 | EncType = "us"; |
| 7760 | break; |
| 7761 | case BuiltinType::Short: |
| 7762 | EncType = "ss"; |
| 7763 | break; |
| 7764 | case BuiltinType::UInt: |
| 7765 | EncType = "ui"; |
| 7766 | break; |
| 7767 | case BuiltinType::Int: |
| 7768 | EncType = "si"; |
| 7769 | break; |
| 7770 | case BuiltinType::ULong: |
| 7771 | EncType = "ul"; |
| 7772 | break; |
| 7773 | case BuiltinType::Long: |
| 7774 | EncType = "sl"; |
| 7775 | break; |
| 7776 | case BuiltinType::ULongLong: |
| 7777 | EncType = "ull"; |
| 7778 | break; |
| 7779 | case BuiltinType::LongLong: |
| 7780 | EncType = "sll"; |
| 7781 | break; |
| 7782 | case BuiltinType::Float: |
| 7783 | EncType = "ft"; |
| 7784 | break; |
| 7785 | case BuiltinType::Double: |
| 7786 | EncType = "d"; |
| 7787 | break; |
| 7788 | case BuiltinType::LongDouble: |
| 7789 | EncType = "ld"; |
| 7790 | break; |
| 7791 | default: |
| 7792 | return false; |
| 7793 | } |
| 7794 | Enc += EncType; |
| 7795 | return true; |
| 7796 | } |
| 7797 | |
| 7798 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 7799 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 7800 | const CodeGen::CodeGenModule &CGM, |
| 7801 | TypeStringCache &TSC) { |
| 7802 | Enc += "p("; |
| 7803 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 7804 | return false; |
| 7805 | Enc += ')'; |
| 7806 | return true; |
| 7807 | } |
| 7808 | |
| 7809 | /// Appends array encoding to Enc before calling appendType for the element. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 7810 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 7811 | const ArrayType *AT, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7812 | const CodeGen::CodeGenModule &CGM, |
| 7813 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 7814 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 7815 | return false; |
| 7816 | Enc += "a("; |
| 7817 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 7818 | CAT->getSize().toStringUnsigned(Enc); |
| 7819 | else |
| 7820 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 7821 | Enc += ':'; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 7822 | // The Qualifiers should be attached to the type rather than the array. |
| 7823 | appendQualifier(Enc, QT); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7824 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 7825 | return false; |
| 7826 | Enc += ')'; |
| 7827 | return true; |
| 7828 | } |
| 7829 | |
| 7830 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 7831 | /// and the arguments. |
| 7832 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 7833 | const CodeGen::CodeGenModule &CGM, |
| 7834 | TypeStringCache &TSC) { |
| 7835 | Enc += "f{"; |
| 7836 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 7837 | return false; |
| 7838 | Enc += "}("; |
| 7839 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 7840 | // N.B. we are only interested in the adjusted param types. |
| 7841 | auto I = FPT->param_type_begin(); |
| 7842 | auto E = FPT->param_type_end(); |
| 7843 | if (I != E) { |
| 7844 | do { |
| 7845 | if (!appendType(Enc, *I, CGM, TSC)) |
| 7846 | return false; |
| 7847 | ++I; |
| 7848 | if (I != E) |
| 7849 | Enc += ','; |
| 7850 | } while (I != E); |
| 7851 | if (FPT->isVariadic()) |
| 7852 | Enc += ",va"; |
| 7853 | } else { |
| 7854 | if (FPT->isVariadic()) |
| 7855 | Enc += "va"; |
| 7856 | else |
| 7857 | Enc += '0'; |
| 7858 | } |
| 7859 | } |
| 7860 | Enc += ')'; |
| 7861 | return true; |
| 7862 | } |
| 7863 | |
| 7864 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 7865 | /// type encodings. |
| 7866 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7867 | const CodeGen::CodeGenModule &CGM, |
| 7868 | TypeStringCache &TSC) { |
| 7869 | |
| 7870 | QualType QT = QType.getCanonicalType(); |
| 7871 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 7872 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 7873 | // The Qualifiers should be attached to the type rather than the array. |
| 7874 | // Thus we don't call appendQualifier() here. |
| 7875 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 7876 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7877 | appendQualifier(Enc, QT); |
| 7878 | |
| 7879 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 7880 | return appendBuiltinType(Enc, BT); |
| 7881 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7882 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 7883 | return appendPointerType(Enc, PT, CGM, TSC); |
| 7884 | |
| 7885 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 7886 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 7887 | |
| 7888 | if (const RecordType *RT = QT->getAsStructureType()) |
| 7889 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7890 | |
| 7891 | if (const RecordType *RT = QT->getAsUnionType()) |
| 7892 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7893 | |
| 7894 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7895 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7896 | |
| 7897 | return false; |
| 7898 | } |
| 7899 | |
| 7900 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7901 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 7902 | if (!D) |
| 7903 | return false; |
| 7904 | |
| 7905 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7906 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 7907 | return false; |
| 7908 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 7909 | } |
| 7910 | |
| 7911 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7912 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 7913 | return false; |
| 7914 | QualType QT = VD->getType().getCanonicalType(); |
| 7915 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 7916 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 7917 | // The Qualifiers should be attached to the type rather than the array. |
| 7918 | // Thus we don't call appendQualifier() here. |
| 7919 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7920 | } |
| 7921 | return appendType(Enc, QT, CGM, TSC); |
| 7922 | } |
| 7923 | return false; |
| 7924 | } |
| 7925 | |
| 7926 | |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7927 | //===----------------------------------------------------------------------===// |
| 7928 | // Driver code |
| 7929 | //===----------------------------------------------------------------------===// |
| 7930 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7931 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 7932 | return getTarget().getTriple(); |
| 7933 | } |
| 7934 | |
| 7935 | bool CodeGenModule::supportsCOMDAT() const { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7936 | return getTriple().supportsCOMDAT(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7937 | } |
| 7938 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7939 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7940 | if (TheTargetCodeGenInfo) |
| 7941 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7942 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7943 | // Helper to set the unique_ptr while still keeping the return value. |
| 7944 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 7945 | this->TheTargetCodeGenInfo.reset(P); |
| 7946 | return *P; |
| 7947 | }; |
| 7948 | |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7949 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 1752ee4 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 7950 | switch (Triple.getArch()) { |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7951 | default: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7952 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7953 | |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 7954 | case llvm::Triple::le32: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7955 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7956 | case llvm::Triple::mips: |
| 7957 | case llvm::Triple::mipsel: |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7958 | if (Triple.getOS() == llvm::Triple::NaCl) |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7959 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 7960 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7961 | |
Akira Hatanaka | 8c6dfbe | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7962 | case llvm::Triple::mips64: |
| 7963 | case llvm::Triple::mips64el: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7964 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | 8c6dfbe | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7965 | |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7966 | case llvm::Triple::aarch64: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 7967 | case llvm::Triple::aarch64_be: { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7968 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 7969 | if (getTarget().getABI() == "darwinpcs") |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7970 | Kind = AArch64ABIInfo::DarwinPCS; |
| 7971 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7972 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 7973 | } |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7974 | |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7975 | case llvm::Triple::wasm32: |
| 7976 | case llvm::Triple::wasm64: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7977 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 7978 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7979 | case llvm::Triple::arm: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 7980 | case llvm::Triple::armeb: |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7981 | case llvm::Triple::thumb: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7982 | case llvm::Triple::thumbeb: { |
| 7983 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7984 | return SetCGInfo( |
| 7985 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7986 | } |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7987 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 7988 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 7989 | StringRef ABIStr = getTarget().getABI(); |
| 7990 | if (ABIStr == "apcs-gnu") |
| 7991 | Kind = ARMABIInfo::APCS; |
| 7992 | else if (ABIStr == "aapcs16") |
| 7993 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 7994 | else if (CodeGenOpts.FloatABI == "hard" || |
| 7995 | (CodeGenOpts.FloatABI != "soft" && |
| 7996 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
| 7997 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
| 7998 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
| 7999 | Kind = ARMABIInfo::AAPCS_VFP; |
| 8000 | |
| 8001 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 8002 | } |
| 8003 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8004 | case llvm::Triple::ppc: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8005 | return SetCGInfo( |
| 8006 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 8007 | case llvm::Triple::ppc64: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8008 | if (Triple.isOSBinFormatELF()) { |
| 8009 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
| 8010 | if (getTarget().getABI() == "elfv2") |
| 8011 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 8012 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8013 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8014 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8015 | } else |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8016 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8017 | case llvm::Triple::ppc64le: { |
Bill Schmidt | ea7fb0c | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 8018 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8019 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 8020 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8021 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 8022 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8023 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8024 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 8025 | } |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 8026 | |
Peter Collingbourne | edb66f3 | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 8027 | case llvm::Triple::nvptx: |
| 8028 | case llvm::Triple::nvptx64: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8029 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 8030 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 8031 | case llvm::Triple::msp430: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8032 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 8033 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 8034 | case llvm::Triple::systemz: { |
| 8035 | bool HasVector = getTarget().getABI() == "vector"; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8036 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 8037 | } |
Ulrich Weigand | b840921 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 8038 | |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8039 | case llvm::Triple::tce: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8040 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 8041 | |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8042 | case llvm::Triple::x86: { |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8043 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 8044 | bool RetSmallStructInRegABI = |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8045 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 8046 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | db57a4c | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 8047 | |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8048 | if (Triple.getOS() == llvm::Triple::Win32) { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8049 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 8050 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8051 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | b8b5297 | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 8052 | } else { |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8053 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 8054 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 8055 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 8056 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8057 | } |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 8058 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8059 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8060 | case llvm::Triple::x86_64: { |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 8061 | StringRef ABI = getTarget().getABI(); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8062 | X86AVXABILevel AVXLevel = |
| 8063 | (ABI == "avx512" |
| 8064 | ? X86AVXABILevel::AVX512 |
| 8065 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 8066 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8067 | switch (Triple.getOS()) { |
| 8068 | case llvm::Triple::Win32: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8069 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 8070 | case llvm::Triple::PS4: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8071 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8072 | default: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8073 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 8074 | } |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 8075 | } |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 8076 | case llvm::Triple::hexagon: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8077 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
| 8078 | case llvm::Triple::lanai: |
| 8079 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 8080 | case llvm::Triple::r600: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8081 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 8082 | case llvm::Triple::amdgcn: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8083 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
| 8084 | case llvm::Triple::sparc: |
| 8085 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | 107196c | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8086 | case llvm::Triple::sparcv9: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8087 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 5f15f4d | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8088 | case llvm::Triple::xcore: |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 8089 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
| 8090 | case llvm::Triple::spir: |
| 8091 | case llvm::Triple::spir64: |
| 8092 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 8093 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 8094 | } |