Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1 | //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // These classes wrap the information about a call or function |
| 11 | // definition used to handle ABI compliancy. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetInfo.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 16 | #include "ABIInfo.h" |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 18 | #include "CGValue.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 21 | #include "clang/CodeGen/CGFunctionInfo.h" |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CodeGenOptions.h" |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DataLayout.h" |
| 26 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 28 | #include <algorithm> // std::sort |
| 29 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 33 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 34 | llvm::Value *Array, |
| 35 | llvm::Value *Value, |
| 36 | unsigned FirstIndex, |
| 37 | unsigned LastIndex) { |
| 38 | // Alternatively, we could emit this as a loop in the source. |
| 39 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 40 | llvm::Value *Cell = |
| 41 | Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 42 | Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 46 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 47 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 48 | T->isMemberFunctionPointerType(); |
| 49 | } |
| 50 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 51 | ABIArgInfo |
| 52 | ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, |
| 53 | llvm::Type *Padding) const { |
| 54 | return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), |
| 55 | ByRef, Realign, Padding); |
| 56 | } |
| 57 | |
| 58 | ABIArgInfo |
| 59 | ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { |
| 60 | return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), |
| 61 | /*ByRef*/ false, Realign); |
| 62 | } |
| 63 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 64 | Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 65 | QualType Ty) const { |
| 66 | return Address::invalid(); |
| 67 | } |
| 68 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 69 | ABIInfo::~ABIInfo() {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 70 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 71 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 72 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 73 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 74 | if (!RD) |
| 75 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 76 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 80 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 81 | const RecordType *RT = T->getAs<RecordType>(); |
| 82 | if (!RT) |
| 83 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 84 | return getRecordArgABI(RT, CXXABI); |
| 85 | } |
| 86 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 87 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 88 | /// should ensure that all elements of the union have the same "machine type". |
| 89 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 90 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 91 | const RecordDecl *UD = UT->getDecl(); |
| 92 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 93 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 94 | return UD->field_begin()->getType(); |
| 95 | } |
| 96 | } |
| 97 | return Ty; |
| 98 | } |
| 99 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 100 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 101 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 104 | ASTContext &ABIInfo::getContext() const { |
| 105 | return CGT.getContext(); |
| 106 | } |
| 107 | |
| 108 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 109 | return CGT.getLLVMContext(); |
| 110 | } |
| 111 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 112 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 113 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 114 | } |
| 115 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 116 | const TargetInfo &ABIInfo::getTarget() const { |
| 117 | return CGT.getTarget(); |
| 118 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 119 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 120 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 125 | uint64_t Members) const { |
| 126 | return false; |
| 127 | } |
| 128 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 129 | bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 130 | return false; |
| 131 | } |
| 132 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 133 | void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 134 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 135 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 136 | switch (TheKind) { |
| 137 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 138 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 139 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 140 | Ty->print(OS); |
| 141 | else |
| 142 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 143 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 144 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 145 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 146 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 147 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 148 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 149 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 150 | case InAlloca: |
| 151 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 152 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 153 | case Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 154 | OS << "Indirect Align=" << getIndirectAlign().getQuantity() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 155 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 156 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 157 | break; |
| 158 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 159 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 160 | break; |
| 161 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 162 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 165 | // Dynamically round a pointer up to a multiple of the given alignment. |
| 166 | static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, |
| 167 | llvm::Value *Ptr, |
| 168 | CharUnits Align) { |
| 169 | llvm::Value *PtrAsInt = Ptr; |
| 170 | // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; |
| 171 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 172 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 173 | llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); |
| 174 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 175 | llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); |
| 176 | PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, |
| 177 | Ptr->getType(), |
| 178 | Ptr->getName() + ".aligned"); |
| 179 | return PtrAsInt; |
| 180 | } |
| 181 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 182 | /// Emit va_arg for a platform using the common void* representation, |
| 183 | /// where arguments are simply emitted in an array of slots on the stack. |
| 184 | /// |
| 185 | /// This version implements the core direct-value passing rules. |
| 186 | /// |
| 187 | /// \param SlotSize - The size and alignment of a stack slot. |
| 188 | /// Each argument will be allocated to a multiple of this number of |
| 189 | /// slots, and all the slots will be aligned to this value. |
| 190 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 191 | /// an argument type with an alignment greater than the slot size |
| 192 | /// will be emitted on a higher-alignment address, potentially |
| 193 | /// leaving one or more empty slots behind as padding. If this |
| 194 | /// is false, the returned address might be less-aligned than |
| 195 | /// DirectAlign. |
| 196 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 197 | Address VAListAddr, |
| 198 | llvm::Type *DirectTy, |
| 199 | CharUnits DirectSize, |
| 200 | CharUnits DirectAlign, |
| 201 | CharUnits SlotSize, |
| 202 | bool AllowHigherAlign) { |
| 203 | // Cast the element type to i8* if necessary. Some platforms define |
| 204 | // va_list as a struct containing an i8* instead of just an i8*. |
| 205 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 206 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 207 | |
| 208 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 209 | |
| 210 | // If the CC aligns values higher than the slot size, do so if needed. |
| 211 | Address Addr = Address::invalid(); |
| 212 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 213 | Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), |
| 214 | DirectAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 215 | } else { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 216 | Addr = Address(Ptr, SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Advance the pointer past the argument, then store that back. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 220 | CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 221 | llvm::Value *NextPtr = |
| 222 | CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, |
| 223 | "argp.next"); |
| 224 | CGF.Builder.CreateStore(NextPtr, VAListAddr); |
| 225 | |
| 226 | // If the argument is smaller than a slot, and this is a big-endian |
| 227 | // target, the argument will be right-adjusted in its slot. |
| 228 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian()) { |
| 229 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 230 | } |
| 231 | |
| 232 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 233 | return Addr; |
| 234 | } |
| 235 | |
| 236 | /// Emit va_arg for a platform using the common void* representation, |
| 237 | /// where arguments are simply emitted in an array of slots on the stack. |
| 238 | /// |
| 239 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 240 | /// \param ValueInfo - The size and alignment of this type, generally |
| 241 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 242 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 243 | /// Each argument will be allocated to a multiple of this number of |
| 244 | /// slots, and all the slots will be aligned to this value. |
| 245 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 246 | /// an argument type with an alignment greater than the slot size |
| 247 | /// will be emitted on a higher-alignment address, potentially |
| 248 | /// leaving one or more empty slots behind as padding. |
| 249 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 250 | QualType ValueTy, bool IsIndirect, |
| 251 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 252 | CharUnits SlotSizeAndAlign, |
| 253 | bool AllowHigherAlign) { |
| 254 | // The size and alignment of the value that was passed directly. |
| 255 | CharUnits DirectSize, DirectAlign; |
| 256 | if (IsIndirect) { |
| 257 | DirectSize = CGF.getPointerSize(); |
| 258 | DirectAlign = CGF.getPointerAlign(); |
| 259 | } else { |
| 260 | DirectSize = ValueInfo.first; |
| 261 | DirectAlign = ValueInfo.second; |
| 262 | } |
| 263 | |
| 264 | // Cast the address we've calculated to the right type. |
| 265 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 266 | if (IsIndirect) |
| 267 | DirectTy = DirectTy->getPointerTo(0); |
| 268 | |
| 269 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 270 | DirectSize, DirectAlign, |
| 271 | SlotSizeAndAlign, |
| 272 | AllowHigherAlign); |
| 273 | |
| 274 | if (IsIndirect) { |
| 275 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 276 | } |
| 277 | |
| 278 | return Addr; |
| 279 | |
| 280 | } |
| 281 | |
| 282 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 283 | Address Addr1, llvm::BasicBlock *Block1, |
| 284 | Address Addr2, llvm::BasicBlock *Block2, |
| 285 | const llvm::Twine &Name = "") { |
| 286 | assert(Addr1.getType() == Addr2.getType()); |
| 287 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 288 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 289 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 290 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 291 | return Address(PHI, Align); |
| 292 | } |
| 293 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 294 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 295 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 296 | // If someone can figure out a general rule for this, that would be great. |
| 297 | // It's probably just doomed to be platform-dependent, though. |
| 298 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 299 | // Verified for: |
| 300 | // x86-64 FreeBSD, Linux, Darwin |
| 301 | // x86-32 FreeBSD, Linux, Darwin |
| 302 | // PowerPC Linux, Darwin |
| 303 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 304 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 305 | return 32; |
| 306 | } |
| 307 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 308 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 309 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 310 | // The following conventions are known to require this to be false: |
| 311 | // x86_stdcall |
| 312 | // MIPS |
| 313 | // For everything else, we just prefer false unless we opt out. |
| 314 | return false; |
| 315 | } |
| 316 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 317 | void |
| 318 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 319 | llvm::SmallString<24> &Opt) const { |
| 320 | // This assumes the user is passing a library name like "rt" instead of a |
| 321 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 322 | // dynamic. |
| 323 | Opt = "-l"; |
| 324 | Opt += Lib; |
| 325 | } |
| 326 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 327 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 328 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 329 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 330 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 331 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 332 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 333 | if (FD->isUnnamedBitfield()) |
| 334 | return true; |
| 335 | |
| 336 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 337 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 338 | // Constant arrays of empty records count as empty, strip them off. |
| 339 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 340 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 341 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 342 | if (AT->getSize() == 0) |
| 343 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 344 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 345 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 346 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 347 | const RecordType *RT = FT->getAs<RecordType>(); |
| 348 | if (!RT) |
| 349 | return false; |
| 350 | |
| 351 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 352 | // |
| 353 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 354 | // current ABI. |
| 355 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 356 | return false; |
| 357 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 358 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 361 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 362 | /// fields. Note that a structure with a flexible array member is not |
| 363 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 364 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 365 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 366 | if (!RT) |
| 367 | return 0; |
| 368 | const RecordDecl *RD = RT->getDecl(); |
| 369 | if (RD->hasFlexibleArrayMember()) |
| 370 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 371 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 372 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 373 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 374 | for (const auto &I : CXXRD->bases()) |
| 375 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 376 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 377 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 378 | for (const auto *I : RD->fields()) |
| 379 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 380 | return false; |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | /// isSingleElementStruct - Determine if a structure is a "single |
| 385 | /// element struct", i.e. it has exactly one non-empty field or |
| 386 | /// exactly one field which is itself a single element |
| 387 | /// struct. Structures with flexible array members are never |
| 388 | /// considered single element structs. |
| 389 | /// |
| 390 | /// \return The field declaration for the single non-empty field, if |
| 391 | /// it exists. |
| 392 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 393 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 394 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 395 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 396 | |
| 397 | const RecordDecl *RD = RT->getDecl(); |
| 398 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 399 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 400 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 401 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 402 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 403 | // If this is a C++ record, check the bases first. |
| 404 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 405 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 406 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 407 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 408 | continue; |
| 409 | |
| 410 | // If we already found an element then this isn't a single-element struct. |
| 411 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 412 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 413 | |
| 414 | // If this is non-empty and not a single element struct, the composite |
| 415 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 416 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 417 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 418 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 423 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 424 | QualType FT = FD->getType(); |
| 425 | |
| 426 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 427 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 428 | continue; |
| 429 | |
| 430 | // If we already found an element then this isn't a single-element |
| 431 | // struct. |
| 432 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 433 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 434 | |
| 435 | // Treat single element arrays as the element. |
| 436 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 437 | if (AT->getSize().getZExtValue() != 1) |
| 438 | break; |
| 439 | FT = AT->getElementType(); |
| 440 | } |
| 441 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 442 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 443 | Found = FT.getTypePtr(); |
| 444 | } else { |
| 445 | Found = isSingleElementStruct(FT, Context); |
| 446 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 447 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 451 | // We don't consider a struct a single-element struct if it has |
| 452 | // padding beyond the element type. |
| 453 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 454 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 455 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 456 | return Found; |
| 457 | } |
| 458 | |
| 459 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 460 | // Treat complex types as the element type. |
| 461 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 462 | Ty = CTy->getElementType(); |
| 463 | |
| 464 | // Check for a type which we know has a simple scalar argument-passing |
| 465 | // convention without any padding. (We're specifically looking for 32 |
| 466 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 467 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 468 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 469 | return false; |
| 470 | |
| 471 | uint64_t Size = Context.getTypeSize(Ty); |
| 472 | return Size == 32 || Size == 64; |
| 473 | } |
| 474 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 475 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 476 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 477 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 478 | /// inhibiting optimizations. |
| 479 | /// |
| 480 | // FIXME: This predicate is missing many cases, currently it just follows |
| 481 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 482 | // should probably make this smarter, or better yet make the LLVM backend |
| 483 | // capable of handling it. |
| 484 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 485 | // We can only expand structure types. |
| 486 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 487 | if (!RT) |
| 488 | return false; |
| 489 | |
| 490 | // We can only expand (C) structures. |
| 491 | // |
| 492 | // FIXME: This needs to be generalized to handle classes as well. |
| 493 | const RecordDecl *RD = RT->getDecl(); |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 494 | if (!RD->isStruct()) |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 495 | return false; |
| 496 | |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 497 | // We try to expand CLike CXXRecordDecl. |
| 498 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 499 | if (!CXXRD->isCLike()) |
| 500 | return false; |
| 501 | } |
| 502 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 503 | uint64_t Size = 0; |
| 504 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 505 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 506 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 507 | return false; |
| 508 | |
| 509 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 510 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 511 | // counts as "basic" is more complicated than what we were doing previously. |
| 512 | if (FD->isBitField()) |
| 513 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 514 | |
| 515 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 518 | // Make sure there are not any holes in the struct. |
| 519 | if (Size != Context.getTypeSize(Ty)) |
| 520 | return false; |
| 521 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 522 | return true; |
| 523 | } |
| 524 | |
| 525 | namespace { |
| 526 | /// DefaultABIInfo - The default implementation for ABI specific |
| 527 | /// details. This implementation provides information which results in |
| 528 | /// self-consistent and sensible LLVM IR generation, but does not |
| 529 | /// conform to any particular ABI. |
| 530 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 531 | public: |
| 532 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 534 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 535 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 536 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 537 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 538 | if (!getCXXABI().classifyReturnType(FI)) |
| 539 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 540 | for (auto &I : FI.arguments()) |
| 541 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 542 | } |
| 543 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 544 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 545 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 546 | }; |
| 547 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 548 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 549 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 550 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 551 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 552 | }; |
| 553 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 554 | Address DefaultABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 555 | QualType Ty) const { |
| 556 | return Address::invalid(); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 559 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 560 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 561 | |
| 562 | if (isAggregateTypeForABI(Ty)) { |
| 563 | // Records with non-trivial destructors/copy-constructors should not be |
| 564 | // passed by value. |
| 565 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 566 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 567 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 568 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 569 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 570 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 571 | // Treat an enum type as its underlying type. |
| 572 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 573 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 574 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 575 | return (Ty->isPromotableIntegerType() ? |
| 576 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 579 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 580 | if (RetTy->isVoidType()) |
| 581 | return ABIArgInfo::getIgnore(); |
| 582 | |
| 583 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 584 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 585 | |
| 586 | // Treat an enum type as its underlying type. |
| 587 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 588 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 589 | |
| 590 | return (RetTy->isPromotableIntegerType() ? |
| 591 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 592 | } |
| 593 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 594 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 595 | // WebAssembly ABI Implementation |
| 596 | // |
| 597 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 598 | //===----------------------------------------------------------------------===// |
| 599 | |
| 600 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 601 | public: |
| 602 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 603 | : DefaultABIInfo(CGT) {} |
| 604 | |
| 605 | private: |
| 606 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 607 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 608 | |
| 609 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 610 | // non-virtual, but computeInfo is virtual, so we overload that. |
| 611 | void computeInfo(CGFunctionInfo &FI) const override { |
| 612 | if (!getCXXABI().classifyReturnType(FI)) |
| 613 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 614 | for (auto &Arg : FI.arguments()) |
| 615 | Arg.info = classifyArgumentType(Arg.type); |
| 616 | } |
| 617 | }; |
| 618 | |
| 619 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 620 | public: |
| 621 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 622 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 623 | }; |
| 624 | |
| 625 | /// \brief Classify argument of given type \p Ty. |
| 626 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 627 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 628 | |
| 629 | if (isAggregateTypeForABI(Ty)) { |
| 630 | // Records with non-trivial destructors/copy-constructors should not be |
| 631 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 632 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 633 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 634 | // Ignore empty structs/unions. |
| 635 | if (isEmptyRecord(getContext(), Ty, true)) |
| 636 | return ABIArgInfo::getIgnore(); |
| 637 | // Lower single-element structs to just pass a regular value. TODO: We |
| 638 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 639 | // though watch out for things like bitfields. |
| 640 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 641 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | // Otherwise just do the default thing. |
| 645 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 646 | } |
| 647 | |
| 648 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 649 | if (isAggregateTypeForABI(RetTy)) { |
| 650 | // Records with non-trivial destructors/copy-constructors should not be |
| 651 | // returned by value. |
| 652 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 653 | // Ignore empty structs/unions. |
| 654 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 655 | return ABIArgInfo::getIgnore(); |
| 656 | // Lower single-element structs to just return a regular value. TODO: We |
| 657 | // could do reasonable-size multiple-element structs too, using |
| 658 | // ABIArgInfo::getDirect(). |
| 659 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 660 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // Otherwise just do the default thing. |
| 665 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 666 | } |
| 667 | |
| 668 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 669 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 670 | // |
| 671 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 672 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 673 | //===----------------------------------------------------------------------===// |
| 674 | |
| 675 | class PNaClABIInfo : public ABIInfo { |
| 676 | public: |
| 677 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 678 | |
| 679 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 680 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 681 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 682 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 683 | Address EmitVAArg(CodeGenFunction &CGF, |
| 684 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 685 | }; |
| 686 | |
| 687 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 688 | public: |
| 689 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 690 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 691 | }; |
| 692 | |
| 693 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 694 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 695 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 696 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 697 | for (auto &I : FI.arguments()) |
| 698 | I.info = classifyArgumentType(I.type); |
| 699 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 700 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 701 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 702 | QualType Ty) const { |
| 703 | return Address::invalid(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 706 | /// \brief Classify argument of given type \p Ty. |
| 707 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 708 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 709 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 710 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 711 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 712 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 713 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 714 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 715 | } else if (Ty->isFloatingType()) { |
| 716 | // Floating-point types don't go inreg. |
| 717 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 718 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 719 | |
| 720 | return (Ty->isPromotableIntegerType() ? |
| 721 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 725 | if (RetTy->isVoidType()) |
| 726 | return ABIArgInfo::getIgnore(); |
| 727 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 728 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 729 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 730 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 731 | |
| 732 | // Treat an enum type as its underlying type. |
| 733 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 734 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 735 | |
| 736 | return (RetTy->isPromotableIntegerType() ? |
| 737 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 738 | } |
| 739 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 740 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 741 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 742 | // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 743 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 744 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 745 | IRType->getScalarSizeInBits() != 64; |
| 746 | } |
| 747 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 748 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 749 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 750 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 751 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 752 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 753 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 754 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 757 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 761 | return Ty; |
| 762 | } |
| 763 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 764 | /// Returns true if this type can be passed in SSE registers with the |
| 765 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 766 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 767 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 768 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 769 | return true; |
| 770 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 771 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 772 | // registers specially. |
| 773 | unsigned VecSize = Context.getTypeSize(VT); |
| 774 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 775 | return true; |
| 776 | } |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 781 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 782 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 783 | return NumMembers <= 4; |
| 784 | } |
| 785 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 786 | //===----------------------------------------------------------------------===// |
| 787 | // X86-32 ABI Implementation |
| 788 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 789 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 790 | /// \brief Similar to llvm::CCState, but for Clang. |
| 791 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 792 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 793 | |
| 794 | unsigned CC; |
| 795 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 796 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 797 | }; |
| 798 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 799 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 800 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 801 | enum Class { |
| 802 | Integer, |
| 803 | Float |
| 804 | }; |
| 805 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 806 | static const unsigned MinABIStackAlignInBytes = 4; |
| 807 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 808 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 809 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 810 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 811 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 812 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 813 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 814 | |
| 815 | static bool isRegisterSize(unsigned Size) { |
| 816 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 817 | } |
| 818 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 819 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 820 | // FIXME: Assumes vectorcall is in use. |
| 821 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 822 | } |
| 823 | |
| 824 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 825 | uint64_t NumMembers) const override { |
| 826 | // FIXME: Assumes vectorcall is in use. |
| 827 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 828 | } |
| 829 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 830 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 831 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 832 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 833 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 834 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 835 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 836 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 837 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 838 | /// \brief Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 839 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 840 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 841 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 842 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 843 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 844 | /// \brief Updates the number of available free registers, returns |
| 845 | /// true if any registers were allocated. |
| 846 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 847 | |
| 848 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 849 | bool &NeedsPadding) const; |
| 850 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 851 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 852 | /// \brief Rewrite the function info so that all memory arguments use |
| 853 | /// inalloca. |
| 854 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 855 | |
| 856 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 857 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 858 | QualType Type) const; |
| 859 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 860 | public: |
| 861 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 862 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 863 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 864 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 865 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 866 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 867 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 868 | unsigned NumRegisterParameters, bool SoftFloatABI) |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 869 | : ABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
| 870 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 871 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 872 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 873 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 874 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 875 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 876 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 877 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 878 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 879 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 880 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 881 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 882 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 883 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 884 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 885 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 886 | static bool isStructReturnInRegABI( |
| 887 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 888 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 889 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 890 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 891 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 892 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 893 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 894 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 895 | return 4; |
| 896 | } |
| 897 | |
| 898 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 899 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 900 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 901 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 902 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 903 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 904 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 905 | } |
| 906 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 907 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 908 | std::string &Constraints, |
| 909 | std::vector<llvm::Type *> &ResultRegTypes, |
| 910 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 911 | std::vector<LValue> &ResultRegDests, |
| 912 | std::string &AsmString, |
| 913 | unsigned NumOutputs) const override; |
| 914 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 915 | llvm::Constant * |
| 916 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 917 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 918 | (0x06 << 8) | // .+0x08 |
| 919 | ('F' << 16) | |
| 920 | ('T' << 24); |
| 921 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 922 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 923 | }; |
| 924 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 925 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 926 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 927 | /// Rewrite input constraint references after adding some output constraints. |
| 928 | /// In the case where there is one output and one input and we add one output, |
| 929 | /// we need to replace all operand references greater than or equal to 1: |
| 930 | /// mov $0, $1 |
| 931 | /// mov eax, $1 |
| 932 | /// The result will be: |
| 933 | /// mov $0, $2 |
| 934 | /// mov eax, $2 |
| 935 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 936 | unsigned NumNewOuts, |
| 937 | std::string &AsmString) { |
| 938 | std::string Buf; |
| 939 | llvm::raw_string_ostream OS(Buf); |
| 940 | size_t Pos = 0; |
| 941 | while (Pos < AsmString.size()) { |
| 942 | size_t DollarStart = AsmString.find('$', Pos); |
| 943 | if (DollarStart == std::string::npos) |
| 944 | DollarStart = AsmString.size(); |
| 945 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 946 | if (DollarEnd == std::string::npos) |
| 947 | DollarEnd = AsmString.size(); |
| 948 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 949 | Pos = DollarEnd; |
| 950 | size_t NumDollars = DollarEnd - DollarStart; |
| 951 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 952 | // We have an operand reference. |
| 953 | size_t DigitStart = Pos; |
| 954 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 955 | if (DigitEnd == std::string::npos) |
| 956 | DigitEnd = AsmString.size(); |
| 957 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 958 | unsigned OperandIndex; |
| 959 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 960 | if (OperandIndex >= FirstIn) |
| 961 | OperandIndex += NumNewOuts; |
| 962 | OS << OperandIndex; |
| 963 | } else { |
| 964 | OS << OperandStr; |
| 965 | } |
| 966 | Pos = DigitEnd; |
| 967 | } |
| 968 | } |
| 969 | AsmString = std::move(OS.str()); |
| 970 | } |
| 971 | |
| 972 | /// Add output constraints for EAX:EDX because they are return registers. |
| 973 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 974 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 975 | std::vector<llvm::Type *> &ResultRegTypes, |
| 976 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 977 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 978 | unsigned NumOutputs) const { |
| 979 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 980 | |
| 981 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 982 | // larger. |
| 983 | if (!Constraints.empty()) |
| 984 | Constraints += ','; |
| 985 | if (RetWidth <= 32) { |
| 986 | Constraints += "={eax}"; |
| 987 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 988 | } else { |
| 989 | // Use the 'A' constraint for EAX:EDX. |
| 990 | Constraints += "=A"; |
| 991 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 992 | } |
| 993 | |
| 994 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 995 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 996 | ResultTruncRegTypes.push_back(CoerceTy); |
| 997 | |
| 998 | // Coerce the integer by bitcasting the return slot pointer. |
| 999 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1000 | CoerceTy->getPointerTo())); |
| 1001 | ResultRegDests.push_back(ReturnSlot); |
| 1002 | |
| 1003 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1004 | } |
| 1005 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1006 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1007 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1008 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1009 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1010 | uint64_t Size = Context.getTypeSize(Ty); |
| 1011 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1012 | // For i386, type must be register sized. |
| 1013 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1014 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1015 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1016 | |
| 1017 | if (Ty->isVectorType()) { |
| 1018 | // 64- and 128- bit vectors inside structures are not returned in |
| 1019 | // registers. |
| 1020 | if (Size == 64 || Size == 128) |
| 1021 | return false; |
| 1022 | |
| 1023 | return true; |
| 1024 | } |
| 1025 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1026 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1027 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1028 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1029 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1030 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1031 | return true; |
| 1032 | |
| 1033 | // Arrays are treated like records. |
| 1034 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1035 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1036 | |
| 1037 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1038 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1039 | if (!RT) return false; |
| 1040 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1041 | // FIXME: Traverse bases here too. |
| 1042 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1043 | // Structure types are passed in register if all fields would be |
| 1044 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1045 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1046 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1047 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1048 | continue; |
| 1049 | |
| 1050 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1051 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1052 | return false; |
| 1053 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1054 | return true; |
| 1055 | } |
| 1056 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1057 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1058 | // If the return value is indirect, then the hidden argument is consuming one |
| 1059 | // integer register. |
| 1060 | if (State.FreeRegs) { |
| 1061 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1062 | if (!IsMCUABI) |
| 1063 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1064 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1065 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1068 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1069 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1070 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1071 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1072 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1073 | const Type *Base = nullptr; |
| 1074 | uint64_t NumElts = 0; |
| 1075 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1076 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1077 | // The LLVM struct type for such an aggregate should lower properly. |
| 1078 | return ABIArgInfo::getDirect(); |
| 1079 | } |
| 1080 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1081 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1082 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1083 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1084 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1085 | |
| 1086 | // 128-bit vectors are a special case; they are returned in |
| 1087 | // registers and we need to make sure to pick a type the LLVM |
| 1088 | // backend will like. |
| 1089 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1090 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1091 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1092 | |
| 1093 | // Always return in register if it fits in a general purpose |
| 1094 | // register, or if it is 64 bits and has a single element. |
| 1095 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1096 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1097 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1098 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1099 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1100 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1104 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1105 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1106 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1107 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1108 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1109 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1110 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1111 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1112 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1113 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1114 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1115 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1116 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1117 | // Small structures which are register sized are generally returned |
| 1118 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1119 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1120 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1121 | |
| 1122 | // As a special-case, if the struct is a "single-element" struct, and |
| 1123 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1124 | // floating-point register. (MSVC does not apply this special case.) |
| 1125 | // We apply a similar transformation for pointer types to improve the |
| 1126 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1127 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1128 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1129 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1130 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1131 | |
| 1132 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1133 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1134 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1137 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1138 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1140 | // Treat an enum type as its underlying type. |
| 1141 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1142 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1143 | |
| 1144 | return (RetTy->isPromotableIntegerType() ? |
| 1145 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1148 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1149 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1150 | } |
| 1151 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1152 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1153 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1154 | if (!RT) |
| 1155 | return 0; |
| 1156 | const RecordDecl *RD = RT->getDecl(); |
| 1157 | |
| 1158 | // If this is a C++ record, check the bases first. |
| 1159 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1160 | for (const auto &I : CXXRD->bases()) |
| 1161 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1162 | return false; |
| 1163 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1164 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1165 | QualType FT = i->getType(); |
| 1166 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1167 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1168 | return true; |
| 1169 | |
| 1170 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1171 | return true; |
| 1172 | } |
| 1173 | |
| 1174 | return false; |
| 1175 | } |
| 1176 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1177 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1178 | unsigned Align) const { |
| 1179 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1180 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1181 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1182 | return 0; // Use default alignment. |
| 1183 | |
| 1184 | // On non-Darwin, the stack type alignment is always 4. |
| 1185 | if (!IsDarwinVectorABI) { |
| 1186 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1187 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1188 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1189 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1190 | // Otherwise, if the type contains an SSE vector type, the alignment is 16. |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1191 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1192 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1193 | return 16; |
| 1194 | |
| 1195 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1198 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1199 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1200 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1201 | if (State.FreeRegs) { |
| 1202 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1203 | if (!IsMCUABI) |
| 1204 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1205 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1206 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1207 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1208 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1209 | // Compute the byval alignment. |
| 1210 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1211 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1212 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1213 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1214 | |
| 1215 | // If the stack alignment is less than the type alignment, realign the |
| 1216 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1217 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1218 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1219 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1222 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1223 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1224 | if (!T) |
| 1225 | T = Ty.getTypePtr(); |
| 1226 | |
| 1227 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1228 | BuiltinType::Kind K = BT->getKind(); |
| 1229 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1230 | return Float; |
| 1231 | } |
| 1232 | return Integer; |
| 1233 | } |
| 1234 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1235 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1236 | if (!IsSoftFloatABI) { |
| 1237 | Class C = classify(Ty); |
| 1238 | if (C == Float) |
| 1239 | return false; |
| 1240 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1241 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1242 | unsigned Size = getContext().getTypeSize(Ty); |
| 1243 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1244 | |
| 1245 | if (SizeInRegs == 0) |
| 1246 | return false; |
| 1247 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1248 | if (!IsMCUABI) { |
| 1249 | if (SizeInRegs > State.FreeRegs) { |
| 1250 | State.FreeRegs = 0; |
| 1251 | return false; |
| 1252 | } |
| 1253 | } else { |
| 1254 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1255 | // earlier parameters that are passed on the stack. Also, |
| 1256 | // it does not allow passing >8-byte structs in-register, |
| 1257 | // even if there are 3 free registers available. |
| 1258 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1259 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1260 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1261 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1262 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1263 | return true; |
| 1264 | } |
| 1265 | |
| 1266 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
| 1267 | bool &InReg, |
| 1268 | bool &NeedsPadding) const { |
| 1269 | NeedsPadding = false; |
| 1270 | InReg = !IsMCUABI; |
| 1271 | |
| 1272 | if (!updateFreeRegs(Ty, State)) |
| 1273 | return false; |
| 1274 | |
| 1275 | if (IsMCUABI) |
| 1276 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1277 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1278 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1279 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1280 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1281 | NeedsPadding = true; |
| 1282 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1283 | return false; |
| 1284 | } |
| 1285 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1286 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1289 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1290 | if (!updateFreeRegs(Ty, State)) |
| 1291 | return false; |
| 1292 | |
| 1293 | if (IsMCUABI) |
| 1294 | return false; |
| 1295 | |
| 1296 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1297 | State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1298 | if (getContext().getTypeSize(Ty) > 32) |
| 1299 | return false; |
| 1300 | |
| 1301 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
| 1302 | Ty->isReferenceType()); |
| 1303 | } |
| 1304 | |
| 1305 | return true; |
| 1306 | } |
| 1307 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1308 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1309 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1310 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1311 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1312 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1313 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1314 | // Check with the C++ ABI first. |
| 1315 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1316 | if (RT) { |
| 1317 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1318 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1319 | return getIndirectResult(Ty, false, State); |
| 1320 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1321 | // The field index doesn't matter, we'll fix it up later. |
| 1322 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1327 | // to other targets. |
| 1328 | const Type *Base = nullptr; |
| 1329 | uint64_t NumElts = 0; |
| 1330 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1331 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1332 | if (State.FreeSSERegs >= NumElts) { |
| 1333 | State.FreeSSERegs -= NumElts; |
| 1334 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1335 | return ABIArgInfo::getDirect(); |
| 1336 | return ABIArgInfo::getExpand(); |
| 1337 | } |
| 1338 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1339 | } |
| 1340 | |
| 1341 | if (isAggregateTypeForABI(Ty)) { |
| 1342 | if (RT) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1343 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1344 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1345 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1346 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1347 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1348 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1349 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1350 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1351 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 1352 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 1353 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1354 | return ABIArgInfo::getIgnore(); |
| 1355 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1356 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1357 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1358 | bool NeedsPadding, InReg; |
| 1359 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1360 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1361 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1362 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1363 | if (InReg) |
| 1364 | return ABIArgInfo::getDirectInReg(Result); |
| 1365 | else |
| 1366 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1367 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1368 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1369 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1370 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1371 | // of those arguments will match the struct. This is important because the |
| 1372 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1373 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1374 | // Don't do this for the MCU if there are still free integer registers |
| 1375 | // (see X86_64 ABI for full explanation). |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1376 | if (getContext().getTypeSize(Ty) <= 4*32 && |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1377 | canExpandIndirectArgument(Ty, getContext()) && |
| 1378 | (!IsMCUABI || State.FreeRegs == 0)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1379 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1380 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1381 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1382 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1383 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1384 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1387 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1388 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1389 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1390 | if (IsDarwinVectorABI) { |
| 1391 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1392 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1393 | (Size == 64 && VT->getNumElements() == 1)) |
| 1394 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1395 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1396 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1397 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1398 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1399 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1400 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1401 | return ABIArgInfo::getDirect(); |
| 1402 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1403 | |
| 1404 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1405 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1406 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1407 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1408 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1409 | |
| 1410 | if (Ty->isPromotableIntegerType()) { |
| 1411 | if (InReg) |
| 1412 | return ABIArgInfo::getExtendInReg(); |
| 1413 | return ABIArgInfo::getExtend(); |
| 1414 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1415 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1416 | if (InReg) |
| 1417 | return ABIArgInfo::getDirectInReg(); |
| 1418 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1421 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1422 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1423 | if (IsMCUABI) |
| 1424 | State.FreeRegs = 3; |
| 1425 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1426 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1427 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1428 | State.FreeRegs = 2; |
| 1429 | State.FreeSSERegs = 6; |
| 1430 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1431 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1432 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1433 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1434 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1435 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1436 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1437 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1438 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1439 | // return value was sret and put it in a register ourselves if appropriate. |
| 1440 | if (State.FreeRegs) { |
| 1441 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1442 | if (!IsMCUABI) |
| 1443 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1444 | } |
| 1445 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1446 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1447 | // The chain argument effectively gives us another free register. |
| 1448 | if (FI.isChainCall()) |
| 1449 | ++State.FreeRegs; |
| 1450 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1451 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1452 | for (auto &I : FI.arguments()) { |
| 1453 | I.info = classifyArgumentType(I.type, State); |
| 1454 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1458 | // all the memory arguments to use inalloca. |
| 1459 | if (UsedInAlloca) |
| 1460 | rewriteWithInAlloca(FI); |
| 1461 | } |
| 1462 | |
| 1463 | void |
| 1464 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1465 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1466 | QualType Type) const { |
| 1467 | // Arguments are always 4-byte-aligned. |
| 1468 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1469 | |
| 1470 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1471 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1472 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1473 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1474 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1475 | // Insert padding bytes to respect alignment. |
| 1476 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1477 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1478 | if (StackOffset != FieldEnd) { |
| 1479 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1480 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1481 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1482 | FrameFields.push_back(Ty); |
| 1483 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1486 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1487 | // Leave ignored and inreg arguments alone. |
| 1488 | switch (Info.getKind()) { |
| 1489 | case ABIArgInfo::InAlloca: |
| 1490 | return true; |
| 1491 | case ABIArgInfo::Indirect: |
| 1492 | assert(Info.getIndirectByVal()); |
| 1493 | return true; |
| 1494 | case ABIArgInfo::Ignore: |
| 1495 | return false; |
| 1496 | case ABIArgInfo::Direct: |
| 1497 | case ABIArgInfo::Extend: |
| 1498 | case ABIArgInfo::Expand: |
| 1499 | if (Info.getInReg()) |
| 1500 | return false; |
| 1501 | return true; |
| 1502 | } |
| 1503 | llvm_unreachable("invalid enum"); |
| 1504 | } |
| 1505 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1506 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1507 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1508 | |
| 1509 | // Build a packed struct type for all of the arguments in memory. |
| 1510 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1511 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1512 | // The stack alignment is always 4. |
| 1513 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1514 | |
| 1515 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1516 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1517 | |
| 1518 | // Put 'this' into the struct before 'sret', if necessary. |
| 1519 | bool IsThisCall = |
| 1520 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1521 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1522 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1523 | isArgInAlloca(I->info)) { |
| 1524 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1525 | ++I; |
| 1526 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1527 | |
| 1528 | // Put the sret parameter into the inalloca struct if it's in memory. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1529 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1530 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1531 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1532 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1533 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1537 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1538 | ++I; |
| 1539 | |
| 1540 | // Put arguments passed in memory into the struct. |
| 1541 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1542 | if (isArgInAlloca(I->info)) |
| 1543 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1547 | /*isPacked=*/true), |
| 1548 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1551 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1552 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1553 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1554 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1555 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1556 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1557 | // |
| 1558 | // Just messing with TypeInfo like this works because we never pass |
| 1559 | // anything indirectly. |
| 1560 | TypeInfo.second = CharUnits::fromQuantity( |
| 1561 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1562 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1563 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1564 | TypeInfo, CharUnits::fromQuantity(4), |
| 1565 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1568 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1569 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1570 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1571 | |
| 1572 | switch (Opts.getStructReturnConvention()) { |
| 1573 | case CodeGenOptions::SRCK_Default: |
| 1574 | break; |
| 1575 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1576 | return false; |
| 1577 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1578 | return true; |
| 1579 | } |
| 1580 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1581 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1582 | return true; |
| 1583 | |
| 1584 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1585 | case llvm::Triple::DragonFly: |
| 1586 | case llvm::Triple::FreeBSD: |
| 1587 | case llvm::Triple::OpenBSD: |
| 1588 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1589 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1590 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1591 | default: |
| 1592 | return false; |
| 1593 | } |
| 1594 | } |
| 1595 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1596 | void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1597 | llvm::GlobalValue *GV, |
| 1598 | CodeGen::CodeGenModule &CGM) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1599 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1600 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1601 | // Get the LLVM function. |
| 1602 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1603 | |
| 1604 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1605 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1606 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1607 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1608 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1609 | llvm::AttributeSet::FunctionIndex, |
| 1610 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1611 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1612 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1613 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1614 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1615 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1616 | } |
| 1617 | } |
| 1618 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1619 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1620 | CodeGen::CodeGenFunction &CGF, |
| 1621 | llvm::Value *Address) const { |
| 1622 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1623 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1624 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1625 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1626 | // 0-7 are the eight integer registers; the order is different |
| 1627 | // on Darwin (for EH), but the range is the same. |
| 1628 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1629 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1630 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1631 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1632 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1633 | // These have size 16, which is sizeof(long double) on |
| 1634 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1635 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1636 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1637 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1638 | } else { |
| 1639 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1640 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1641 | Builder.CreateAlignedStore( |
| 1642 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1643 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1644 | |
| 1645 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1646 | // These have size 12, which is sizeof(long double) on |
| 1647 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1648 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1649 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1650 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1651 | |
| 1652 | return false; |
| 1653 | } |
| 1654 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1655 | //===----------------------------------------------------------------------===// |
| 1656 | // X86-64 ABI Implementation |
| 1657 | //===----------------------------------------------------------------------===// |
| 1658 | |
| 1659 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1660 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1661 | /// The AVX ABI level for X86 targets. |
| 1662 | enum class X86AVXABILevel { |
| 1663 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1664 | AVX, |
| 1665 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1666 | }; |
| 1667 | |
| 1668 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1669 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1670 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1671 | case X86AVXABILevel::AVX512: |
| 1672 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1673 | case X86AVXABILevel::AVX: |
| 1674 | return 256; |
| 1675 | case X86AVXABILevel::None: |
| 1676 | return 128; |
| 1677 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1678 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1681 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1682 | class X86_64ABIInfo : public ABIInfo { |
| 1683 | enum Class { |
| 1684 | Integer = 0, |
| 1685 | SSE, |
| 1686 | SSEUp, |
| 1687 | X87, |
| 1688 | X87Up, |
| 1689 | ComplexX87, |
| 1690 | NoClass, |
| 1691 | Memory |
| 1692 | }; |
| 1693 | |
| 1694 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1695 | /// |
| 1696 | /// Merge an accumulating classification \arg Accum with a field |
| 1697 | /// classification \arg Field. |
| 1698 | /// |
| 1699 | /// \param Accum - The accumulating classification. This should |
| 1700 | /// always be either NoClass or the result of a previous merge |
| 1701 | /// call. In addition, this should never be Memory (the caller |
| 1702 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1703 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1704 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1705 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1706 | /// |
| 1707 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1708 | /// final MEMORY or SSE classes when necessary. |
| 1709 | /// |
| 1710 | /// \param AggregateSize - The size of the current aggregate in |
| 1711 | /// the classification process. |
| 1712 | /// |
| 1713 | /// \param Lo - The classification for the parts of the type |
| 1714 | /// residing in the low word of the containing object. |
| 1715 | /// |
| 1716 | /// \param Hi - The classification for the parts of the type |
| 1717 | /// residing in the higher words of the containing object. |
| 1718 | /// |
| 1719 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1720 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1721 | /// classify - Determine the x86_64 register classes in which the |
| 1722 | /// given type T should be passed. |
| 1723 | /// |
| 1724 | /// \param Lo - The classification for the parts of the type |
| 1725 | /// residing in the low word of the containing object. |
| 1726 | /// |
| 1727 | /// \param Hi - The classification for the parts of the type |
| 1728 | /// residing in the high word of the containing object. |
| 1729 | /// |
| 1730 | /// \param OffsetBase - The bit offset of this type in the |
| 1731 | /// containing object. Some parameters are classified different |
| 1732 | /// depending on whether they straddle an eightbyte boundary. |
| 1733 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1734 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1735 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1736 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1737 | /// If a word is unused its result will be NoClass; if a type should |
| 1738 | /// be passed in Memory then at least the classification of \arg Lo |
| 1739 | /// will be Memory. |
| 1740 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1741 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1742 | /// |
| 1743 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1744 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1745 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1746 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1747 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1748 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1749 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1750 | unsigned IROffset, QualType SourceTy, |
| 1751 | unsigned SourceOffset) const; |
| 1752 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1753 | unsigned IROffset, QualType SourceTy, |
| 1754 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1755 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1756 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1757 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1758 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1759 | |
| 1760 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1761 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1762 | /// |
| 1763 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1764 | /// available. |
| 1765 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1766 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1767 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1768 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1769 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1770 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1771 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1772 | unsigned &neededSSE, |
| 1773 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1774 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1775 | bool IsIllegalVectorType(QualType Ty) const; |
| 1776 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1777 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1778 | /// unfortunately in ways that were not always consistent with |
| 1779 | /// certain previous compilers. In particular, platforms which |
| 1780 | /// required strict binary compatibility with older versions of GCC |
| 1781 | /// may need to exempt themselves. |
| 1782 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1783 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1786 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1787 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1788 | // 64-bit hardware. |
| 1789 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1790 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1791 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1792 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
| 1793 | ABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1794 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1795 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1796 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1797 | bool isPassedUsingAVXType(QualType type) const { |
| 1798 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1799 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1800 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1801 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1802 | if (info.isDirect()) { |
| 1803 | llvm::Type *ty = info.getCoerceToType(); |
| 1804 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1805 | return (vectorTy->getBitWidth() > 128); |
| 1806 | } |
| 1807 | return false; |
| 1808 | } |
| 1809 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1810 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1811 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1812 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1813 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 1814 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1815 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1816 | |
| 1817 | bool has64BitPointers() const { |
| 1818 | return Has64BitPointers; |
| 1819 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1820 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1821 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1822 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1823 | class WinX86_64ABIInfo : public ABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1824 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 1825 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
| 1826 | : ABIInfo(CGT), |
| 1827 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1828 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1829 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1830 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1831 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1832 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1833 | |
| 1834 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1835 | // FIXME: Assumes vectorcall is in use. |
| 1836 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1837 | } |
| 1838 | |
| 1839 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1840 | uint64_t NumMembers) const override { |
| 1841 | // FIXME: Assumes vectorcall is in use. |
| 1842 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1843 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 1844 | |
| 1845 | private: |
| 1846 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 1847 | bool IsReturnType) const; |
| 1848 | |
| 1849 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1850 | }; |
| 1851 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1852 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1853 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1854 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1855 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1856 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1857 | const X86_64ABIInfo &getABIInfo() const { |
| 1858 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1859 | } |
| 1860 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1861 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1862 | return 7; |
| 1863 | } |
| 1864 | |
| 1865 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1866 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1867 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1868 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1869 | // 0-15 are the 16 integer registers. |
| 1870 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1871 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1872 | return false; |
| 1873 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1874 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1875 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1876 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1877 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1878 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1879 | } |
| 1880 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1881 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1882 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1883 | // The default CC on x86-64 sets %al to the number of SSA |
| 1884 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1885 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1886 | // that when AVX types are involved: the ABI explicitly states it is |
| 1887 | // undefined, and it doesn't work in practice because of how the ABI |
| 1888 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1889 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1890 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1891 | for (CallArgList::const_iterator |
| 1892 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1893 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1894 | HasAVXType = true; |
| 1895 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1896 | } |
| 1897 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1898 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1899 | if (!HasAVXType) |
| 1900 | return true; |
| 1901 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1902 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1903 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1906 | llvm::Constant * |
| 1907 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1908 | unsigned Sig; |
| 1909 | if (getABIInfo().has64BitPointers()) |
| 1910 | Sig = (0xeb << 0) | // jmp rel8 |
| 1911 | (0x0a << 8) | // .+0x0c |
| 1912 | ('F' << 16) | |
| 1913 | ('T' << 24); |
| 1914 | else |
| 1915 | Sig = (0xeb << 0) | // jmp rel8 |
| 1916 | (0x06 << 8) | // .+0x08 |
| 1917 | ('F' << 16) | |
| 1918 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1919 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1920 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1921 | |
| 1922 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 1923 | CodeGen::CodeGenModule &CGM) const override { |
| 1924 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 1925 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1926 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1927 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1928 | } |
| 1929 | } |
| 1930 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1931 | }; |
| 1932 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1933 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 1934 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1935 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 1936 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1937 | |
| 1938 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1939 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1940 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 1941 | // If the argument contains a space, enclose it in quotes. |
| 1942 | if (Lib.find(" ") != StringRef::npos) |
| 1943 | Opt += "\"" + Lib.str() + "\""; |
| 1944 | else |
| 1945 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1946 | } |
| 1947 | }; |
| 1948 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1949 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1950 | // If the argument does not end in .lib, automatically add the suffix. |
| 1951 | // If the argument contains a space, enclose it in quotes. |
| 1952 | // This matches the behavior of MSVC. |
| 1953 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 1954 | std::string ArgStr = Quote ? "\"" : ""; |
| 1955 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1956 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1957 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1958 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1959 | return ArgStr; |
| 1960 | } |
| 1961 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1962 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1963 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1964 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1965 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 1966 | unsigned NumRegisterParameters) |
| 1967 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1968 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1969 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1970 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1971 | CodeGen::CodeGenModule &CGM) const override; |
| 1972 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1973 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1974 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1975 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1976 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1977 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1978 | |
| 1979 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1980 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1981 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1982 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1983 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1984 | }; |
| 1985 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1986 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 1987 | llvm::GlobalValue *GV, |
| 1988 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1989 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1990 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 1991 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1992 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1993 | Fn->addFnAttr("stack-probe-size", |
| 1994 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1995 | } |
| 1996 | } |
| 1997 | } |
| 1998 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1999 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2000 | llvm::GlobalValue *GV, |
| 2001 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2002 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2003 | |
| 2004 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2005 | } |
| 2006 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2007 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2008 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2009 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2010 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2011 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2012 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2013 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2014 | CodeGen::CodeGenModule &CGM) const override; |
| 2015 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2016 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2017 | return 7; |
| 2018 | } |
| 2019 | |
| 2020 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2021 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2022 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2023 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2024 | // 0-15 are the 16 integer registers. |
| 2025 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2026 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2027 | return false; |
| 2028 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2029 | |
| 2030 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2031 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2032 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2033 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2034 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2035 | |
| 2036 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2037 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2038 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2039 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2040 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2041 | }; |
| 2042 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2043 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2044 | llvm::GlobalValue *GV, |
| 2045 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2046 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2047 | |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2048 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2049 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2050 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2051 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2052 | } |
| 2053 | } |
| 2054 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2055 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2056 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2057 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2058 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2059 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2060 | Class &Hi) const { |
| 2061 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2062 | // |
| 2063 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2064 | // memory. |
| 2065 | // |
| 2066 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2067 | // memory. |
| 2068 | // |
| 2069 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2070 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2071 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2072 | // ABI working for processors that don't support the __m256 type. |
| 2073 | // |
| 2074 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2075 | // |
| 2076 | // Some of these are enforced by the merging logic. Others can arise |
| 2077 | // only with unions; for example: |
| 2078 | // union { _Complex double; unsigned; } |
| 2079 | // |
| 2080 | // Note that clauses (b) and (c) were added in 0.98. |
| 2081 | // |
| 2082 | if (Hi == Memory) |
| 2083 | Lo = Memory; |
| 2084 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2085 | Lo = Memory; |
| 2086 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2087 | Lo = Memory; |
| 2088 | if (Hi == SSEUp && Lo != SSE) |
| 2089 | Hi = SSE; |
| 2090 | } |
| 2091 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2092 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2093 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2094 | // classified recursively so that always two fields are |
| 2095 | // considered. The resulting class is calculated according to |
| 2096 | // the classes of the fields in the eightbyte: |
| 2097 | // |
| 2098 | // (a) If both classes are equal, this is the resulting class. |
| 2099 | // |
| 2100 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2101 | // the other class. |
| 2102 | // |
| 2103 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2104 | // class. |
| 2105 | // |
| 2106 | // (d) If one of the classes is INTEGER, the result is the |
| 2107 | // INTEGER. |
| 2108 | // |
| 2109 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2110 | // MEMORY is used as class. |
| 2111 | // |
| 2112 | // (f) Otherwise class SSE is used. |
| 2113 | |
| 2114 | // Accum should never be memory (we should have returned) or |
| 2115 | // ComplexX87 (because this cannot be passed in a structure). |
| 2116 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2117 | "Invalid accumulated classification during merge."); |
| 2118 | if (Accum == Field || Field == NoClass) |
| 2119 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2120 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2121 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2122 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2123 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2124 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2125 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2126 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2127 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2128 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2129 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2132 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2133 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2134 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2135 | // Class pairs with appropriate constructor methods for the various |
| 2136 | // situations. |
| 2137 | |
| 2138 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2139 | // shouldn't be passed in registers for example, so there is no chance they |
| 2140 | // can straddle an eightbyte. Verify & simplify. |
| 2141 | |
| 2142 | Lo = Hi = NoClass; |
| 2143 | |
| 2144 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2145 | Current = Memory; |
| 2146 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2147 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2148 | BuiltinType::Kind k = BT->getKind(); |
| 2149 | |
| 2150 | if (k == BuiltinType::Void) { |
| 2151 | Current = NoClass; |
| 2152 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2153 | Lo = Integer; |
| 2154 | Hi = Integer; |
| 2155 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2156 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2157 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2158 | Current = SSE; |
| 2159 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2160 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2161 | if (LDF == &llvm::APFloat::IEEEquad) { |
| 2162 | Lo = SSE; |
| 2163 | Hi = SSEUp; |
| 2164 | } else if (LDF == &llvm::APFloat::x87DoubleExtended) { |
| 2165 | Lo = X87; |
| 2166 | Hi = X87Up; |
| 2167 | } else if (LDF == &llvm::APFloat::IEEEdouble) { |
| 2168 | Current = SSE; |
| 2169 | } else |
| 2170 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2171 | } |
| 2172 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2173 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2174 | return; |
| 2175 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2176 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2177 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2178 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2179 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2180 | return; |
| 2181 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2182 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2183 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2184 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2185 | return; |
| 2186 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2187 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2188 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2189 | if (Ty->isMemberFunctionPointerType()) { |
| 2190 | if (Has64BitPointers) { |
| 2191 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2192 | // Lo and Hi now. |
| 2193 | Lo = Hi = Integer; |
| 2194 | } else { |
| 2195 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2196 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2197 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2198 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2199 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2200 | Lo = Hi = Integer; |
| 2201 | } else { |
| 2202 | Current = Integer; |
| 2203 | } |
| 2204 | } |
| 2205 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2206 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2207 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2208 | return; |
| 2209 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2210 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2211 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2212 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2213 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2214 | // gcc passes the following as integer: |
| 2215 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2216 | // 2 bytes - <2 x char>, <1 x short> |
| 2217 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2218 | Current = Integer; |
| 2219 | |
| 2220 | // If this type crosses an eightbyte boundary, it should be |
| 2221 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2222 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2223 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2224 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2225 | Hi = Lo; |
| 2226 | } else if (Size == 64) { |
| 2227 | // gcc passes <1 x double> in memory. :( |
| 2228 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 2229 | return; |
| 2230 | |
| 2231 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 2232 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 2233 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2234 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 2235 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2236 | Current = Integer; |
| 2237 | else |
| 2238 | Current = SSE; |
| 2239 | |
| 2240 | // If this type crosses an eightbyte boundary, it should be |
| 2241 | // split. |
| 2242 | if (OffsetBase && OffsetBase != 64) |
| 2243 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2244 | } else if (Size == 128 || |
| 2245 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2246 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2247 | // least significant one belongs to class SSE and all the others to class |
| 2248 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2249 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2250 | // This design isn't correct for 256-bits, but since there're no cases |
| 2251 | // where the upper parts would need to be inspected, avoid adding |
| 2252 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2253 | // |
| 2254 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2255 | // registers if they are "named", i.e. not part of the "..." of a |
| 2256 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2257 | // |
| 2258 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2259 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2260 | Lo = SSE; |
| 2261 | Hi = SSEUp; |
| 2262 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2263 | return; |
| 2264 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2265 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2266 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2267 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2268 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2269 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2270 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2271 | if (Size <= 64) |
| 2272 | Current = Integer; |
| 2273 | else if (Size <= 128) |
| 2274 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2275 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2276 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2277 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2278 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2279 | } else if (ET == getContext().LongDoubleTy) { |
| 2280 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2281 | if (LDF == &llvm::APFloat::IEEEquad) |
| 2282 | Current = Memory; |
| 2283 | else if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 2284 | Current = ComplexX87; |
| 2285 | else if (LDF == &llvm::APFloat::IEEEdouble) |
| 2286 | Lo = Hi = SSE; |
| 2287 | else |
| 2288 | llvm_unreachable("unexpected long double representation!"); |
| 2289 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2290 | |
| 2291 | // If this complex type crosses an eightbyte boundary then it |
| 2292 | // should be split. |
| 2293 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2294 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2295 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2296 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2297 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2298 | return; |
| 2299 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2300 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2301 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2302 | // Arrays are treated like structures. |
| 2303 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2304 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2305 | |
| 2306 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2307 | // than four eightbytes, ..., it has class MEMORY. |
| 2308 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2309 | return; |
| 2310 | |
| 2311 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2312 | // fields, it has class MEMORY. |
| 2313 | // |
| 2314 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2315 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2316 | return; |
| 2317 | |
| 2318 | // Otherwise implement simplified merge. We could be smarter about |
| 2319 | // this, but it isn't worth it and would be harder to verify. |
| 2320 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2321 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2322 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2323 | |
| 2324 | // The only case a 256-bit wide vector could be used is when the array |
| 2325 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2326 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2327 | if (Size > 128 && EltSize != 256) |
| 2328 | return; |
| 2329 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2330 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2331 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2332 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2333 | Lo = merge(Lo, FieldLo); |
| 2334 | Hi = merge(Hi, FieldHi); |
| 2335 | if (Lo == Memory || Hi == Memory) |
| 2336 | break; |
| 2337 | } |
| 2338 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2339 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2340 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2341 | return; |
| 2342 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2343 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2344 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2345 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2346 | |
| 2347 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2348 | // than four eightbytes, ..., it has class MEMORY. |
| 2349 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2350 | return; |
| 2351 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2352 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2353 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2354 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2355 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2356 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2357 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2358 | const RecordDecl *RD = RT->getDecl(); |
| 2359 | |
| 2360 | // Assume variable sized types are passed in memory. |
| 2361 | if (RD->hasFlexibleArrayMember()) |
| 2362 | return; |
| 2363 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2364 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2365 | |
| 2366 | // Reset Lo class, this will be recomputed. |
| 2367 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2368 | |
| 2369 | // If this is a C++ record, classify the bases first. |
| 2370 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2371 | for (const auto &I : CXXRD->bases()) { |
| 2372 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2373 | "Unexpected base class!"); |
| 2374 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2375 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2376 | |
| 2377 | // Classify this field. |
| 2378 | // |
| 2379 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2380 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2381 | // initialized to class NO_CLASS. |
| 2382 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2383 | uint64_t Offset = |
| 2384 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2385 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2386 | Lo = merge(Lo, FieldLo); |
| 2387 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2388 | if (Lo == Memory || Hi == Memory) { |
| 2389 | postMerge(Size, Lo, Hi); |
| 2390 | return; |
| 2391 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2396 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2397 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2398 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2399 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2400 | bool BitField = i->isBitField(); |
| 2401 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2402 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2403 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2404 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2405 | // The only case a 256-bit wide vector could be used is when the struct |
| 2406 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2407 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2408 | // |
| 2409 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2410 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2411 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2412 | return; |
| 2413 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2414 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2415 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2416 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2417 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2418 | return; |
| 2419 | } |
| 2420 | |
| 2421 | // Classify this field. |
| 2422 | // |
| 2423 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2424 | // exceeds a single eightbyte, each is classified |
| 2425 | // separately. Each eightbyte gets initialized to class |
| 2426 | // NO_CLASS. |
| 2427 | Class FieldLo, FieldHi; |
| 2428 | |
| 2429 | // Bit-fields require special handling, they do not force the |
| 2430 | // structure to be passed in memory even if unaligned, and |
| 2431 | // therefore they can straddle an eightbyte. |
| 2432 | if (BitField) { |
| 2433 | // Ignore padding bit-fields. |
| 2434 | if (i->isUnnamedBitfield()) |
| 2435 | continue; |
| 2436 | |
| 2437 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2438 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2439 | |
| 2440 | uint64_t EB_Lo = Offset / 64; |
| 2441 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2442 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2443 | if (EB_Lo) { |
| 2444 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2445 | FieldLo = NoClass; |
| 2446 | FieldHi = Integer; |
| 2447 | } else { |
| 2448 | FieldLo = Integer; |
| 2449 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2450 | } |
| 2451 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2452 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2453 | Lo = merge(Lo, FieldLo); |
| 2454 | Hi = merge(Hi, FieldHi); |
| 2455 | if (Lo == Memory || Hi == Memory) |
| 2456 | break; |
| 2457 | } |
| 2458 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2459 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2463 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2464 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2465 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2466 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2467 | // Treat an enum type as its underlying type. |
| 2468 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2469 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2470 | |
| 2471 | return (Ty->isPromotableIntegerType() ? |
| 2472 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2473 | } |
| 2474 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2475 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2478 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2479 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2480 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2481 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2482 | if (Size <= 64 || Size > LargestVector) |
| 2483 | return true; |
| 2484 | } |
| 2485 | |
| 2486 | return false; |
| 2487 | } |
| 2488 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2489 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2490 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2491 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2492 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2493 | // |
| 2494 | // This assumption is optimistic, as there could be free registers available |
| 2495 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2496 | // the argument in the free register. This does not seem to happen currently, |
| 2497 | // but this code would be much safer if we could mark the argument with |
| 2498 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2499 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2500 | // Treat an enum type as its underlying type. |
| 2501 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2502 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2503 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2504 | return (Ty->isPromotableIntegerType() ? |
| 2505 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2506 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2507 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2508 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2509 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2510 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2511 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2512 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2513 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2514 | |
| 2515 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2516 | // is important for good codegen. |
| 2517 | // |
| 2518 | // We do this by coercing the value into a scalar type which the backend can |
| 2519 | // handle naturally (i.e., without using byval). |
| 2520 | // |
| 2521 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2522 | // free integer registers. Doing this when there are free integer registers |
| 2523 | // would require more care, as we would have to ensure that the coerced value |
| 2524 | // did not claim the unused register. That would require either reording the |
| 2525 | // arguments to the function (so that any subsequent inreg values came first), |
| 2526 | // or only doing this optimization when there were no following arguments that |
| 2527 | // might be inreg. |
| 2528 | // |
| 2529 | // We currently expect it to be rare (particularly in well written code) for |
| 2530 | // arguments to be passed on the stack when there are still free integer |
| 2531 | // registers available (this would typically imply large structs being passed |
| 2532 | // by value), so this seems like a fair tradeoff for now. |
| 2533 | // |
| 2534 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2535 | // attributes. See PR12193. |
| 2536 | if (freeIntRegs == 0) { |
| 2537 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2538 | |
| 2539 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2540 | // type, which will end up on the stack (with alignment 8). |
| 2541 | if (Align == 8 && Size <= 64) |
| 2542 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2543 | Size)); |
| 2544 | } |
| 2545 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2546 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2549 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2550 | /// register. Pick an LLVM IR type that will be passed as a vector register. |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2551 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2552 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2553 | // vectors; strip them off if present. |
| 2554 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2555 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2556 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2557 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2558 | if (isa<llvm::VectorType>(IRType) || |
| 2559 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2560 | return IRType; |
| 2561 | |
| 2562 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2563 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2564 | assert((Size == 128 || Size == 256) && "Invalid type found!"); |
| 2565 | |
| 2566 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2567 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2568 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2571 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2572 | /// is known to either be off the end of the specified type or being in |
| 2573 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2574 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2575 | /// classification that put one of the two halves in the INTEGER class. |
| 2576 | /// |
| 2577 | /// It is conservatively correct to return false. |
| 2578 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2579 | unsigned EndBit, ASTContext &Context) { |
| 2580 | // If the bytes being queried are off the end of the type, there is no user |
| 2581 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2582 | // types that don't contain interesting padding. |
| 2583 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2584 | if (TySize <= StartBit) |
| 2585 | return true; |
| 2586 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2587 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2588 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2589 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2590 | |
| 2591 | // Check each element to see if the element overlaps with the queried range. |
| 2592 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2593 | // If the element is after the span we care about, then we're done.. |
| 2594 | unsigned EltOffset = i*EltSize; |
| 2595 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2596 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2597 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2598 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2599 | EndBit-EltOffset, Context)) |
| 2600 | return false; |
| 2601 | } |
| 2602 | // If it overlaps no elements, then it is safe to process as padding. |
| 2603 | return true; |
| 2604 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2606 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2607 | const RecordDecl *RD = RT->getDecl(); |
| 2608 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2609 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2610 | // If this is a C++ record, check the bases first. |
| 2611 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2612 | for (const auto &I : CXXRD->bases()) { |
| 2613 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2614 | "Unexpected base class!"); |
| 2615 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2616 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2617 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2618 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2619 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2620 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2621 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2622 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2623 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2624 | EndBit-BaseOffset, Context)) |
| 2625 | return false; |
| 2626 | } |
| 2627 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2629 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2630 | // this could be sped up a lot by being smarter about queried fields, |
| 2631 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2632 | // much. |
| 2633 | unsigned idx = 0; |
| 2634 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2635 | i != e; ++i, ++idx) { |
| 2636 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2637 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2638 | // If we found a field after the region we care about, then we're done. |
| 2639 | if (FieldOffset >= EndBit) break; |
| 2640 | |
| 2641 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2642 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2643 | Context)) |
| 2644 | return false; |
| 2645 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2646 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2647 | // If nothing in this record overlapped the area of interest, then we're |
| 2648 | // clean. |
| 2649 | return true; |
| 2650 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2651 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2652 | return false; |
| 2653 | } |
| 2654 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2655 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2656 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2657 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2658 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2659 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2660 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2661 | // Base case if we find a float. |
| 2662 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2663 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2664 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2665 | // If this is a struct, recurse into the field at the specified offset. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2666 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2667 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2668 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2669 | IROffset -= SL->getElementOffset(Elt); |
| 2670 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2671 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2672 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2673 | // If this is an array, recurse into the field at the specified offset. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2674 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2675 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2676 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2677 | IROffset -= IROffset/EltSize*EltSize; |
| 2678 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2679 | } |
| 2680 | |
| 2681 | return false; |
| 2682 | } |
| 2683 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2684 | |
| 2685 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2686 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2687 | llvm::Type *X86_64ABIInfo:: |
| 2688 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2689 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2690 | // The only three choices we have are either double, <2 x float>, or float. We |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2691 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2692 | // structs that contain 3 floats. |
| 2693 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2694 | SourceOffset*8+64, getContext())) |
| 2695 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2696 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2697 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2698 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2699 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2700 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2701 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2702 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2703 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2704 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2705 | } |
| 2706 | |
| 2707 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2708 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2709 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2710 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2711 | /// the best LLVM IR type to represent this, which may be i64 or may be anything |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2712 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2713 | /// etc). |
| 2714 | /// |
| 2715 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2716 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2717 | /// the 8-byte value references. PrefType may be null. |
| 2718 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2719 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2720 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2721 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2722 | llvm::Type *X86_64ABIInfo:: |
| 2723 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2724 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2725 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2726 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2727 | if (IROffset == 0) { |
| 2728 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2729 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2730 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2731 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2732 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2733 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2734 | // goodness in the source type is just tail padding. This is allowed to |
| 2735 | // kick in for struct {double,int} on the int, but not on |
| 2736 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2737 | // have to do this analysis on the source type because we can't depend on |
| 2738 | // unions being lowered a specific way etc. |
| 2739 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2740 | IRType->isIntegerTy(32) || |
| 2741 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2742 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2743 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2744 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2745 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2746 | SourceOffset*8+64, getContext())) |
| 2747 | return IRType; |
| 2748 | } |
| 2749 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2750 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2751 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2752 | // If this is a struct, recurse into the field at the specified offset. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2753 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2754 | if (IROffset < SL->getSizeInBytes()) { |
| 2755 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2756 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2757 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2758 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2759 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2760 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2761 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2762 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2763 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2764 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2765 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2766 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2767 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2768 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2769 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2770 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2771 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2772 | // integer register that isn't too big to fit the rest of the struct. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2773 | unsigned TySizeInBytes = |
| 2774 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2775 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2776 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2777 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2778 | // It is always safe to classify this as an integer type up to i64 that |
| 2779 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2780 | return llvm::IntegerType::get(getVMContext(), |
| 2781 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2784 | |
| 2785 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2786 | /// be used as elements of a two register pair to pass or return, return a |
| 2787 | /// first class aggregate to represent them. For example, if the low part of |
| 2788 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2789 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2790 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2791 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2792 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2793 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2794 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2795 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2796 | // the second element at offset 8. Check for this: |
| 2797 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2798 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 2799 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2800 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2801 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2802 | // To handle this, we have to increase the size of the low part so that the |
| 2803 | // second element will start at an 8 byte offset. We can't increase the size |
| 2804 | // of the second element because it might make us access off the end of the |
| 2805 | // struct. |
| 2806 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 2807 | // There are usually two sorts of types the ABI generation code can produce |
| 2808 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 2809 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 2810 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2811 | // Promote these to a larger type. |
| 2812 | if (Lo->isFloatTy()) |
| 2813 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2814 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 2815 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 2816 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2817 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2818 | } |
| 2819 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2820 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2821 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2822 | |
| 2823 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2824 | // Verify that the second element is at an 8-byte offset. |
| 2825 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2826 | "Invalid x86-64 argument pair!"); |
| 2827 | return Result; |
| 2828 | } |
| 2829 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2830 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2831 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2832 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2833 | // classification algorithm. |
| 2834 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2835 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2836 | |
| 2837 | // Check some invariants. |
| 2838 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2839 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2840 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2841 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2842 | switch (Lo) { |
| 2843 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2844 | if (Hi == NoClass) |
| 2845 | return ABIArgInfo::getIgnore(); |
| 2846 | // If the low part is just padding, it takes no register, leave ResType |
| 2847 | // null. |
| 2848 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2849 | "Unknown missing lo part"); |
| 2850 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2851 | |
| 2852 | case SSEUp: |
| 2853 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2854 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2855 | |
| 2856 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2857 | // hidden argument. |
| 2858 | case Memory: |
| 2859 | return getIndirectReturnResult(RetTy); |
| 2860 | |
| 2861 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2862 | // available register of the sequence %rax, %rdx is used. |
| 2863 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2864 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2865 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2866 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2867 | // so that the parameter gets the right LLVM IR attributes. |
| 2868 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2869 | // Treat an enum type as its underlying type. |
| 2870 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2871 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2872 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2873 | if (RetTy->isIntegralOrEnumerationType() && |
| 2874 | RetTy->isPromotableIntegerType()) |
| 2875 | return ABIArgInfo::getExtend(); |
| 2876 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2877 | break; |
| 2878 | |
| 2879 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2880 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2881 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2882 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2883 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2884 | |
| 2885 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2886 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2887 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2888 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2889 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2890 | |
| 2891 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2892 | // part of the value is returned in %st0 and the imaginary part in |
| 2893 | // %st1. |
| 2894 | case ComplexX87: |
| 2895 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2896 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2897 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2898 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2899 | break; |
| 2900 | } |
| 2901 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2902 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2903 | switch (Hi) { |
| 2904 | // Memory was handled previously and X87 should |
| 2905 | // never occur as a hi class. |
| 2906 | case Memory: |
| 2907 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2908 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2909 | |
| 2910 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2911 | case NoClass: |
| 2912 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2913 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2914 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2915 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2916 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2917 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2918 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2919 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2920 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2921 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2922 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2923 | break; |
| 2924 | |
| 2925 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2926 | // is passed in the next available eightbyte chunk if the last used |
| 2927 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2928 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2929 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2930 | case SSEUp: |
| 2931 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2932 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2933 | break; |
| 2934 | |
| 2935 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2936 | // returned together with the previous X87 value in %st0. |
| 2937 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2938 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2939 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2940 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2941 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2942 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2943 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2944 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2945 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2946 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2947 | break; |
| 2948 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2949 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2950 | // If a high part was specified, merge it together with the low part. It is |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2951 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2952 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2953 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2954 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2955 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2956 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2959 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2960 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2961 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2962 | const |
| 2963 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 2964 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 2965 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2966 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2967 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2968 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2969 | // Check some invariants. |
| 2970 | // FIXME: Enforce these by construction. |
| 2971 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2972 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2973 | |
| 2974 | neededInt = 0; |
| 2975 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2976 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2977 | switch (Lo) { |
| 2978 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2979 | if (Hi == NoClass) |
| 2980 | return ABIArgInfo::getIgnore(); |
| 2981 | // If the low part is just padding, it takes no register, leave ResType |
| 2982 | // null. |
| 2983 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2984 | "Unknown missing lo part"); |
| 2985 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2986 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2987 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2988 | // on the stack. |
| 2989 | case Memory: |
| 2990 | |
| 2991 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2992 | // COMPLEX_X87, it is passed in memory. |
| 2993 | case X87: |
| 2994 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2995 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2996 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2997 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2998 | |
| 2999 | case SSEUp: |
| 3000 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3001 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3002 | |
| 3003 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3004 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3005 | // and %r9 is used. |
| 3006 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3007 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3008 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3009 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3010 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3011 | |
| 3012 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3013 | // so that the parameter gets the right LLVM IR attributes. |
| 3014 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3015 | // Treat an enum type as its underlying type. |
| 3016 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3017 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3018 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3019 | if (Ty->isIntegralOrEnumerationType() && |
| 3020 | Ty->isPromotableIntegerType()) |
| 3021 | return ABIArgInfo::getExtend(); |
| 3022 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3023 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3024 | break; |
| 3025 | |
| 3026 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3027 | // available SSE register is used, the registers are taken in the |
| 3028 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3029 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3030 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3031 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3032 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3033 | break; |
| 3034 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3035 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3036 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3037 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3038 | switch (Hi) { |
| 3039 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3040 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3041 | // which is passed in memory. |
| 3042 | case Memory: |
| 3043 | case X87: |
| 3044 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3045 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3046 | |
| 3047 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3048 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3049 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3050 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3051 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3052 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3053 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3054 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3055 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3056 | break; |
| 3057 | |
| 3058 | // X87Up generally doesn't occur here (long double is passed in |
| 3059 | // memory), except in situations involving unions. |
| 3060 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3061 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3062 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3063 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3064 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3065 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3066 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3067 | ++neededSSE; |
| 3068 | break; |
| 3069 | |
| 3070 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3071 | // eightbyte is passed in the upper half of the last used SSE |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3072 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3073 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3074 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3075 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3076 | break; |
| 3077 | } |
| 3078 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3079 | // If a high part was specified, merge it together with the low part. It is |
| 3080 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3081 | // first class struct aggregate with the high and low part: {low, high} |
| 3082 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3083 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3084 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3085 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3088 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3089 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3090 | if (!getCXXABI().classifyReturnType(FI)) |
| 3091 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3092 | |
| 3093 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3094 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3095 | |
| 3096 | // If the return value is indirect, then the hidden argument is consuming one |
| 3097 | // integer register. |
| 3098 | if (FI.getReturnInfo().isIndirect()) |
| 3099 | --freeIntRegs; |
| 3100 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3101 | // The chain argument effectively gives us another free register. |
| 3102 | if (FI.isChainCall()) |
| 3103 | ++freeIntRegs; |
| 3104 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3105 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3106 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3107 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3108 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3109 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3110 | it != ie; ++it, ++ArgNo) { |
| 3111 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3112 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3113 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3114 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3115 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3116 | |
| 3117 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3118 | // eightbyte of an argument, the whole argument is passed on the |
| 3119 | // stack. If registers have already been assigned for some |
| 3120 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3121 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3122 | freeIntRegs -= neededInt; |
| 3123 | freeSSERegs -= neededSSE; |
| 3124 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3125 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3126 | } |
| 3127 | } |
| 3128 | } |
| 3129 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3130 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3131 | Address VAListAddr, QualType Ty) { |
| 3132 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3133 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3134 | llvm::Value *overflow_arg_area = |
| 3135 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3136 | |
| 3137 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3138 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3139 | // It isn't stated explicitly in the standard, but in practice we use |
| 3140 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3141 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3142 | if (Align > CharUnits::fromQuantity(8)) { |
| 3143 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3144 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3148 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3149 | llvm::Value *Res = |
| 3150 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3151 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3152 | |
| 3153 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3154 | // l->overflow_arg_area + sizeof(type). |
| 3155 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3156 | // an 8 byte boundary. |
| 3157 | |
| 3158 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3159 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3160 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3161 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3162 | "overflow_arg_area.next"); |
| 3163 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3164 | |
| 3165 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3166 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3169 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3170 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3171 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3172 | // struct { |
| 3173 | // i32 gp_offset; |
| 3174 | // i32 fp_offset; |
| 3175 | // i8* overflow_arg_area; |
| 3176 | // i8* reg_save_area; |
| 3177 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3178 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3179 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3180 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3181 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3182 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3183 | |
| 3184 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3185 | // in the registers. If not go to step 7. |
| 3186 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3187 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3188 | |
| 3189 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3190 | // general purpose registers needed to pass type and num_fp to hold |
| 3191 | // the number of floating point registers needed. |
| 3192 | |
| 3193 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3194 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3195 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3196 | // |
| 3197 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3198 | // register save space). |
| 3199 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3200 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3201 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3202 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3203 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3204 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3205 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3206 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3207 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3208 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3209 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3213 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3214 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3215 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3216 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3217 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3218 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3219 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3220 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3221 | } |
| 3222 | |
| 3223 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3224 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3225 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3226 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3227 | |
| 3228 | // Emit code to load the value if it was passed in registers. |
| 3229 | |
| 3230 | CGF.EmitBlock(InRegBlock); |
| 3231 | |
| 3232 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3233 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3234 | // copying to a temporary location in case the parameter is passed |
| 3235 | // in different register classes or requires an alignment greater |
| 3236 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3237 | // |
| 3238 | // FIXME: This really results in shameful code when we end up needing to |
| 3239 | // collect arguments from different places; often what should result in a |
| 3240 | // simple assembling of a structure from scattered addresses has many more |
| 3241 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3242 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3243 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3244 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3245 | "reg_save_area"); |
| 3246 | |
| 3247 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3248 | if (neededInt && neededSSE) { |
| 3249 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3250 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3251 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3252 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3253 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3254 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3255 | llvm::Type *TyLo = ST->getElementType(0); |
| 3256 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3257 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3258 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3259 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3260 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3261 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3262 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3263 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3264 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3265 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3266 | // Copy the first element. |
| 3267 | llvm::Value *V = |
| 3268 | CGF.Builder.CreateDefaultAlignedLoad( |
| 3269 | CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 3270 | CGF.Builder.CreateStore(V, |
| 3271 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3272 | |
| 3273 | // Copy the second element. |
| 3274 | V = CGF.Builder.CreateDefaultAlignedLoad( |
| 3275 | CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 3276 | CharUnits Offset = CharUnits::fromQuantity( |
| 3277 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3278 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3279 | |
| 3280 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3281 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3282 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3283 | CharUnits::fromQuantity(8)); |
| 3284 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3285 | |
| 3286 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3287 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3288 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3289 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3290 | CharUnits TyAlign = SizeAlign.second; |
| 3291 | |
| 3292 | // Copy into a temporary if the type is more aligned than the |
| 3293 | // register save area. |
| 3294 | if (TyAlign.getQuantity() > 8) { |
| 3295 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3296 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3297 | RegAddr = Tmp; |
| 3298 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3299 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3300 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3301 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3302 | CharUnits::fromQuantity(16)); |
| 3303 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3304 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3305 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3306 | // SSE registers are spaced 16 bytes apart in the register save |
| 3307 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3308 | // The ABI isn't explicit about this, but it seems reasonable |
| 3309 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3310 | // naturally 16-byte aligned and the prologue is expected to store |
| 3311 | // all the SSE registers to the RSA. |
| 3312 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3313 | CharUnits::fromQuantity(16)); |
| 3314 | Address RegAddrHi = |
| 3315 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3316 | CharUnits::fromQuantity(16)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3317 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3318 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3319 | llvm::Value *V; |
| 3320 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3321 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3322 | V = CGF.Builder.CreateLoad( |
| 3323 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3324 | CGF.Builder.CreateStore(V, |
| 3325 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3326 | V = CGF.Builder.CreateLoad( |
| 3327 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3328 | CGF.Builder.CreateStore(V, |
| 3329 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3330 | |
| 3331 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3335 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3336 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3337 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3338 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3339 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3340 | gp_offset_p); |
| 3341 | } |
| 3342 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3343 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3344 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3345 | fp_offset_p); |
| 3346 | } |
| 3347 | CGF.EmitBranch(ContBlock); |
| 3348 | |
| 3349 | // Emit code to load the value if it was passed in memory. |
| 3350 | |
| 3351 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3352 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3353 | |
| 3354 | // Return the appropriate result. |
| 3355 | |
| 3356 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3357 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3358 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3359 | return ResAddr; |
| 3360 | } |
| 3361 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3362 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3363 | QualType Ty) const { |
| 3364 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3365 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3366 | CharUnits::fromQuantity(8), |
| 3367 | /*allowHigherAlign*/ false); |
| 3368 | } |
| 3369 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3370 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 3371 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3372 | |
| 3373 | if (Ty->isVoidType()) |
| 3374 | return ABIArgInfo::getIgnore(); |
| 3375 | |
| 3376 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3377 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3378 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3379 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3380 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3381 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3382 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3383 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3384 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3385 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3386 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3387 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3391 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3392 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3393 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3394 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3395 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3396 | // other targets. |
| 3397 | const Type *Base = nullptr; |
| 3398 | uint64_t NumElts = 0; |
| 3399 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3400 | if (FreeSSERegs >= NumElts) { |
| 3401 | FreeSSERegs -= NumElts; |
| 3402 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3403 | return ABIArgInfo::getDirect(); |
| 3404 | return ABIArgInfo::getExpand(); |
| 3405 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3406 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3410 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3411 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3412 | // directly. |
| 3413 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3414 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3415 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3418 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3419 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3420 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3421 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3422 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3423 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3424 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3425 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3428 | // Bool type is always extended to the ABI, other builtin types are not |
| 3429 | // extended. |
| 3430 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3431 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3432 | return ABIArgInfo::getExtend(); |
| 3433 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3434 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3435 | // passes them indirectly through memory. |
| 3436 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3437 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 3438 | if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 3439 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3440 | } |
| 3441 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3442 | return ABIArgInfo::getDirect(); |
| 3443 | } |
| 3444 | |
| 3445 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3446 | bool IsVectorCall = |
| 3447 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3448 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3449 | // We can use up to 4 SSE return registers with vectorcall. |
| 3450 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3451 | if (!getCXXABI().classifyReturnType(FI)) |
| 3452 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3453 | |
| 3454 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3455 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3456 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3457 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3460 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3461 | QualType Ty) const { |
| 3462 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3463 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3464 | CharUnits::fromQuantity(8), |
| 3465 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3466 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3467 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3468 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3469 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3470 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3471 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3472 | bool IsSoftFloatABI; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3473 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3474 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 3475 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3476 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3477 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3478 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3479 | }; |
| 3480 | |
| 3481 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3482 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3483 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 3484 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3485 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3486 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3487 | // This is recovered from gcc output. |
| 3488 | return 1; // r1 is the dedicated stack pointer |
| 3489 | } |
| 3490 | |
| 3491 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3492 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3493 | }; |
| 3494 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3495 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3496 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3497 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 3498 | QualType Ty) const { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3499 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3500 | // TODO: Implement this. For now ignore. |
| 3501 | (void)CTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3502 | return Address::invalid(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3505 | // struct __va_list_tag { |
| 3506 | // unsigned char gpr; |
| 3507 | // unsigned char fpr; |
| 3508 | // unsigned short reserved; |
| 3509 | // void *overflow_arg_area; |
| 3510 | // void *reg_save_area; |
| 3511 | // }; |
| 3512 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3513 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3514 | bool isInt = |
| 3515 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3516 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3517 | |
| 3518 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 3519 | // with the argument-lowering code. |
| 3520 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3521 | |
| 3522 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3523 | |
| 3524 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 3525 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3526 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3527 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 3528 | } else { |
| 3529 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3530 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3531 | |
| 3532 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 3533 | |
| 3534 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3535 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3536 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 3537 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 3538 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3539 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3540 | llvm::Value *CC = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3541 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(8), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3542 | |
| 3543 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3544 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3545 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3546 | |
| 3547 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3548 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3549 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 3550 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3551 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3552 | // Case 1: consume registers. |
| 3553 | Address RegAddr = Address::invalid(); |
| 3554 | { |
| 3555 | CGF.EmitBlock(UsingRegs); |
| 3556 | |
| 3557 | Address RegSaveAreaPtr = |
| 3558 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 3559 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 3560 | CharUnits::fromQuantity(8)); |
| 3561 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 3562 | |
| 3563 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3564 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3565 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 3566 | CharUnits::fromQuantity(32)); |
| 3567 | } |
| 3568 | |
| 3569 | // Get the address of the saved value by scaling the number of |
| 3570 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3571 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3572 | llvm::Value *RegOffset = |
| 3573 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 3574 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 3575 | RegAddr.getPointer(), RegOffset), |
| 3576 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 3577 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 3578 | |
| 3579 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3580 | NumRegs = |
| 3581 | Builder.CreateAdd(NumRegs, |
| 3582 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3583 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 3584 | |
| 3585 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3586 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3587 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3588 | // Case 2: consume space in the overflow area. |
| 3589 | Address MemAddr = Address::invalid(); |
| 3590 | { |
| 3591 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3592 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3593 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 3594 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 3595 | |
| 3596 | CharUnits Size; |
| 3597 | if (!isIndirect) { |
| 3598 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3599 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3600 | } else { |
| 3601 | Size = CGF.getPointerSize(); |
| 3602 | } |
| 3603 | |
| 3604 | Address OverflowAreaAddr = |
| 3605 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3606 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3607 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3608 | // Round up address of argument to alignment |
| 3609 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3610 | if (Align > OverflowAreaAlign) { |
| 3611 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 3612 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 3613 | Align); |
| 3614 | } |
| 3615 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3616 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 3617 | |
| 3618 | // Increase the overflow area. |
| 3619 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 3620 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 3621 | CGF.EmitBranch(Cont); |
| 3622 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3623 | |
| 3624 | CGF.EmitBlock(Cont); |
| 3625 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3626 | // Merge the cases with a phi. |
| 3627 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 3628 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3629 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3630 | // Load the pointer if the argument was passed indirectly. |
| 3631 | if (isIndirect) { |
| 3632 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 3633 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | return Result; |
| 3637 | } |
| 3638 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3639 | bool |
| 3640 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3641 | llvm::Value *Address) const { |
| 3642 | // This is calculated from the LLVM and GCC tables and verified |
| 3643 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3644 | |
| 3645 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3646 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3647 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3648 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3649 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3650 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3651 | |
| 3652 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3653 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3654 | |
| 3655 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3656 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3657 | |
| 3658 | // 64-76 are various 4-byte special-purpose registers: |
| 3659 | // 64: mq |
| 3660 | // 65: lr |
| 3661 | // 66: ctr |
| 3662 | // 67: ap |
| 3663 | // 68-75 cr0-7 |
| 3664 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3665 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3666 | |
| 3667 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3668 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3669 | |
| 3670 | // 109: vrsave |
| 3671 | // 110: vscr |
| 3672 | // 111: spe_acc |
| 3673 | // 112: spefscr |
| 3674 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3675 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3676 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3677 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3678 | } |
| 3679 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3680 | // PowerPC-64 |
| 3681 | |
| 3682 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3683 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3684 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3685 | public: |
| 3686 | enum ABIKind { |
| 3687 | ELFv1 = 0, |
| 3688 | ELFv2 |
| 3689 | }; |
| 3690 | |
| 3691 | private: |
| 3692 | static const unsigned GPRBits = 64; |
| 3693 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3694 | bool HasQPX; |
| 3695 | |
| 3696 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3697 | // will be passed in a QPX register. |
| 3698 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3699 | if (!HasQPX) |
| 3700 | return false; |
| 3701 | |
| 3702 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3703 | unsigned NumElements = VT->getNumElements(); |
| 3704 | if (NumElements == 1) |
| 3705 | return false; |
| 3706 | |
| 3707 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3708 | if (getContext().getTypeSize(Ty) <= 256) |
| 3709 | return true; |
| 3710 | } else if (VT->getElementType()-> |
| 3711 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3712 | if (getContext().getTypeSize(Ty) <= 128) |
| 3713 | return true; |
| 3714 | } |
| 3715 | } |
| 3716 | |
| 3717 | return false; |
| 3718 | } |
| 3719 | |
| 3720 | bool IsQPXVectorTy(QualType Ty) const { |
| 3721 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3722 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3723 | |
| 3724 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3725 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
| 3726 | : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3727 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3728 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3729 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3730 | |
| 3731 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3732 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3733 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3734 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3735 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3736 | uint64_t Members) const override; |
| 3737 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3738 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3739 | // Example: For aggregate arguments that fit in a register, we could |
| 3740 | // use getDirectInReg (as is done below for structs containing a single |
| 3741 | // floating-point value) to avoid pushing them to memory on function |
| 3742 | // entry. This would require changing the logic in PPCISelLowering |
| 3743 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3744 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3745 | if (!getCXXABI().classifyReturnType(FI)) |
| 3746 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3747 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3748 | // We rely on the default argument classification for the most part. |
| 3749 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3750 | // or vector item must be passed in a register if one is available. |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3751 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3752 | if (T) { |
| 3753 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3754 | if (IsQPXVectorTy(T) || |
| 3755 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3756 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3757 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3758 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3759 | continue; |
| 3760 | } |
| 3761 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3762 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3763 | } |
| 3764 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3765 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3766 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3767 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3768 | }; |
| 3769 | |
| 3770 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3771 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3772 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3773 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3774 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 3775 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3776 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3777 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3778 | // This is recovered from gcc output. |
| 3779 | return 1; // r1 is the dedicated stack pointer |
| 3780 | } |
| 3781 | |
| 3782 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3783 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3784 | }; |
| 3785 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3786 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3787 | public: |
| 3788 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3789 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3790 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3791 | // This is recovered from gcc output. |
| 3792 | return 1; // r1 is the dedicated stack pointer |
| 3793 | } |
| 3794 | |
| 3795 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3796 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3797 | }; |
| 3798 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3799 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3800 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3801 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3802 | // extended to 64 bits. |
| 3803 | bool |
| 3804 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3805 | // Treat an enum type as its underlying type. |
| 3806 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3807 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3808 | |
| 3809 | // Promotable integer types are required to be promoted by the ABI. |
| 3810 | if (Ty->isPromotableIntegerType()) |
| 3811 | return true; |
| 3812 | |
| 3813 | // In addition to the usual promotable integer types, we also need to |
| 3814 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3815 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3816 | switch (BT->getKind()) { |
| 3817 | case BuiltinType::Int: |
| 3818 | case BuiltinType::UInt: |
| 3819 | return true; |
| 3820 | default: |
| 3821 | break; |
| 3822 | } |
| 3823 | |
| 3824 | return false; |
| 3825 | } |
| 3826 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3827 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 3828 | /// higher alignment in the parameter area. Always returns at least 8. |
| 3829 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3830 | // Complex types are passed just like their elements. |
| 3831 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3832 | Ty = CTy->getElementType(); |
| 3833 | |
| 3834 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3835 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3836 | if (IsQPXVectorTy(Ty)) { |
| 3837 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3838 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3839 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3840 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3841 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3842 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3843 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3844 | |
| 3845 | // For single-element float/vector structs, we consider the whole type |
| 3846 | // to have the same alignment requirements as its single element. |
| 3847 | const Type *AlignAsType = nullptr; |
| 3848 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3849 | if (EltType) { |
| 3850 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3851 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3852 | getContext().getTypeSize(EltType) == 128) || |
| 3853 | (BT && BT->isFloatingPoint())) |
| 3854 | AlignAsType = EltType; |
| 3855 | } |
| 3856 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3857 | // Likewise for ELFv2 homogeneous aggregates. |
| 3858 | const Type *Base = nullptr; |
| 3859 | uint64_t Members = 0; |
| 3860 | if (!AlignAsType && Kind == ELFv2 && |
| 3861 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3862 | AlignAsType = Base; |
| 3863 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3864 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3865 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 3866 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3867 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3868 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3869 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3870 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3871 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3872 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3873 | |
| 3874 | // Otherwise, we only need alignment for any aggregate type that |
| 3875 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3876 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 3877 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3878 | return CharUnits::fromQuantity(32); |
| 3879 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3880 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3881 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3882 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3883 | } |
| 3884 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3885 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3886 | /// aggregate. Base is set to the base element type, and Members is set |
| 3887 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3888 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3889 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3890 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3891 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3892 | if (NElements == 0) |
| 3893 | return false; |
| 3894 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3895 | return false; |
| 3896 | Members *= NElements; |
| 3897 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3898 | const RecordDecl *RD = RT->getDecl(); |
| 3899 | if (RD->hasFlexibleArrayMember()) |
| 3900 | return false; |
| 3901 | |
| 3902 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 3903 | |
| 3904 | // If this is a C++ record, check the bases first. |
| 3905 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 3906 | for (const auto &I : CXXRD->bases()) { |
| 3907 | // Ignore empty records. |
| 3908 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 3909 | continue; |
| 3910 | |
| 3911 | uint64_t FldMembers; |
| 3912 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 3913 | return false; |
| 3914 | |
| 3915 | Members += FldMembers; |
| 3916 | } |
| 3917 | } |
| 3918 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3919 | for (const auto *FD : RD->fields()) { |
| 3920 | // Ignore (non-zero arrays of) empty records. |
| 3921 | QualType FT = FD->getType(); |
| 3922 | while (const ConstantArrayType *AT = |
| 3923 | getContext().getAsConstantArrayType(FT)) { |
| 3924 | if (AT->getSize().getZExtValue() == 0) |
| 3925 | return false; |
| 3926 | FT = AT->getElementType(); |
| 3927 | } |
| 3928 | if (isEmptyRecord(getContext(), FT, true)) |
| 3929 | continue; |
| 3930 | |
| 3931 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3932 | if (getContext().getLangOpts().CPlusPlus && |
| 3933 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3934 | continue; |
| 3935 | |
| 3936 | uint64_t FldMembers; |
| 3937 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3938 | return false; |
| 3939 | |
| 3940 | Members = (RD->isUnion() ? |
| 3941 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3942 | } |
| 3943 | |
| 3944 | if (!Base) |
| 3945 | return false; |
| 3946 | |
| 3947 | // Ensure there is no padding. |
| 3948 | if (getContext().getTypeSize(Base) * Members != |
| 3949 | getContext().getTypeSize(Ty)) |
| 3950 | return false; |
| 3951 | } else { |
| 3952 | Members = 1; |
| 3953 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3954 | Members = 2; |
| 3955 | Ty = CT->getElementType(); |
| 3956 | } |
| 3957 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3958 | // Most ABIs only support float, double, and some vector type widths. |
| 3959 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3960 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3961 | |
| 3962 | // The base type must be the same for all members. Types that |
| 3963 | // agree in both total size and mode (float vs. vector) are |
| 3964 | // treated as being equivalent here. |
| 3965 | const Type *TyPtr = Ty.getTypePtr(); |
| 3966 | if (!Base) |
| 3967 | Base = TyPtr; |
| 3968 | |
| 3969 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3970 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3971 | return false; |
| 3972 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3973 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 3974 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3975 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3976 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 3977 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3978 | // double, long double, or 128-bit vectors. |
| 3979 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3980 | if (BT->getKind() == BuiltinType::Float || |
| 3981 | BT->getKind() == BuiltinType::Double || |
| 3982 | BT->getKind() == BuiltinType::LongDouble) |
| 3983 | return true; |
| 3984 | } |
| 3985 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3986 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3987 | return true; |
| 3988 | } |
| 3989 | return false; |
| 3990 | } |
| 3991 | |
| 3992 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 3993 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3994 | // Vector types require one register, floating point types require one |
| 3995 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3996 | uint32_t NumRegs = |
| 3997 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3998 | |
| 3999 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4000 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4003 | ABIArgInfo |
| 4004 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4005 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4006 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4007 | if (Ty->isAnyComplexType()) |
| 4008 | return ABIArgInfo::getDirect(); |
| 4009 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4010 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4011 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4012 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4013 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4014 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4015 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4016 | else if (Size < 128) { |
| 4017 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4018 | return ABIArgInfo::getDirect(CoerceTy); |
| 4019 | } |
| 4020 | } |
| 4021 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4022 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4023 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4024 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4025 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4026 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4027 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4028 | |
| 4029 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4030 | const Type *Base = nullptr; |
| 4031 | uint64_t Members = 0; |
| 4032 | if (Kind == ELFv2 && |
| 4033 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4034 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4035 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4036 | return ABIArgInfo::getDirect(CoerceTy); |
| 4037 | } |
| 4038 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4039 | // If an aggregate may end up fully in registers, we do not |
| 4040 | // use the ByVal method, but pass the aggregate as array. |
| 4041 | // This is usually beneficial since we avoid forcing the |
| 4042 | // back-end to store the argument to memory. |
| 4043 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4044 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4045 | llvm::Type *CoerceTy; |
| 4046 | |
| 4047 | // Types up to 8 bytes are passed as integer type (which will be |
| 4048 | // properly aligned in the argument save area doubleword). |
| 4049 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4050 | CoerceTy = |
| 4051 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4052 | // Larger types are passed as arrays, with the base type selected |
| 4053 | // according to the required alignment in the save area. |
| 4054 | else { |
| 4055 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4056 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4057 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4058 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4059 | } |
| 4060 | |
| 4061 | return ABIArgInfo::getDirect(CoerceTy); |
| 4062 | } |
| 4063 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4064 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4065 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4066 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4067 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | return (isPromotableTypeForABI(Ty) ? |
| 4071 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4072 | } |
| 4073 | |
| 4074 | ABIArgInfo |
| 4075 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4076 | if (RetTy->isVoidType()) |
| 4077 | return ABIArgInfo::getIgnore(); |
| 4078 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4079 | if (RetTy->isAnyComplexType()) |
| 4080 | return ABIArgInfo::getDirect(); |
| 4081 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4082 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4083 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4084 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4085 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4086 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4087 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4088 | else if (Size < 128) { |
| 4089 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4090 | return ABIArgInfo::getDirect(CoerceTy); |
| 4091 | } |
| 4092 | } |
| 4093 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4094 | if (isAggregateTypeForABI(RetTy)) { |
| 4095 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4096 | const Type *Base = nullptr; |
| 4097 | uint64_t Members = 0; |
| 4098 | if (Kind == ELFv2 && |
| 4099 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4100 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4101 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4102 | return ABIArgInfo::getDirect(CoerceTy); |
| 4103 | } |
| 4104 | |
| 4105 | // ELFv2 small aggregates are returned in up to two registers. |
| 4106 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4107 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4108 | if (Bits == 0) |
| 4109 | return ABIArgInfo::getIgnore(); |
| 4110 | |
| 4111 | llvm::Type *CoerceTy; |
| 4112 | if (Bits > GPRBits) { |
| 4113 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 4114 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4115 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4116 | CoerceTy = |
| 4117 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4118 | return ABIArgInfo::getDirect(CoerceTy); |
| 4119 | } |
| 4120 | |
| 4121 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4122 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4123 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4124 | |
| 4125 | return (isPromotableTypeForABI(RetTy) ? |
| 4126 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4127 | } |
| 4128 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4129 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4130 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4131 | QualType Ty) const { |
| 4132 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4133 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4134 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4135 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4136 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4137 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4138 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4139 | // in separate doublewords. However, Clang expects us to produce a |
| 4140 | // pointer to a structure with the two parts packed tightly. So generate |
| 4141 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4142 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4143 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4144 | CharUnits EltSize = TypeInfo.first / 2; |
| 4145 | if (EltSize < SlotSize) { |
| 4146 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4147 | SlotSize * 2, SlotSize, |
| 4148 | SlotSize, /*AllowHigher*/ true); |
| 4149 | |
| 4150 | Address RealAddr = Addr; |
| 4151 | Address ImagAddr = RealAddr; |
| 4152 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4153 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4154 | SlotSize - EltSize); |
| 4155 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4156 | 2 * SlotSize - EltSize); |
| 4157 | } else { |
| 4158 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4159 | } |
| 4160 | |
| 4161 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4162 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4163 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4164 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4165 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4166 | |
| 4167 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4168 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4169 | /*init*/ true); |
| 4170 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4171 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4174 | // Otherwise, just use the general rule. |
| 4175 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4176 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | static bool |
| 4180 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4181 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4182 | // This is calculated from the LLVM and GCC tables and verified |
| 4183 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4184 | |
| 4185 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4186 | |
| 4187 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4188 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4189 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4190 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4191 | |
| 4192 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4193 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4194 | |
| 4195 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4196 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4197 | |
| 4198 | // 64-76 are various 4-byte special-purpose registers: |
| 4199 | // 64: mq |
| 4200 | // 65: lr |
| 4201 | // 66: ctr |
| 4202 | // 67: ap |
| 4203 | // 68-75 cr0-7 |
| 4204 | // 76: xer |
| 4205 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 4206 | |
| 4207 | // 77-108: v0-31, the 16-byte vector registers |
| 4208 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4209 | |
| 4210 | // 109: vrsave |
| 4211 | // 110: vscr |
| 4212 | // 111: spe_acc |
| 4213 | // 112: spefscr |
| 4214 | // 113: sfp |
| 4215 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 4216 | |
| 4217 | return false; |
| 4218 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4219 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4220 | bool |
| 4221 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4222 | CodeGen::CodeGenFunction &CGF, |
| 4223 | llvm::Value *Address) const { |
| 4224 | |
| 4225 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4226 | } |
| 4227 | |
| 4228 | bool |
| 4229 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4230 | llvm::Value *Address) const { |
| 4231 | |
| 4232 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4233 | } |
| 4234 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4235 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4236 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4237 | //===----------------------------------------------------------------------===// |
| 4238 | |
| 4239 | namespace { |
| 4240 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4241 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4242 | public: |
| 4243 | enum ABIKind { |
| 4244 | AAPCS = 0, |
| 4245 | DarwinPCS |
| 4246 | }; |
| 4247 | |
| 4248 | private: |
| 4249 | ABIKind Kind; |
| 4250 | |
| 4251 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4252 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4253 | |
| 4254 | private: |
| 4255 | ABIKind getABIKind() const { return Kind; } |
| 4256 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4257 | |
| 4258 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4259 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4260 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4261 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4262 | uint64_t Members) const override; |
| 4263 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4264 | bool isIllegalVectorType(QualType Ty) const; |
| 4265 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4266 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4267 | if (!getCXXABI().classifyReturnType(FI)) |
| 4268 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4269 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4270 | for (auto &it : FI.arguments()) |
| 4271 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4274 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4275 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4276 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4277 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4278 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4279 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4280 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4281 | QualType Ty) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4282 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4283 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 4284 | } |
| 4285 | }; |
| 4286 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4287 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4288 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4289 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4290 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4291 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4292 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4293 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 4294 | } |
| 4295 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4296 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4297 | return 31; |
| 4298 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4299 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4300 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4301 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4302 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4303 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4304 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4305 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4306 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4307 | // Handle illegal vector types here. |
| 4308 | if (isIllegalVectorType(Ty)) { |
| 4309 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4310 | if (Size <= 32) { |
| 4311 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4312 | return ABIArgInfo::getDirect(ResType); |
| 4313 | } |
| 4314 | if (Size == 64) { |
| 4315 | llvm::Type *ResType = |
| 4316 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4317 | return ABIArgInfo::getDirect(ResType); |
| 4318 | } |
| 4319 | if (Size == 128) { |
| 4320 | llvm::Type *ResType = |
| 4321 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4322 | return ABIArgInfo::getDirect(ResType); |
| 4323 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4324 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4325 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4326 | |
| 4327 | if (!isAggregateTypeForABI(Ty)) { |
| 4328 | // Treat an enum type as its underlying type. |
| 4329 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4330 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4331 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4332 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4333 | ? ABIArgInfo::getExtend() |
| 4334 | : ABIArgInfo::getDirect()); |
| 4335 | } |
| 4336 | |
| 4337 | // Structures with either a non-trivial destructor or a non-trivial |
| 4338 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4339 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4340 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4341 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4345 | // elsewhere for GNU compatibility. |
| 4346 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4347 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4348 | return ABIArgInfo::getIgnore(); |
| 4349 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4350 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4351 | } |
| 4352 | |
| 4353 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4354 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4355 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4356 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4357 | return ABIArgInfo::getDirect( |
| 4358 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4362 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4363 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4364 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4365 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4366 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4367 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4368 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4369 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4370 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4371 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4372 | } |
| 4373 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4374 | } |
| 4375 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4376 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4379 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4380 | if (RetTy->isVoidType()) |
| 4381 | return ABIArgInfo::getIgnore(); |
| 4382 | |
| 4383 | // Large vector types should be returned via memory. |
| 4384 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4385 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4386 | |
| 4387 | if (!isAggregateTypeForABI(RetTy)) { |
| 4388 | // Treat an enum type as its underlying type. |
| 4389 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4390 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4391 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4392 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4393 | ? ABIArgInfo::getExtend() |
| 4394 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4397 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4398 | return ABIArgInfo::getIgnore(); |
| 4399 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4400 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4401 | uint64_t Members = 0; |
| 4402 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4403 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4404 | return ABIArgInfo::getDirect(); |
| 4405 | |
| 4406 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4407 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4408 | if (Size <= 128) { |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4409 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4410 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4411 | |
| 4412 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4413 | // For aggregates with 16-byte alignment, we use i128. |
| 4414 | if (Alignment < 128 && Size == 128) { |
| 4415 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4416 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4417 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4418 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4419 | } |
| 4420 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4421 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4424 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4425 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4426 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4427 | // Check whether VT is legal. |
| 4428 | unsigned NumElements = VT->getNumElements(); |
| 4429 | uint64_t Size = getContext().getTypeSize(VT); |
| 4430 | // NumElements should be power of 2 between 1 and 16. |
| 4431 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 4432 | return true; |
| 4433 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4434 | } |
| 4435 | return false; |
| 4436 | } |
| 4437 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4438 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4439 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4440 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4441 | // but with the difference that any floating-point type is allowed, |
| 4442 | // including __fp16. |
| 4443 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4444 | if (BT->isFloatingPoint()) |
| 4445 | return true; |
| 4446 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4447 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4448 | if (VecSize == 64 || VecSize == 128) |
| 4449 | return true; |
| 4450 | } |
| 4451 | return false; |
| 4452 | } |
| 4453 | |
| 4454 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4455 | uint64_t Members) const { |
| 4456 | return Members <= 4; |
| 4457 | } |
| 4458 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4459 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4460 | QualType Ty, |
| 4461 | CodeGenFunction &CGF) const { |
| 4462 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4463 | bool IsIndirect = AI.isIndirect(); |
| 4464 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4465 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4466 | if (IsIndirect) |
| 4467 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4468 | else if (AI.getCoerceToType()) |
| 4469 | BaseTy = AI.getCoerceToType(); |
| 4470 | |
| 4471 | unsigned NumRegs = 1; |
| 4472 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4473 | BaseTy = ArrTy->getElementType(); |
| 4474 | NumRegs = ArrTy->getNumElements(); |
| 4475 | } |
| 4476 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4477 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4478 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4479 | // Standard, section B.4: |
| 4480 | // |
| 4481 | // struct { |
| 4482 | // void *__stack; |
| 4483 | // void *__gr_top; |
| 4484 | // void *__vr_top; |
| 4485 | // int __gr_offs; |
| 4486 | // int __vr_offs; |
| 4487 | // }; |
| 4488 | |
| 4489 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4490 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4491 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4492 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4493 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4494 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4495 | CharUnits TyAlign = TyInfo.second; |
| 4496 | |
| 4497 | Address reg_offs_p = Address::invalid(); |
| 4498 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4499 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4500 | CharUnits reg_top_offset; |
| 4501 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4502 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4503 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4504 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4505 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 4506 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4507 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4508 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4509 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4510 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4511 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4512 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4513 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4514 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 4515 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4516 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4517 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4518 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4519 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | //======================================= |
| 4523 | // Find out where argument was passed |
| 4524 | //======================================= |
| 4525 | |
| 4526 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4527 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4528 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4529 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4530 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4531 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4532 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4533 | |
| 4534 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4535 | |
| 4536 | // Otherwise, at least some kind of argument could go in these registers, the |
Bob Wilson | 3abf169 | 2014-04-21 01:23:36 +0000 | [diff] [blame] | 4537 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4538 | CGF.EmitBlock(MaybeRegBlock); |
| 4539 | |
| 4540 | // Integer arguments may need to correct register alignment (for example a |
| 4541 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4542 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4543 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 4544 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4545 | |
| 4546 | reg_offs = CGF.Builder.CreateAdd( |
| 4547 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4548 | "align_regoffs"); |
| 4549 | reg_offs = CGF.Builder.CreateAnd( |
| 4550 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4551 | "aligned_regoffs"); |
| 4552 | } |
| 4553 | |
| 4554 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4555 | // The fact that this is done unconditionally reflects the fact that |
| 4556 | // allocating an argument to the stack also uses up all the remaining |
| 4557 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4558 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4559 | NewOffset = CGF.Builder.CreateAdd( |
| 4560 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4561 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4562 | |
| 4563 | // Now we're in a position to decide whether this argument really was in |
| 4564 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4565 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4566 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4567 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4568 | |
| 4569 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4570 | |
| 4571 | //======================================= |
| 4572 | // Argument was in registers |
| 4573 | //======================================= |
| 4574 | |
| 4575 | // Now we emit the code for if the argument was originally passed in |
| 4576 | // registers. First start the appropriate block: |
| 4577 | CGF.EmitBlock(InRegBlock); |
| 4578 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4579 | llvm::Value *reg_top = nullptr; |
| 4580 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 4581 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4582 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4583 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 4584 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 4585 | Address RegAddr = Address::invalid(); |
| 4586 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4587 | |
| 4588 | if (IsIndirect) { |
| 4589 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4590 | // stored registers or on the stack will actually be a struct **. |
| 4591 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4592 | } |
| 4593 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4594 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4595 | uint64_t NumMembers = 0; |
| 4596 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4597 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4598 | // Homogeneous aggregates passed in registers will have their elements split |
| 4599 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4600 | // qN+1, ...). We reload and store into a temporary local variable |
| 4601 | // contiguously. |
| 4602 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4603 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4604 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4605 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4606 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 4607 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4608 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4609 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 4610 | int Offset = 0; |
| 4611 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4612 | BaseTyInfo.first.getQuantity() < 16) |
| 4613 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 4614 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4615 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4616 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 4617 | Address LoadAddr = |
| 4618 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 4619 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 4620 | |
| 4621 | Address StoreAddr = |
| 4622 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4623 | |
| 4624 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4625 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4626 | } |
| 4627 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4628 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4629 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4630 | // Otherwise the object is contiguous in memory. |
| 4631 | |
| 4632 | // It might be right-aligned in its slot. |
| 4633 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 4634 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4635 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4636 | TyInfo.first < SlotSize) { |
| 4637 | CharUnits Offset = SlotSize - TyInfo.first; |
| 4638 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4641 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | CGF.EmitBranch(ContBlock); |
| 4645 | |
| 4646 | //======================================= |
| 4647 | // Argument was on the stack |
| 4648 | //======================================= |
| 4649 | CGF.EmitBlock(OnStackBlock); |
| 4650 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4651 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 4652 | CharUnits::Zero(), "stack_p"); |
| 4653 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4654 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4655 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4656 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4657 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 4658 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4659 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4660 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4661 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4662 | OnStackPtr = CGF.Builder.CreateAdd( |
| 4663 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4664 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4665 | OnStackPtr = CGF.Builder.CreateAnd( |
| 4666 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4667 | "align_stack"); |
| 4668 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4669 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4670 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4671 | Address OnStackAddr(OnStackPtr, |
| 4672 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4673 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4674 | // All stack slots are multiples of 8 bytes. |
| 4675 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 4676 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4677 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4678 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4679 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4680 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4681 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4682 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4683 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4684 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4685 | |
| 4686 | // Write the new value of __stack for the next call to va_arg |
| 4687 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4688 | |
| 4689 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4690 | TyInfo.first < StackSlotSize) { |
| 4691 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 4692 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4695 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4696 | |
| 4697 | CGF.EmitBranch(ContBlock); |
| 4698 | |
| 4699 | //======================================= |
| 4700 | // Tidy up |
| 4701 | //======================================= |
| 4702 | CGF.EmitBlock(ContBlock); |
| 4703 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4704 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 4705 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4706 | |
| 4707 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4708 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 4709 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4710 | |
| 4711 | return ResAddr; |
| 4712 | } |
| 4713 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4714 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4715 | CodeGenFunction &CGF) const { |
| 4716 | // The backend's lowering doesn't support va_arg for aggregates or |
| 4717 | // illegal vector types. Lower VAArg here for these cases and use |
| 4718 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4719 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4720 | return Address::invalid(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4721 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4722 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4723 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4724 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4725 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4726 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 4727 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 4728 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4731 | // The size of the actual thing passed, which might end up just |
| 4732 | // being a pointer for indirect types. |
| 4733 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4734 | |
| 4735 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 4736 | // aggregates should be passed indirectly. |
| 4737 | bool IsIndirect = false; |
| 4738 | if (TyInfo.first.getQuantity() > 16) { |
| 4739 | const Type *Base = nullptr; |
| 4740 | uint64_t Members = 0; |
| 4741 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4742 | } |
| 4743 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4744 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 4745 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4749 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4750 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4751 | |
| 4752 | namespace { |
| 4753 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4754 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4755 | public: |
| 4756 | enum ABIKind { |
| 4757 | APCS = 0, |
| 4758 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4759 | AAPCS_VFP = 2, |
| 4760 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4761 | }; |
| 4762 | |
| 4763 | private: |
| 4764 | ABIKind Kind; |
| 4765 | |
| 4766 | public: |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4767 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4768 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4769 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4770 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4771 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4772 | switch (getTarget().getTriple().getEnvironment()) { |
| 4773 | case llvm::Triple::Android: |
| 4774 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4775 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4776 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4777 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4778 | return true; |
| 4779 | default: |
| 4780 | return false; |
| 4781 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4784 | bool isEABIHF() const { |
| 4785 | switch (getTarget().getTriple().getEnvironment()) { |
| 4786 | case llvm::Triple::EABIHF: |
| 4787 | case llvm::Triple::GNUEABIHF: |
| 4788 | return true; |
| 4789 | default: |
| 4790 | return false; |
| 4791 | } |
| 4792 | } |
| 4793 | |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 4794 | bool isAndroid() const { |
| 4795 | return (getTarget().getTriple().getEnvironment() == |
| 4796 | llvm::Triple::Android); |
| 4797 | } |
| 4798 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4799 | ABIKind getABIKind() const { return Kind; } |
| 4800 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4801 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4802 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4803 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4804 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4805 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4806 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4807 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4808 | uint64_t Members) const override; |
| 4809 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4810 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4811 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4812 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4813 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4814 | |
| 4815 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4816 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4817 | void setCCs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4818 | }; |
| 4819 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4820 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4821 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4822 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4823 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4824 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4825 | const ARMABIInfo &getABIInfo() const { |
| 4826 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4827 | } |
| 4828 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4829 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4830 | return 13; |
| 4831 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4832 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4833 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4834 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4835 | } |
| 4836 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4837 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4838 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4839 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4840 | |
| 4841 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4842 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4843 | return false; |
| 4844 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4845 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4846 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4847 | if (getABIInfo().isEABI()) return 88; |
| 4848 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4849 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4850 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4851 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4852 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 4853 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4854 | if (!FD) |
| 4855 | return; |
| 4856 | |
| 4857 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4858 | if (!Attr) |
| 4859 | return; |
| 4860 | |
| 4861 | const char *Kind; |
| 4862 | switch (Attr->getInterrupt()) { |
| 4863 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4864 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4865 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4866 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4867 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4868 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4869 | } |
| 4870 | |
| 4871 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4872 | |
| 4873 | Fn->addFnAttr("interrupt", Kind); |
| 4874 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4875 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 4876 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4877 | return; |
| 4878 | |
| 4879 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4880 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4881 | // the backend to perform a realignment as part of the function prologue. |
| 4882 | llvm::AttrBuilder B; |
| 4883 | B.addStackAlignmentAttr(8); |
| 4884 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4885 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4886 | llvm::AttributeSet::FunctionIndex, |
| 4887 | B)); |
| 4888 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4889 | }; |
| 4890 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4891 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
| 4892 | void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV, |
| 4893 | CodeGen::CodeGenModule &CGM) const; |
| 4894 | |
| 4895 | public: |
| 4896 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4897 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 4898 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4899 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4900 | CodeGen::CodeGenModule &CGM) const override; |
| 4901 | }; |
| 4902 | |
| 4903 | void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute( |
| 4904 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4905 | if (!isa<FunctionDecl>(D)) |
| 4906 | return; |
| 4907 | if (CGM.getCodeGenOpts().StackProbeSize == 4096) |
| 4908 | return; |
| 4909 | |
| 4910 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4911 | F->addFnAttr("stack-probe-size", |
| 4912 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 4913 | } |
| 4914 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4915 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4916 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4917 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4918 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 4919 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4920 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4921 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4922 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4923 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4924 | FI.getReturnInfo() = |
| 4925 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4926 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4927 | for (auto &I : FI.arguments()) |
| 4928 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4929 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4930 | // Always honor user-specified calling convention. |
| 4931 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4932 | return; |
| 4933 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4934 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4935 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4936 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4937 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4938 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4939 | /// Return the default calling convention that LLVM will use. |
| 4940 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4941 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame^] | 4942 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4943 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4944 | else if (isEABI()) |
| 4945 | return llvm::CallingConv::ARM_AAPCS; |
| 4946 | else |
| 4947 | return llvm::CallingConv::ARM_APCS; |
| 4948 | } |
| 4949 | |
| 4950 | /// Return the calling convention that our ABI would like us to use |
| 4951 | /// as the C calling convention. |
| 4952 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4953 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4954 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4955 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4956 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4957 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4958 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4959 | llvm_unreachable("bad ABI kind"); |
| 4960 | } |
| 4961 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4962 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4963 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4964 | |
| 4965 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4966 | // they'd just match what LLVM will infer from the triple. |
| 4967 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4968 | if (abiCC != getLLVMDefaultCC()) |
| 4969 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4970 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4971 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 4972 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 4973 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
| 4974 | switch (getABIKind()) { |
| 4975 | case APCS: |
| 4976 | case AAPCS16_VFP: |
| 4977 | if (abiCC != getLLVMDefaultCC()) |
| 4978 | BuiltinCC = abiCC; |
| 4979 | break; |
| 4980 | case AAPCS: |
| 4981 | case AAPCS_VFP: |
| 4982 | BuiltinCC = llvm::CallingConv::ARM_AAPCS; |
| 4983 | break; |
| 4984 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4987 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 4988 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4989 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4990 | // A single-precision floating-point type (including promoted |
| 4991 | // half-precision types); A double-precision floating-point type; |
| 4992 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4993 | // with a Base Type of a single- or double-precision floating-point type, |
| 4994 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4995 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4996 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4997 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4998 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4999 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5000 | // Handle illegal vector types here. |
| 5001 | if (isIllegalVectorType(Ty)) { |
| 5002 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5003 | if (Size <= 32) { |
| 5004 | llvm::Type *ResType = |
| 5005 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5006 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5007 | } |
| 5008 | if (Size == 64) { |
| 5009 | llvm::Type *ResType = llvm::VectorType::get( |
| 5010 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5011 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5012 | } |
| 5013 | if (Size == 128) { |
| 5014 | llvm::Type *ResType = llvm::VectorType::get( |
| 5015 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5016 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5017 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5018 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5021 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 5022 | // unspecified. This is not done for OpenCL as it handles the half type |
| 5023 | // natively, and does not need to interwork with AAPCS code. |
| 5024 | if (Ty->isHalfType() && !getContext().getLangOpts().OpenCL) { |
| 5025 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5026 | llvm::Type::getFloatTy(getVMContext()) : |
| 5027 | llvm::Type::getInt32Ty(getVMContext()); |
| 5028 | return ABIArgInfo::getDirect(ResType); |
| 5029 | } |
| 5030 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5031 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5032 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5033 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5034 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5035 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5036 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5037 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5038 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5039 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5040 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5041 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5042 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5043 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5044 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5045 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5046 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5047 | return ABIArgInfo::getIgnore(); |
| 5048 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5049 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5050 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5051 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5052 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5053 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5054 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5055 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5056 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5057 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5058 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5059 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5060 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5061 | // this convention even for a variadic function: the backend will use GPRs |
| 5062 | // if needed. |
| 5063 | const Type *Base = nullptr; |
| 5064 | uint64_t Members = 0; |
| 5065 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5066 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5067 | llvm::Type *Ty = |
| 5068 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5069 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5070 | } |
| 5071 | } |
| 5072 | |
| 5073 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5074 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5075 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5076 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5077 | // and a pointer is passed. |
| 5078 | return ABIArgInfo::getIndirect( |
| 5079 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5082 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5083 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5084 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5085 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5086 | uint64_t ABIAlign = 4; |
| 5087 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5088 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5089 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5090 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5091 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5092 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5093 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5094 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5095 | /*ByVal=*/true, |
| 5096 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5097 | } |
| 5098 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5099 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5100 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5101 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5102 | // FIXME: Try to match the types of the arguments more accurately where |
| 5103 | // we can. |
| 5104 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5105 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5106 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5107 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5108 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5109 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5110 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5111 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5112 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5113 | } |
| 5114 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5115 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5116 | llvm::LLVMContext &VMContext) { |
| 5117 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5118 | // is called integer-like if its size is less than or equal to one word, and |
| 5119 | // the offset of each of its addressable sub-fields is zero. |
| 5120 | |
| 5121 | uint64_t Size = Context.getTypeSize(Ty); |
| 5122 | |
| 5123 | // Check that the type fits in a word. |
| 5124 | if (Size > 32) |
| 5125 | return false; |
| 5126 | |
| 5127 | // FIXME: Handle vector types! |
| 5128 | if (Ty->isVectorType()) |
| 5129 | return false; |
| 5130 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5131 | // Float types are never treated as "integer like". |
| 5132 | if (Ty->isRealFloatingType()) |
| 5133 | return false; |
| 5134 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5135 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5136 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5137 | return true; |
| 5138 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5139 | // Small complex integer types are "integer like". |
| 5140 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5141 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5142 | |
| 5143 | // Single element and zero sized arrays should be allowed, by the definition |
| 5144 | // above, but they are not. |
| 5145 | |
| 5146 | // Otherwise, it must be a record type. |
| 5147 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5148 | if (!RT) return false; |
| 5149 | |
| 5150 | // Ignore records with flexible arrays. |
| 5151 | const RecordDecl *RD = RT->getDecl(); |
| 5152 | if (RD->hasFlexibleArrayMember()) |
| 5153 | return false; |
| 5154 | |
| 5155 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5156 | // like". |
| 5157 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5158 | |
| 5159 | bool HadField = false; |
| 5160 | unsigned idx = 0; |
| 5161 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5162 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5163 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5164 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5165 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5166 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5167 | // struct { int : 0; int x } |
| 5168 | // is non-integer like according to gcc. |
| 5169 | if (FD->isBitField()) { |
| 5170 | if (!RD->isUnion()) |
| 5171 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5172 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5173 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5174 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5175 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5176 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5177 | } |
| 5178 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5179 | // Check if this field is at offset 0. |
| 5180 | if (Layout.getFieldOffset(idx) != 0) |
| 5181 | return false; |
| 5182 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5183 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5184 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5185 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5186 | // Only allow at most one field in a structure. This doesn't match the |
| 5187 | // wording above, but follows gcc in situations with a field following an |
| 5188 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5189 | if (!RD->isUnion()) { |
| 5190 | if (HadField) |
| 5191 | return false; |
| 5192 | |
| 5193 | HadField = true; |
| 5194 | } |
| 5195 | } |
| 5196 | |
| 5197 | return true; |
| 5198 | } |
| 5199 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5200 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5201 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5202 | bool IsEffectivelyAAPCS_VFP = |
| 5203 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5204 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5205 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5206 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5207 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5208 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5209 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5210 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5211 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5212 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5213 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5214 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5215 | // natively, and does not need to interwork with AAPCS code. |
| 5216 | if (RetTy->isHalfType() && !getContext().getLangOpts().OpenCL) { |
| 5217 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5218 | llvm::Type::getFloatTy(getVMContext()) : |
| 5219 | llvm::Type::getInt32Ty(getVMContext()); |
| 5220 | return ABIArgInfo::getDirect(ResType); |
| 5221 | } |
| 5222 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5223 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5224 | // Treat an enum type as its underlying type. |
| 5225 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5226 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5227 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5228 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5229 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5230 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5231 | |
| 5232 | // Are we following APCS? |
| 5233 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5234 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5235 | return ABIArgInfo::getIgnore(); |
| 5236 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5237 | // Complex types are all returned as packed integers. |
| 5238 | // |
| 5239 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5240 | // correctly. |
| 5241 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5242 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5243 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5244 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5245 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5246 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5247 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5248 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5249 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5250 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5251 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5252 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5253 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5254 | } |
| 5255 | |
| 5256 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5257 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5258 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5259 | |
| 5260 | // Otherwise this is an AAPCS variant. |
| 5261 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5262 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5263 | return ABIArgInfo::getIgnore(); |
| 5264 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5265 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5266 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5267 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5268 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5269 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5270 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5271 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5272 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5273 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5276 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5277 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5278 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5279 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5280 | if (getDataLayout().isBigEndian()) |
| 5281 | // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5282 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5283 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5284 | // Return in the smallest viable integer type. |
| 5285 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5286 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5287 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5288 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5289 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5290 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5291 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5292 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5293 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5294 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5295 | } |
| 5296 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5297 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5298 | } |
| 5299 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5300 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5301 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 5302 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 5303 | if (isAndroid()) { |
| 5304 | // Android shipped using Clang 3.1, which supported a slightly different |
| 5305 | // vector ABI. The primary differences were that 3-element vector types |
| 5306 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 5307 | // accepts that legacy behavior for Android only. |
| 5308 | // Check whether VT is legal. |
| 5309 | unsigned NumElements = VT->getNumElements(); |
| 5310 | // NumElements should be power of 2 or equal to 3. |
| 5311 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 5312 | return true; |
| 5313 | } else { |
| 5314 | // Check whether VT is legal. |
| 5315 | unsigned NumElements = VT->getNumElements(); |
| 5316 | uint64_t Size = getContext().getTypeSize(VT); |
| 5317 | // NumElements should be power of 2. |
| 5318 | if (!llvm::isPowerOf2_32(NumElements)) |
| 5319 | return true; |
| 5320 | // Size should be greater than 32 bits. |
| 5321 | return Size <= 32; |
| 5322 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5323 | } |
| 5324 | return false; |
| 5325 | } |
| 5326 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5327 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5328 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 5329 | // double, or 64-bit or 128-bit vectors. |
| 5330 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5331 | if (BT->getKind() == BuiltinType::Float || |
| 5332 | BT->getKind() == BuiltinType::Double || |
| 5333 | BT->getKind() == BuiltinType::LongDouble) |
| 5334 | return true; |
| 5335 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5336 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5337 | if (VecSize == 64 || VecSize == 128) |
| 5338 | return true; |
| 5339 | } |
| 5340 | return false; |
| 5341 | } |
| 5342 | |
| 5343 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5344 | uint64_t Members) const { |
| 5345 | return Members <= 4; |
| 5346 | } |
| 5347 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5348 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5349 | QualType Ty) const { |
| 5350 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5351 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5352 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5353 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5354 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 5355 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5356 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5359 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5360 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5361 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5362 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 5363 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5364 | const Type *Base = nullptr; |
| 5365 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5366 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 5367 | IsIndirect = true; |
| 5368 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5369 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 5370 | // allocated by the caller. |
| 5371 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 5372 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5373 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 5374 | IsIndirect = true; |
| 5375 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5376 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5377 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 5378 | // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5379 | // Our callers should be prepared to handle an under-aligned address. |
| 5380 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 5381 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5382 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5383 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 5384 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5385 | // ARMv7k allows type alignment up to 16 bytes. |
| 5386 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5387 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5388 | } else { |
| 5389 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5390 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5391 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5392 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5393 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 5394 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5395 | } |
| 5396 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5397 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5398 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5399 | //===----------------------------------------------------------------------===// |
| 5400 | |
| 5401 | namespace { |
| 5402 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5403 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5404 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5405 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5406 | |
| 5407 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5408 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5409 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5410 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5411 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5412 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5413 | }; |
| 5414 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5415 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5416 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5417 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5418 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5419 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5420 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5421 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5422 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5423 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5424 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5425 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5426 | }; |
| 5427 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5428 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5429 | if (RetTy->isVoidType()) |
| 5430 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5431 | |
| 5432 | // note: this is different from default ABI |
| 5433 | if (!RetTy->isScalarType()) |
| 5434 | return ABIArgInfo::getDirect(); |
| 5435 | |
| 5436 | // Treat an enum type as its underlying type. |
| 5437 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5438 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5439 | |
| 5440 | return (RetTy->isPromotableIntegerType() ? |
| 5441 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5442 | } |
| 5443 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5444 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5445 | // Treat an enum type as its underlying type. |
| 5446 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5447 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5448 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5449 | // Return aggregates type as indirect by value |
| 5450 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5451 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5452 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5453 | return (Ty->isPromotableIntegerType() ? |
| 5454 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5455 | } |
| 5456 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5457 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5458 | if (!getCXXABI().classifyReturnType(FI)) |
| 5459 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5460 | for (auto &I : FI.arguments()) |
| 5461 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5462 | |
| 5463 | // Always honor user-specified calling convention. |
| 5464 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5465 | return; |
| 5466 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5467 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5468 | } |
| 5469 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5470 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5471 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5472 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5473 | } |
| 5474 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5475 | void NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5476 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5477 | CodeGen::CodeGenModule &M) const{ |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5478 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5479 | if (!FD) return; |
| 5480 | |
| 5481 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5482 | |
| 5483 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5484 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5485 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5486 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5487 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5488 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5489 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5490 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5491 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5492 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5493 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5494 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5495 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5496 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5497 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5498 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5499 | // __global__ functions cannot be called from the device, we do not |
| 5500 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5501 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5502 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5503 | addNVVMMetadata(F, "kernel", 1); |
| 5504 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5505 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5506 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5507 | llvm::APSInt MaxThreads(32); |
| 5508 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5509 | if (MaxThreads > 0) |
| 5510 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5511 | |
| 5512 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5513 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5514 | // we don't have to add a PTX directive. |
| 5515 | if (Attr->getMinBlocks()) { |
| 5516 | llvm::APSInt MinBlocks(32); |
| 5517 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5518 | if (MinBlocks > 0) |
| 5519 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5520 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5521 | } |
| 5522 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5523 | } |
| 5524 | } |
| 5525 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5526 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5527 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5528 | llvm::Module *M = F->getParent(); |
| 5529 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5530 | |
| 5531 | // Get "nvvm.annotations" metadata node |
| 5532 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5533 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5534 | llvm::Metadata *MDVals[] = { |
| 5535 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5536 | llvm::ConstantAsMetadata::get( |
| 5537 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5538 | // Append metadata to nvvm.annotations |
| 5539 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5540 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5541 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5542 | |
| 5543 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5544 | // SystemZ ABI Implementation |
| 5545 | //===----------------------------------------------------------------------===// |
| 5546 | |
| 5547 | namespace { |
| 5548 | |
| 5549 | class SystemZABIInfo : public ABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5550 | bool HasVector; |
| 5551 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5552 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5553 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
| 5554 | : ABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5555 | |
| 5556 | bool isPromotableIntegerType(QualType Ty) const; |
| 5557 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5558 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5559 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5560 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5561 | |
| 5562 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5563 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5564 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5565 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5566 | if (!getCXXABI().classifyReturnType(FI)) |
| 5567 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5568 | for (auto &I : FI.arguments()) |
| 5569 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5572 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5573 | QualType Ty) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5574 | }; |
| 5575 | |
| 5576 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5577 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5578 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5579 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5580 | }; |
| 5581 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5582 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5583 | |
| 5584 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5585 | // Treat an enum type as its underlying type. |
| 5586 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5587 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5588 | |
| 5589 | // Promotable integer types are required to be promoted by the ABI. |
| 5590 | if (Ty->isPromotableIntegerType()) |
| 5591 | return true; |
| 5592 | |
| 5593 | // 32-bit values must also be promoted. |
| 5594 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5595 | switch (BT->getKind()) { |
| 5596 | case BuiltinType::Int: |
| 5597 | case BuiltinType::UInt: |
| 5598 | return true; |
| 5599 | default: |
| 5600 | return false; |
| 5601 | } |
| 5602 | return false; |
| 5603 | } |
| 5604 | |
| 5605 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5606 | return (Ty->isAnyComplexType() || |
| 5607 | Ty->isVectorType() || |
| 5608 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5611 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5612 | return (HasVector && |
| 5613 | Ty->isVectorType() && |
| 5614 | getContext().getTypeSize(Ty) <= 128); |
| 5615 | } |
| 5616 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5617 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5618 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5619 | switch (BT->getKind()) { |
| 5620 | case BuiltinType::Float: |
| 5621 | case BuiltinType::Double: |
| 5622 | return true; |
| 5623 | default: |
| 5624 | return false; |
| 5625 | } |
| 5626 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5627 | return false; |
| 5628 | } |
| 5629 | |
| 5630 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5631 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5632 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5633 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5634 | |
| 5635 | // If this is a C++ record, check the bases first. |
| 5636 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5637 | for (const auto &I : CXXRD->bases()) { |
| 5638 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5639 | |
| 5640 | // Empty bases don't affect things either way. |
| 5641 | if (isEmptyRecord(getContext(), Base, true)) |
| 5642 | continue; |
| 5643 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5644 | if (!Found.isNull()) |
| 5645 | return Ty; |
| 5646 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5650 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5651 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5652 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5653 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5654 | if (getContext().getLangOpts().CPlusPlus && |
| 5655 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5656 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5657 | |
| 5658 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5659 | // Nested structures still do though. |
| 5660 | if (!Found.isNull()) |
| 5661 | return Ty; |
| 5662 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5666 | // An 8-byte aligned struct s { float f; } is passed as a double. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5667 | if (!Found.isNull()) |
| 5668 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5669 | } |
| 5670 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5671 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5672 | } |
| 5673 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5674 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5675 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5676 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5677 | // struct { |
| 5678 | // i64 __gpr; |
| 5679 | // i64 __fpr; |
| 5680 | // i8 *__overflow_arg_area; |
| 5681 | // i8 *__reg_save_area; |
| 5682 | // }; |
| 5683 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5684 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5685 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5686 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5687 | Ty = getContext().getCanonicalType(Ty); |
| 5688 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5689 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5690 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5691 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5692 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5693 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5694 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5695 | CharUnits UnpaddedSize; |
| 5696 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5697 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5698 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 5699 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5700 | } else { |
| 5701 | if (AI.getCoerceToType()) |
| 5702 | ArgTy = AI.getCoerceToType(); |
| 5703 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5704 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5705 | UnpaddedSize = TyInfo.first; |
| 5706 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5707 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5708 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 5709 | if (IsVector && UnpaddedSize > PaddedSize) |
| 5710 | PaddedSize = CharUnits::fromQuantity(16); |
| 5711 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5712 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5713 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5714 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5715 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5716 | llvm::Value *PaddedSizeV = |
| 5717 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5718 | |
| 5719 | if (IsVector) { |
| 5720 | // Work out the address of a vector argument on the stack. |
| 5721 | // Vector arguments are always passed in the high bits of a |
| 5722 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5723 | Address OverflowArgAreaPtr = |
| 5724 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5725 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5726 | Address OverflowArgArea = |
| 5727 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5728 | TyInfo.second); |
| 5729 | Address MemAddr = |
| 5730 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5731 | |
| 5732 | // Update overflow_arg_area_ptr pointer |
| 5733 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5734 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5735 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5736 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5737 | |
| 5738 | return MemAddr; |
| 5739 | } |
| 5740 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5741 | assert(PaddedSize.getQuantity() == 8); |
| 5742 | |
| 5743 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 5744 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5745 | if (InFPRs) { |
| 5746 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5747 | RegCountField = 1; // __fpr |
| 5748 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5749 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5750 | } else { |
| 5751 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5752 | RegCountField = 0; // __gpr |
| 5753 | RegSaveIndex = 2; // save offset for r2 |
| 5754 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5755 | } |
| 5756 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5757 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 5758 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 5759 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5760 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5761 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5762 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5763 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5764 | |
| 5765 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5766 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5767 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5768 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5769 | |
| 5770 | // Emit code to load the value if it was passed in registers. |
| 5771 | CGF.EmitBlock(InRegBlock); |
| 5772 | |
| 5773 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5774 | llvm::Value *ScaledRegCount = |
| 5775 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5776 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5777 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 5778 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5779 | llvm::Value *RegOffset = |
| 5780 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5781 | Address RegSaveAreaPtr = |
| 5782 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5783 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5784 | llvm::Value *RegSaveArea = |
| 5785 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5786 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 5787 | "raw_reg_addr"), |
| 5788 | PaddedSize); |
| 5789 | Address RegAddr = |
| 5790 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5791 | |
| 5792 | // Update the register count |
| 5793 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5794 | llvm::Value *NewRegCount = |
| 5795 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5796 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5797 | CGF.EmitBranch(ContBlock); |
| 5798 | |
| 5799 | // Emit code to load the value if it was passed in memory. |
| 5800 | CGF.EmitBlock(InMemBlock); |
| 5801 | |
| 5802 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5803 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 5804 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 5805 | Address OverflowArgArea = |
| 5806 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5807 | PaddedSize); |
| 5808 | Address RawMemAddr = |
| 5809 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 5810 | Address MemAddr = |
| 5811 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5812 | |
| 5813 | // Update overflow_arg_area_ptr pointer |
| 5814 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5815 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5816 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5817 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5818 | CGF.EmitBranch(ContBlock); |
| 5819 | |
| 5820 | // Return the appropriate result. |
| 5821 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5822 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5823 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5824 | |
| 5825 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5826 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 5827 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5828 | |
| 5829 | return ResAddr; |
| 5830 | } |
| 5831 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5832 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5833 | if (RetTy->isVoidType()) |
| 5834 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5835 | if (isVectorArgumentType(RetTy)) |
| 5836 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5837 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5838 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5839 | return (isPromotableIntegerType(RetTy) ? |
| 5840 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5841 | } |
| 5842 | |
| 5843 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5844 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5845 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5846 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5847 | |
| 5848 | // Integers and enums are extended to full register width. |
| 5849 | if (isPromotableIntegerType(Ty)) |
| 5850 | return ABIArgInfo::getExtend(); |
| 5851 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5852 | // Handle vector types and vector-like structure types. Note that |
| 5853 | // as opposed to float-like structure types, we do not allow any |
| 5854 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5855 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5856 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 5857 | if (isVectorArgumentType(SingleElementTy) && |
| 5858 | getContext().getTypeSize(SingleElementTy) == Size) |
| 5859 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 5860 | |
| 5861 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5862 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5863 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5864 | |
| 5865 | // Handle small structures. |
| 5866 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5867 | // Structures with flexible arrays have variable length, so really |
| 5868 | // fail the size test above. |
| 5869 | const RecordDecl *RD = RT->getDecl(); |
| 5870 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5871 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5872 | |
| 5873 | // The structure is passed as an unextended integer, a float, or a double. |
| 5874 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5875 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5876 | assert(Size == 32 || Size == 64); |
| 5877 | if (Size == 32) |
| 5878 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5879 | else |
| 5880 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5881 | } else |
| 5882 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5883 | return ABIArgInfo::getDirect(PassTy); |
| 5884 | } |
| 5885 | |
| 5886 | // Non-structure compounds are passed indirectly. |
| 5887 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5888 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5889 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5890 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5891 | } |
| 5892 | |
| 5893 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5894 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5895 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5896 | |
| 5897 | namespace { |
| 5898 | |
| 5899 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5900 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5901 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5902 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5903 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5904 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5905 | }; |
| 5906 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5907 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5908 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5909 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5910 | llvm::GlobalValue *GV, |
| 5911 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5912 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5913 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5914 | // Handle 'interrupt' attribute: |
| 5915 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5916 | |
| 5917 | // Step 1: Set ISR calling convention. |
| 5918 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5919 | |
| 5920 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5921 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5922 | |
| 5923 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5924 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5925 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5926 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5927 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5928 | } |
| 5929 | } |
| 5930 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5931 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5932 | // MIPS ABI Implementation. This works for both little-endian and |
| 5933 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5934 | //===----------------------------------------------------------------------===// |
| 5935 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5936 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5937 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5938 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5939 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5940 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5941 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5942 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5943 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5944 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5945 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5946 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5947 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5948 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5949 | |
| 5950 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5951 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5952 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5953 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5954 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5955 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5956 | }; |
| 5957 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5958 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5959 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5960 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5961 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5962 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5963 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5964 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5965 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5966 | return 29; |
| 5967 | } |
| 5968 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5969 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5970 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5971 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5972 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5973 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5974 | if (FD->hasAttr<Mips16Attr>()) { |
| 5975 | Fn->addFnAttr("mips16"); |
| 5976 | } |
| 5977 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5978 | Fn->addFnAttr("nomips16"); |
| 5979 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5980 | |
| 5981 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 5982 | if (!Attr) |
| 5983 | return; |
| 5984 | |
| 5985 | const char *Kind; |
| 5986 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5987 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 5988 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 5989 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 5990 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 5991 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 5992 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 5993 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 5994 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 5995 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 5996 | } |
| 5997 | |
| 5998 | Fn->addFnAttr("interrupt", Kind); |
| 5999 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6000 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6001 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6002 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6003 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6004 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6005 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6006 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6007 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6008 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6009 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6010 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6011 | void MipsABIInfo::CoerceToIntArgs( |
| 6012 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6013 | llvm::IntegerType *IntTy = |
| 6014 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6015 | |
| 6016 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6017 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6018 | ArgList.push_back(IntTy); |
| 6019 | |
| 6020 | // If necessary, add one more integer type to ArgList. |
| 6021 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6022 | |
| 6023 | if (R) |
| 6024 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6025 | } |
| 6026 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6027 | // In N32/64, an aligned double precision floating point field is passed in |
| 6028 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6029 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6030 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6031 | |
| 6032 | if (IsO32) { |
| 6033 | CoerceToIntArgs(TySize, ArgList); |
| 6034 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6035 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6036 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6037 | if (Ty->isComplexType()) |
| 6038 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6039 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6040 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6041 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6042 | // Unions/vectors are passed in integer registers. |
| 6043 | if (!RT || !RT->isStructureOrClassType()) { |
| 6044 | CoerceToIntArgs(TySize, ArgList); |
| 6045 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6046 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6047 | |
| 6048 | const RecordDecl *RD = RT->getDecl(); |
| 6049 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6050 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6051 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6052 | uint64_t LastOffset = 0; |
| 6053 | unsigned idx = 0; |
| 6054 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6055 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6056 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6057 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6058 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6059 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6060 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6061 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6062 | |
| 6063 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6064 | continue; |
| 6065 | |
| 6066 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6067 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6068 | continue; |
| 6069 | |
| 6070 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6071 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6072 | ArgList.push_back(I64); |
| 6073 | |
| 6074 | // Add double type. |
| 6075 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6076 | LastOffset = Offset + 64; |
| 6077 | } |
| 6078 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6079 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6080 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6081 | |
| 6082 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6083 | } |
| 6084 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6085 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6086 | uint64_t Offset) const { |
| 6087 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6088 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6089 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6090 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6091 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6092 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6093 | ABIArgInfo |
| 6094 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6095 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6096 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6097 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6098 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6099 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6100 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6101 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6102 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6103 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6104 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6105 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6106 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6107 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6108 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6109 | return ABIArgInfo::getIgnore(); |
| 6110 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6111 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6112 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6113 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6114 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6115 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6116 | // If we have reached here, aggregates are passed directly by coercing to |
| 6117 | // another structure type. Padding is inserted if the offset of the |
| 6118 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6119 | ABIArgInfo ArgInfo = |
| 6120 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6121 | getPaddingType(OrigOffset, CurrOffset)); |
| 6122 | ArgInfo.setInReg(true); |
| 6123 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6124 | } |
| 6125 | |
| 6126 | // Treat an enum type as its underlying type. |
| 6127 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6128 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6129 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6130 | // All integral types are promoted to the GPR width. |
| 6131 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6132 | return ABIArgInfo::getExtend(); |
| 6133 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6134 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6135 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6136 | } |
| 6137 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6138 | llvm::Type* |
| 6139 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6140 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6141 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6142 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6143 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6144 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6145 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6146 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6147 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6148 | // N32/64 returns struct/classes in floating point registers if the |
| 6149 | // following conditions are met: |
| 6150 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6151 | // 2. The struct/class has one or two fields all of which are floating |
| 6152 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6153 | // 3. The offset of the first field is zero (this follows what gcc does). |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6154 | // |
| 6155 | // Any other composite results are returned in integer registers. |
| 6156 | // |
| 6157 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6158 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6159 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6160 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6161 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6162 | if (!BT || !BT->isFloatingPoint()) |
| 6163 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6164 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6165 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6166 | } |
| 6167 | |
| 6168 | if (b == e) |
| 6169 | return llvm::StructType::get(getVMContext(), RTList, |
| 6170 | RD->hasAttr<PackedAttr>()); |
| 6171 | |
| 6172 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6173 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6176 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6177 | return llvm::StructType::get(getVMContext(), RTList); |
| 6178 | } |
| 6179 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6180 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6181 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6182 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6183 | if (RetTy->isVoidType()) |
| 6184 | return ABIArgInfo::getIgnore(); |
| 6185 | |
| 6186 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6187 | // However, N32/N64 ignores zero sized return values. |
| 6188 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6189 | return ABIArgInfo::getIgnore(); |
| 6190 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6191 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6192 | if (Size <= 128) { |
| 6193 | if (RetTy->isAnyComplexType()) |
| 6194 | return ABIArgInfo::getDirect(); |
| 6195 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6196 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6197 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6198 | if (!IsO32 || |
| 6199 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6200 | ABIArgInfo ArgInfo = |
| 6201 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6202 | ArgInfo.setInReg(true); |
| 6203 | return ArgInfo; |
| 6204 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6205 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6206 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6207 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6208 | } |
| 6209 | |
| 6210 | // Treat an enum type as its underlying type. |
| 6211 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6212 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6213 | |
| 6214 | return (RetTy->isPromotableIntegerType() ? |
| 6215 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6216 | } |
| 6217 | |
| 6218 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6219 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6220 | if (!getCXXABI().classifyReturnType(FI)) |
| 6221 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6222 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6223 | // Check if a pointer to an aggregate is passed as a hidden argument. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6224 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6225 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6226 | for (auto &I : FI.arguments()) |
| 6227 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6228 | } |
| 6229 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6230 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6231 | QualType OrigTy) const { |
| 6232 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6233 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6234 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6235 | // Pointers are also promoted in the same way but this only matters for N32. |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6236 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6237 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6238 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6239 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6240 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6241 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6242 | DidPromote = true; |
| 6243 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6244 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6245 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6246 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6247 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6248 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6249 | // The alignment of things in the argument area is never larger than |
| 6250 | // StackAlignInBytes. |
| 6251 | TyInfo.second = |
| 6252 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6253 | |
| 6254 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6255 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6256 | |
| 6257 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6258 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6259 | |
| 6260 | |
| 6261 | // If there was a promotion, "unpromote" into a temporary. |
| 6262 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6263 | if (DidPromote) { |
| 6264 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6265 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6266 | |
| 6267 | // Truncate down to the right width. |
| 6268 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6269 | : CGF.IntPtrTy); |
| 6270 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6271 | if (OrigTy->isPointerType()) |
| 6272 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6273 | |
| 6274 | CGF.Builder.CreateStore(V, Temp); |
| 6275 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6276 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6277 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6278 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6279 | } |
| 6280 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6281 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6282 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6283 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6284 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 6285 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 6286 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6287 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6288 | return false; |
| 6289 | } |
| 6290 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6291 | bool |
| 6292 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6293 | llvm::Value *Address) const { |
| 6294 | // This information comes from gcc's implementation, which seems to |
| 6295 | // as canonical as it gets. |
| 6296 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6297 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 6298 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6299 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6300 | |
| 6301 | // 0-31 are the general purpose registers, $0 - $31. |
| 6302 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 6303 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 6304 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6305 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6306 | |
| 6307 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 6308 | // They are one bit wide and ignored here. |
| 6309 | |
| 6310 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 6311 | // (coprocessor 1 is the FP unit) |
| 6312 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 6313 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 6314 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6315 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6316 | return false; |
| 6317 | } |
| 6318 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6319 | //===----------------------------------------------------------------------===// |
| 6320 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6321 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6322 | // handling. |
| 6323 | //===----------------------------------------------------------------------===// |
| 6324 | |
| 6325 | namespace { |
| 6326 | |
| 6327 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 6328 | public: |
| 6329 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 6330 | : DefaultTargetCodeGenInfo(CGT) {} |
| 6331 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6332 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6333 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6334 | }; |
| 6335 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6336 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6337 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6338 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6339 | if (!FD) return; |
| 6340 | |
| 6341 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6342 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6343 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6344 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 6345 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6346 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 6347 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 6348 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6349 | // Convert the reqd_work_group_size() attributes to metadata. |
| 6350 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6351 | llvm::NamedMDNode *OpenCLMetadata = |
| 6352 | M.getModule().getOrInsertNamedMetadata( |
| 6353 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6354 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6355 | SmallVector<llvm::Metadata *, 5> Operands; |
| 6356 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6357 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6358 | Operands.push_back( |
| 6359 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6360 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 6361 | Operands.push_back( |
| 6362 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6363 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 6364 | Operands.push_back( |
| 6365 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6366 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6367 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6368 | // Add a boolean constant operand for "required" (true) or "hint" |
| 6369 | // (false) for implementing the work_group_size_hint attr later. |
| 6370 | // Currently always true as the hint is not yet implemented. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6371 | Operands.push_back( |
| 6372 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6373 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 6374 | } |
| 6375 | } |
| 6376 | } |
| 6377 | } |
| 6378 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6379 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6380 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6381 | //===----------------------------------------------------------------------===// |
| 6382 | // Hexagon ABI Implementation |
| 6383 | //===----------------------------------------------------------------------===// |
| 6384 | |
| 6385 | namespace { |
| 6386 | |
| 6387 | class HexagonABIInfo : public ABIInfo { |
| 6388 | |
| 6389 | |
| 6390 | public: |
| 6391 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6392 | |
| 6393 | private: |
| 6394 | |
| 6395 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6396 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 6397 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6398 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6399 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6400 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6401 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6402 | }; |
| 6403 | |
| 6404 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6405 | public: |
| 6406 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6407 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 6408 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6409 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6410 | return 29; |
| 6411 | } |
| 6412 | }; |
| 6413 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6414 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6415 | |
| 6416 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6417 | if (!getCXXABI().classifyReturnType(FI)) |
| 6418 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6419 | for (auto &I : FI.arguments()) |
| 6420 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6421 | } |
| 6422 | |
| 6423 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 6424 | if (!isAggregateTypeForABI(Ty)) { |
| 6425 | // Treat an enum type as its underlying type. |
| 6426 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6427 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6428 | |
| 6429 | return (Ty->isPromotableIntegerType() ? |
| 6430 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6431 | } |
| 6432 | |
| 6433 | // Ignore empty records. |
| 6434 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6435 | return ABIArgInfo::getIgnore(); |
| 6436 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6437 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6438 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6439 | |
| 6440 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6441 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6442 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6443 | // Pass in the smallest viable integer type. |
| 6444 | else if (Size > 32) |
| 6445 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6446 | else if (Size > 16) |
| 6447 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6448 | else if (Size > 8) |
| 6449 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6450 | else |
| 6451 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6452 | } |
| 6453 | |
| 6454 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6455 | if (RetTy->isVoidType()) |
| 6456 | return ABIArgInfo::getIgnore(); |
| 6457 | |
| 6458 | // Large vector types should be returned via memory. |
| 6459 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6460 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6461 | |
| 6462 | if (!isAggregateTypeForABI(RetTy)) { |
| 6463 | // Treat an enum type as its underlying type. |
| 6464 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6465 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6466 | |
| 6467 | return (RetTy->isPromotableIntegerType() ? |
| 6468 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6469 | } |
| 6470 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6471 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6472 | return ABIArgInfo::getIgnore(); |
| 6473 | |
| 6474 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6475 | // are returned indirectly. |
| 6476 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6477 | if (Size <= 64) { |
| 6478 | // Return in the smallest viable integer type. |
| 6479 | if (Size <= 8) |
| 6480 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6481 | if (Size <= 16) |
| 6482 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6483 | if (Size <= 32) |
| 6484 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6485 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6486 | } |
| 6487 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6488 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6489 | } |
| 6490 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6491 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6492 | QualType Ty) const { |
| 6493 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 6494 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6495 | getContext().getTypeInfoInChars(Ty), |
| 6496 | CharUnits::fromQuantity(4), |
| 6497 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6498 | } |
| 6499 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6500 | //===----------------------------------------------------------------------===// |
| 6501 | // AMDGPU ABI Implementation |
| 6502 | //===----------------------------------------------------------------------===// |
| 6503 | |
| 6504 | namespace { |
| 6505 | |
| 6506 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6507 | public: |
| 6508 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6509 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6510 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6511 | CodeGen::CodeGenModule &M) const override; |
| 6512 | }; |
| 6513 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6514 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6515 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6516 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6517 | const Decl *D, |
| 6518 | llvm::GlobalValue *GV, |
| 6519 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6520 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6521 | if (!FD) |
| 6522 | return; |
| 6523 | |
| 6524 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6525 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6526 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6527 | if (NumVGPR != 0) |
| 6528 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6529 | } |
| 6530 | |
| 6531 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6532 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6533 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6534 | if (NumSGPR != 0) |
| 6535 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6536 | } |
| 6537 | } |
| 6538 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6539 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6540 | //===----------------------------------------------------------------------===// |
| 6541 | // SPARC v9 ABI Implementation. |
| 6542 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6543 | // |
| 6544 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6545 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6546 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6547 | // |
| 6548 | // One case requires special care: |
| 6549 | // |
| 6550 | // struct mixed { |
| 6551 | // int i; |
| 6552 | // float f; |
| 6553 | // }; |
| 6554 | // |
| 6555 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6556 | // parameter array, but the int is passed in an integer register, and the float |
| 6557 | // is passed in a floating point register. This is represented as two arguments |
| 6558 | // with the LLVM IR inreg attribute: |
| 6559 | // |
| 6560 | // declare void f(i32 inreg %i, float inreg %f) |
| 6561 | // |
| 6562 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6563 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6564 | // bytes. |
| 6565 | // |
| 6566 | namespace { |
| 6567 | class SparcV9ABIInfo : public ABIInfo { |
| 6568 | public: |
| 6569 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6570 | |
| 6571 | private: |
| 6572 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6573 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6574 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6575 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6576 | |
| 6577 | // Coercion type builder for structs passed in registers. The coercion type |
| 6578 | // serves two purposes: |
| 6579 | // |
| 6580 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6581 | // in registers. |
| 6582 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6583 | // code generator knows to pass them in floating point registers. |
| 6584 | // |
| 6585 | // We also compute the InReg flag which indicates that the struct contains |
| 6586 | // aligned 32-bit floats. |
| 6587 | // |
| 6588 | struct CoerceBuilder { |
| 6589 | llvm::LLVMContext &Context; |
| 6590 | const llvm::DataLayout &DL; |
| 6591 | SmallVector<llvm::Type*, 8> Elems; |
| 6592 | uint64_t Size; |
| 6593 | bool InReg; |
| 6594 | |
| 6595 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6596 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6597 | |
| 6598 | // Pad Elems with integers until Size is ToSize. |
| 6599 | void pad(uint64_t ToSize) { |
| 6600 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6601 | if (ToSize == Size) |
| 6602 | return; |
| 6603 | |
| 6604 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6605 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6606 | if (Aligned > Size && Aligned <= ToSize) { |
| 6607 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6608 | Size = Aligned; |
| 6609 | } |
| 6610 | |
| 6611 | // Add whole 64-bit words. |
| 6612 | while (Size + 64 <= ToSize) { |
| 6613 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6614 | Size += 64; |
| 6615 | } |
| 6616 | |
| 6617 | // Final in-word padding. |
| 6618 | if (Size < ToSize) { |
| 6619 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6620 | Size = ToSize; |
| 6621 | } |
| 6622 | } |
| 6623 | |
| 6624 | // Add a floating point element at Offset. |
| 6625 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6626 | // Unaligned floats are treated as integers. |
| 6627 | if (Offset % Bits) |
| 6628 | return; |
| 6629 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6630 | if (Bits < 64) |
| 6631 | InReg = true; |
| 6632 | pad(Offset); |
| 6633 | Elems.push_back(Ty); |
| 6634 | Size = Offset + Bits; |
| 6635 | } |
| 6636 | |
| 6637 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6638 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6639 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6640 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6641 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6642 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6643 | switch (ElemTy->getTypeID()) { |
| 6644 | case llvm::Type::StructTyID: |
| 6645 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6646 | break; |
| 6647 | case llvm::Type::FloatTyID: |
| 6648 | addFloat(ElemOffset, ElemTy, 32); |
| 6649 | break; |
| 6650 | case llvm::Type::DoubleTyID: |
| 6651 | addFloat(ElemOffset, ElemTy, 64); |
| 6652 | break; |
| 6653 | case llvm::Type::FP128TyID: |
| 6654 | addFloat(ElemOffset, ElemTy, 128); |
| 6655 | break; |
| 6656 | case llvm::Type::PointerTyID: |
| 6657 | if (ElemOffset % 64 == 0) { |
| 6658 | pad(ElemOffset); |
| 6659 | Elems.push_back(ElemTy); |
| 6660 | Size += 64; |
| 6661 | } |
| 6662 | break; |
| 6663 | default: |
| 6664 | break; |
| 6665 | } |
| 6666 | } |
| 6667 | } |
| 6668 | |
| 6669 | // Check if Ty is a usable substitute for the coercion type. |
| 6670 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 6671 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6672 | } |
| 6673 | |
| 6674 | // Get the coercion type as a literal struct type. |
| 6675 | llvm::Type *getType() const { |
| 6676 | if (Elems.size() == 1) |
| 6677 | return Elems.front(); |
| 6678 | else |
| 6679 | return llvm::StructType::get(Context, Elems); |
| 6680 | } |
| 6681 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6682 | }; |
| 6683 | } // end anonymous namespace |
| 6684 | |
| 6685 | ABIArgInfo |
| 6686 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6687 | if (Ty->isVoidType()) |
| 6688 | return ABIArgInfo::getIgnore(); |
| 6689 | |
| 6690 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6691 | |
| 6692 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6693 | // pointer / sret pointer. |
| 6694 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6695 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6696 | |
| 6697 | // Treat an enum type as its underlying type. |
| 6698 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6699 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6700 | |
| 6701 | // Integer types smaller than a register are extended. |
| 6702 | if (Size < 64 && Ty->isIntegerType()) |
| 6703 | return ABIArgInfo::getExtend(); |
| 6704 | |
| 6705 | // Other non-aggregates go in registers. |
| 6706 | if (!isAggregateTypeForABI(Ty)) |
| 6707 | return ABIArgInfo::getDirect(); |
| 6708 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6709 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6710 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 6711 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6712 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6713 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6714 | // This is a small aggregate type that should be passed in registers. |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6715 | // Build a coercion type from the LLVM struct type. |
| 6716 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6717 | if (!StrTy) |
| 6718 | return ABIArgInfo::getDirect(); |
| 6719 | |
| 6720 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6721 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6722 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6723 | |
| 6724 | // Try to use the original type for coercion. |
| 6725 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6726 | |
| 6727 | if (CB.InReg) |
| 6728 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6729 | else |
| 6730 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6733 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6734 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6735 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6736 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6737 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6738 | AI.setCoerceToType(ArgTy); |
| 6739 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6740 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6741 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6742 | CGBuilderTy &Builder = CGF.Builder; |
| 6743 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 6744 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6745 | |
| 6746 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 6747 | |
| 6748 | Address ArgAddr = Address::invalid(); |
| 6749 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6750 | switch (AI.getKind()) { |
| 6751 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6752 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6753 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6754 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6755 | case ABIArgInfo::Extend: { |
| 6756 | Stride = SlotSize; |
| 6757 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 6758 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6759 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6760 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6761 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6762 | case ABIArgInfo::Direct: { |
| 6763 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6764 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6765 | ArgAddr = Addr; |
| 6766 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6767 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6768 | |
| 6769 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6770 | Stride = SlotSize; |
| 6771 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 6772 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 6773 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6774 | break; |
| 6775 | |
| 6776 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6777 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6778 | } |
| 6779 | |
| 6780 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6781 | llvm::Value *NextPtr = |
| 6782 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 6783 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6784 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6785 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6786 | } |
| 6787 | |
| 6788 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6789 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6790 | for (auto &I : FI.arguments()) |
| 6791 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6792 | } |
| 6793 | |
| 6794 | namespace { |
| 6795 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6796 | public: |
| 6797 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6798 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6799 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6800 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6801 | return 14; |
| 6802 | } |
| 6803 | |
| 6804 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6805 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6806 | }; |
| 6807 | } // end anonymous namespace |
| 6808 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6809 | bool |
| 6810 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6811 | llvm::Value *Address) const { |
| 6812 | // This is calculated from the LLVM and GCC tables and verified |
| 6813 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6814 | |
| 6815 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6816 | |
| 6817 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6818 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6819 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6820 | |
| 6821 | // 0-31: the 8-byte general-purpose registers |
| 6822 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6823 | |
| 6824 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6825 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6826 | |
| 6827 | // Y = 64 |
| 6828 | // PSR = 65 |
| 6829 | // WIM = 66 |
| 6830 | // TBR = 67 |
| 6831 | // PC = 68 |
| 6832 | // NPC = 69 |
| 6833 | // FSR = 70 |
| 6834 | // CSR = 71 |
| 6835 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6836 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6837 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6838 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6839 | |
| 6840 | return false; |
| 6841 | } |
| 6842 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6843 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6844 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6845 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6846 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6847 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6848 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6849 | |
| 6850 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6851 | /// it by reference between functions that append to it. |
| 6852 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6853 | |
| 6854 | /// TypeStringCache caches the meta encodings of Types. |
| 6855 | /// |
| 6856 | /// The reason for caching TypeStrings is two fold: |
| 6857 | /// 1. To cache a type's encoding for later uses; |
| 6858 | /// 2. As a means to break recursive member type inclusion. |
| 6859 | /// |
| 6860 | /// A cache Entry can have a Status of: |
| 6861 | /// NonRecursive: The type encoding is not recursive; |
| 6862 | /// Recursive: The type encoding is recursive; |
| 6863 | /// Incomplete: An incomplete TypeString; |
| 6864 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6865 | /// Recursive type encoding. |
| 6866 | /// |
| 6867 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6868 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6869 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6870 | /// the type is encountered. |
| 6871 | /// |
| 6872 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6873 | /// possible. The type itself is recursive and it may contain other types which |
| 6874 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6875 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6876 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6877 | /// |
| 6878 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6879 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6880 | /// are placed into the cache during type expansion as a means to identify and |
| 6881 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6882 | /// the entry becomes IncompleteUsed. |
| 6883 | /// |
| 6884 | /// During the expansion of a RecordType's members: |
| 6885 | /// |
| 6886 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6887 | /// cached encoding is used; |
| 6888 | /// |
| 6889 | /// If the cache contains a Recursive encoding for the member type, the |
| 6890 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6891 | /// |
| 6892 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6893 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6894 | /// |
| 6895 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6896 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6897 | /// it is swapped back in; |
| 6898 | /// |
| 6899 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6900 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6901 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6902 | /// |
| 6903 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6904 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6905 | /// Else the member is part of a recursive type and thus the recursion has |
| 6906 | /// been exited too soon for the encoding to be correct for the member. |
| 6907 | /// |
| 6908 | class TypeStringCache { |
| 6909 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6910 | struct Entry { |
| 6911 | std::string Str; // The encoded TypeString for the type. |
| 6912 | enum Status State; // Information about the encoding in 'Str'. |
| 6913 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6914 | // during the expansion of RecordType's members. |
| 6915 | }; |
| 6916 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6917 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6918 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6919 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 6920 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6921 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6922 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6923 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6924 | bool IsRecursive); |
| 6925 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6926 | }; |
| 6927 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6928 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6929 | /// FieldEncoding is a helper for this ordering process. |
| 6930 | class FieldEncoding { |
| 6931 | bool HasName; |
| 6932 | std::string Enc; |
| 6933 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 6934 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
| 6935 | StringRef str() {return Enc.c_str();} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6936 | bool operator<(const FieldEncoding &rhs) const { |
| 6937 | if (HasName != rhs.HasName) return HasName; |
| 6938 | return Enc < rhs.Enc; |
| 6939 | } |
| 6940 | }; |
| 6941 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6942 | class XCoreABIInfo : public DefaultABIInfo { |
| 6943 | public: |
| 6944 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6945 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6946 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6947 | }; |
| 6948 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6949 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6950 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6951 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6952 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6953 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6954 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6955 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6956 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6957 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6958 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6959 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6960 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6961 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6962 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6963 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6964 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6965 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 6966 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6967 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6968 | // Handle the argument. |
| 6969 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6970 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6971 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6972 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6973 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6974 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6975 | |
| 6976 | Address Val = Address::invalid(); |
| 6977 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6978 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6979 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6980 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6981 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6982 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6983 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 6984 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6985 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6986 | case ABIArgInfo::Extend: |
| 6987 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6988 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 6989 | ArgSize = CharUnits::fromQuantity( |
| 6990 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6991 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6992 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6993 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6994 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 6995 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 6996 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6997 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6998 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6999 | |
| 7000 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7001 | if (!ArgSize.isZero()) { |
| 7002 | llvm::Value *APN = |
| 7003 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 7004 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7005 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7006 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7007 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7008 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7009 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7010 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 7011 | /// into the cache as a means to identify and break recursion. |
| 7012 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 7013 | /// be reinserted by removeIncomplete(). |
| 7014 | /// All other types of encoding should have been used rather than arriving here. |
| 7015 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 7016 | std::string StubEnc) { |
| 7017 | if (!ID) |
| 7018 | return; |
| 7019 | Entry &E = Map[ID]; |
| 7020 | assert( (E.Str.empty() || E.State == Recursive) && |
| 7021 | "Incorrectly use of addIncomplete"); |
| 7022 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 7023 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 7024 | E.Str.swap(StubEnc); |
| 7025 | E.State = Incomplete; |
| 7026 | ++IncompleteCount; |
| 7027 | } |
| 7028 | |
| 7029 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 7030 | /// must be removed from the cache. |
| 7031 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 7032 | /// Returns true if the RecordType was defined recursively. |
| 7033 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 7034 | if (!ID) |
| 7035 | return false; |
| 7036 | auto I = Map.find(ID); |
| 7037 | assert(I != Map.end() && "Entry not present"); |
| 7038 | Entry &E = I->second; |
| 7039 | assert( (E.State == Incomplete || |
| 7040 | E.State == IncompleteUsed) && |
| 7041 | "Entry must be an incomplete type"); |
| 7042 | bool IsRecursive = false; |
| 7043 | if (E.State == IncompleteUsed) { |
| 7044 | // We made use of our Incomplete encoding, thus we are recursive. |
| 7045 | IsRecursive = true; |
| 7046 | --IncompleteUsedCount; |
| 7047 | } |
| 7048 | if (E.Swapped.empty()) |
| 7049 | Map.erase(I); |
| 7050 | else { |
| 7051 | // Swap the Recursive back. |
| 7052 | E.Swapped.swap(E.Str); |
| 7053 | E.Swapped.clear(); |
| 7054 | E.State = Recursive; |
| 7055 | } |
| 7056 | --IncompleteCount; |
| 7057 | return IsRecursive; |
| 7058 | } |
| 7059 | |
| 7060 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 7061 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 7062 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7063 | bool IsRecursive) { |
| 7064 | if (!ID || IncompleteUsedCount) |
| 7065 | return; // No key or it is is an incomplete sub-type so don't add. |
| 7066 | Entry &E = Map[ID]; |
| 7067 | if (IsRecursive && !E.Str.empty()) { |
| 7068 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 7069 | "This is not the same Recursive entry"); |
| 7070 | // The parent container was not recursive after all, so we could have used |
| 7071 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 7072 | // we started viz: IncompleteCount!=0. |
| 7073 | return; |
| 7074 | } |
| 7075 | assert(E.Str.empty() && "Entry already present"); |
| 7076 | E.Str = Str.str(); |
| 7077 | E.State = IsRecursive? Recursive : NonRecursive; |
| 7078 | } |
| 7079 | |
| 7080 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 7081 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 7082 | /// encoding is Recursive, return an empty StringRef. |
| 7083 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 7084 | if (!ID) |
| 7085 | return StringRef(); // We have no key. |
| 7086 | auto I = Map.find(ID); |
| 7087 | if (I == Map.end()) |
| 7088 | return StringRef(); // We have no encoding. |
| 7089 | Entry &E = I->second; |
| 7090 | if (E.State == Recursive && IncompleteCount) |
| 7091 | return StringRef(); // We don't use Recursive encodings for member types. |
| 7092 | |
| 7093 | if (E.State == Incomplete) { |
| 7094 | // The incomplete type is being used to break out of recursion. |
| 7095 | E.State = IncompleteUsed; |
| 7096 | ++IncompleteUsedCount; |
| 7097 | } |
| 7098 | return E.Str.c_str(); |
| 7099 | } |
| 7100 | |
| 7101 | /// The XCore ABI includes a type information section that communicates symbol |
| 7102 | /// type information to the linker. The linker uses this information to verify |
| 7103 | /// safety/correctness of things such as array bound and pointers et al. |
| 7104 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 7105 | /// This type information (TypeString) is emitted into meta data for all global |
| 7106 | /// symbols: definitions, declarations, functions & variables. |
| 7107 | /// |
| 7108 | /// The TypeString carries type, qualifier, name, size & value details. |
| 7109 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7110 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7111 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 7112 | /// |
| 7113 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7114 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 7115 | |
| 7116 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 7117 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7118 | CodeGen::CodeGenModule &CGM) const { |
| 7119 | SmallStringEnc Enc; |
| 7120 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 7121 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7122 | llvm::SmallVector<llvm::Metadata *, 2> MDVals; |
| 7123 | MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7124 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 7125 | llvm::NamedMDNode *MD = |
| 7126 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 7127 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 7128 | } |
| 7129 | } |
| 7130 | |
| 7131 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7132 | const CodeGen::CodeGenModule &CGM, |
| 7133 | TypeStringCache &TSC); |
| 7134 | |
| 7135 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7136 | /// Builds a SmallVector containing the encoded field types in declaration |
| 7137 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7138 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 7139 | const RecordDecl *RD, |
| 7140 | const CodeGen::CodeGenModule &CGM, |
| 7141 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7142 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7143 | SmallStringEnc Enc; |
| 7144 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7145 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7146 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7147 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7148 | Enc += "b("; |
| 7149 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7150 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7151 | Enc += ':'; |
| 7152 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7153 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7154 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7155 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7156 | Enc += ')'; |
| 7157 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 7158 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7159 | } |
| 7160 | return true; |
| 7161 | } |
| 7162 | |
| 7163 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 7164 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 7165 | /// Union types have their fields ordered according to the ABI. |
| 7166 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 7167 | const CodeGen::CodeGenModule &CGM, |
| 7168 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 7169 | // Append the cached TypeString if we have one. |
| 7170 | StringRef TypeString = TSC.lookupStr(ID); |
| 7171 | if (!TypeString.empty()) { |
| 7172 | Enc += TypeString; |
| 7173 | return true; |
| 7174 | } |
| 7175 | |
| 7176 | // Start to emit an incomplete TypeString. |
| 7177 | size_t Start = Enc.size(); |
| 7178 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 7179 | Enc += '('; |
| 7180 | if (ID) |
| 7181 | Enc += ID->getName(); |
| 7182 | Enc += "){"; |
| 7183 | |
| 7184 | // We collect all encoded fields and order as necessary. |
| 7185 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7186 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 7187 | if (RD && !RD->field_empty()) { |
| 7188 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 7189 | // so that recursive calls to this RecordType will use it whilst building a |
| 7190 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7191 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7192 | std::string StubEnc(Enc.substr(Start).str()); |
| 7193 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 7194 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 7195 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 7196 | (void) TSC.removeIncomplete(ID); |
| 7197 | return false; |
| 7198 | } |
| 7199 | IsRecursive = TSC.removeIncomplete(ID); |
| 7200 | // The ABI requires unions to be sorted but not structures. |
| 7201 | // See FieldEncoding::operator< for sort algorithm. |
| 7202 | if (RT->isUnionType()) |
| 7203 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7204 | // We can now complete the TypeString. |
| 7205 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7206 | for (unsigned I = 0; I != E; ++I) { |
| 7207 | if (I) |
| 7208 | Enc += ','; |
| 7209 | Enc += FE[I].str(); |
| 7210 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7211 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7212 | Enc += '}'; |
| 7213 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 7214 | return true; |
| 7215 | } |
| 7216 | |
| 7217 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 7218 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 7219 | TypeStringCache &TSC, |
| 7220 | const IdentifierInfo *ID) { |
| 7221 | // Append the cached TypeString if we have one. |
| 7222 | StringRef TypeString = TSC.lookupStr(ID); |
| 7223 | if (!TypeString.empty()) { |
| 7224 | Enc += TypeString; |
| 7225 | return true; |
| 7226 | } |
| 7227 | |
| 7228 | size_t Start = Enc.size(); |
| 7229 | Enc += "e("; |
| 7230 | if (ID) |
| 7231 | Enc += ID->getName(); |
| 7232 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7233 | |
| 7234 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7235 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7236 | SmallVector<FieldEncoding, 16> FE; |
| 7237 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 7238 | ++I) { |
| 7239 | SmallStringEnc EnumEnc; |
| 7240 | EnumEnc += "m("; |
| 7241 | EnumEnc += I->getName(); |
| 7242 | EnumEnc += "){"; |
| 7243 | I->getInitVal().toString(EnumEnc); |
| 7244 | EnumEnc += '}'; |
| 7245 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 7246 | } |
| 7247 | std::sort(FE.begin(), FE.end()); |
| 7248 | unsigned E = FE.size(); |
| 7249 | for (unsigned I = 0; I != E; ++I) { |
| 7250 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7251 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7252 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7253 | } |
| 7254 | } |
| 7255 | Enc += '}'; |
| 7256 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 7257 | return true; |
| 7258 | } |
| 7259 | |
| 7260 | /// Appends type's qualifier to Enc. |
| 7261 | /// This is done prior to appending the type's encoding. |
| 7262 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 7263 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 7264 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7265 | int Lookup = 0; |
| 7266 | if (QT.isConstQualified()) |
| 7267 | Lookup += 1<<0; |
| 7268 | if (QT.isRestrictQualified()) |
| 7269 | Lookup += 1<<1; |
| 7270 | if (QT.isVolatileQualified()) |
| 7271 | Lookup += 1<<2; |
| 7272 | Enc += Table[Lookup]; |
| 7273 | } |
| 7274 | |
| 7275 | /// Appends built-in types to Enc. |
| 7276 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 7277 | const char *EncType; |
| 7278 | switch (BT->getKind()) { |
| 7279 | case BuiltinType::Void: |
| 7280 | EncType = "0"; |
| 7281 | break; |
| 7282 | case BuiltinType::Bool: |
| 7283 | EncType = "b"; |
| 7284 | break; |
| 7285 | case BuiltinType::Char_U: |
| 7286 | EncType = "uc"; |
| 7287 | break; |
| 7288 | case BuiltinType::UChar: |
| 7289 | EncType = "uc"; |
| 7290 | break; |
| 7291 | case BuiltinType::SChar: |
| 7292 | EncType = "sc"; |
| 7293 | break; |
| 7294 | case BuiltinType::UShort: |
| 7295 | EncType = "us"; |
| 7296 | break; |
| 7297 | case BuiltinType::Short: |
| 7298 | EncType = "ss"; |
| 7299 | break; |
| 7300 | case BuiltinType::UInt: |
| 7301 | EncType = "ui"; |
| 7302 | break; |
| 7303 | case BuiltinType::Int: |
| 7304 | EncType = "si"; |
| 7305 | break; |
| 7306 | case BuiltinType::ULong: |
| 7307 | EncType = "ul"; |
| 7308 | break; |
| 7309 | case BuiltinType::Long: |
| 7310 | EncType = "sl"; |
| 7311 | break; |
| 7312 | case BuiltinType::ULongLong: |
| 7313 | EncType = "ull"; |
| 7314 | break; |
| 7315 | case BuiltinType::LongLong: |
| 7316 | EncType = "sll"; |
| 7317 | break; |
| 7318 | case BuiltinType::Float: |
| 7319 | EncType = "ft"; |
| 7320 | break; |
| 7321 | case BuiltinType::Double: |
| 7322 | EncType = "d"; |
| 7323 | break; |
| 7324 | case BuiltinType::LongDouble: |
| 7325 | EncType = "ld"; |
| 7326 | break; |
| 7327 | default: |
| 7328 | return false; |
| 7329 | } |
| 7330 | Enc += EncType; |
| 7331 | return true; |
| 7332 | } |
| 7333 | |
| 7334 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 7335 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 7336 | const CodeGen::CodeGenModule &CGM, |
| 7337 | TypeStringCache &TSC) { |
| 7338 | Enc += "p("; |
| 7339 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 7340 | return false; |
| 7341 | Enc += ')'; |
| 7342 | return true; |
| 7343 | } |
| 7344 | |
| 7345 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7346 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 7347 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7348 | const CodeGen::CodeGenModule &CGM, |
| 7349 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 7350 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 7351 | return false; |
| 7352 | Enc += "a("; |
| 7353 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 7354 | CAT->getSize().toStringUnsigned(Enc); |
| 7355 | else |
| 7356 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 7357 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7358 | // The Qualifiers should be attached to the type rather than the array. |
| 7359 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7360 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 7361 | return false; |
| 7362 | Enc += ')'; |
| 7363 | return true; |
| 7364 | } |
| 7365 | |
| 7366 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 7367 | /// and the arguments. |
| 7368 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 7369 | const CodeGen::CodeGenModule &CGM, |
| 7370 | TypeStringCache &TSC) { |
| 7371 | Enc += "f{"; |
| 7372 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 7373 | return false; |
| 7374 | Enc += "}("; |
| 7375 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 7376 | // N.B. we are only interested in the adjusted param types. |
| 7377 | auto I = FPT->param_type_begin(); |
| 7378 | auto E = FPT->param_type_end(); |
| 7379 | if (I != E) { |
| 7380 | do { |
| 7381 | if (!appendType(Enc, *I, CGM, TSC)) |
| 7382 | return false; |
| 7383 | ++I; |
| 7384 | if (I != E) |
| 7385 | Enc += ','; |
| 7386 | } while (I != E); |
| 7387 | if (FPT->isVariadic()) |
| 7388 | Enc += ",va"; |
| 7389 | } else { |
| 7390 | if (FPT->isVariadic()) |
| 7391 | Enc += "va"; |
| 7392 | else |
| 7393 | Enc += '0'; |
| 7394 | } |
| 7395 | } |
| 7396 | Enc += ')'; |
| 7397 | return true; |
| 7398 | } |
| 7399 | |
| 7400 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 7401 | /// type encodings. |
| 7402 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7403 | const CodeGen::CodeGenModule &CGM, |
| 7404 | TypeStringCache &TSC) { |
| 7405 | |
| 7406 | QualType QT = QType.getCanonicalType(); |
| 7407 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7408 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 7409 | // The Qualifiers should be attached to the type rather than the array. |
| 7410 | // Thus we don't call appendQualifier() here. |
| 7411 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 7412 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7413 | appendQualifier(Enc, QT); |
| 7414 | |
| 7415 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 7416 | return appendBuiltinType(Enc, BT); |
| 7417 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7418 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 7419 | return appendPointerType(Enc, PT, CGM, TSC); |
| 7420 | |
| 7421 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 7422 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 7423 | |
| 7424 | if (const RecordType *RT = QT->getAsStructureType()) |
| 7425 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7426 | |
| 7427 | if (const RecordType *RT = QT->getAsUnionType()) |
| 7428 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7429 | |
| 7430 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7431 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7432 | |
| 7433 | return false; |
| 7434 | } |
| 7435 | |
| 7436 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7437 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 7438 | if (!D) |
| 7439 | return false; |
| 7440 | |
| 7441 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7442 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 7443 | return false; |
| 7444 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 7445 | } |
| 7446 | |
| 7447 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7448 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 7449 | return false; |
| 7450 | QualType QT = VD->getType().getCanonicalType(); |
| 7451 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 7452 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7453 | // The Qualifiers should be attached to the type rather than the array. |
| 7454 | // Thus we don't call appendQualifier() here. |
| 7455 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7456 | } |
| 7457 | return appendType(Enc, QT, CGM, TSC); |
| 7458 | } |
| 7459 | return false; |
| 7460 | } |
| 7461 | |
| 7462 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7463 | //===----------------------------------------------------------------------===// |
| 7464 | // Driver code |
| 7465 | //===----------------------------------------------------------------------===// |
| 7466 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 7467 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 7468 | return getTarget().getTriple(); |
| 7469 | } |
| 7470 | |
| 7471 | bool CodeGenModule::supportsCOMDAT() const { |
| 7472 | return !getTriple().isOSBinFormatMachO(); |
| 7473 | } |
| 7474 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7475 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7476 | if (TheTargetCodeGenInfo) |
| 7477 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7478 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7479 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 7480 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7481 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7482 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7483 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 7484 | case llvm::Triple::le32: |
| 7485 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7486 | case llvm::Triple::mips: |
| 7487 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 7488 | if (Triple.getOS() == llvm::Triple::NaCl) |
| 7489 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7490 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 7491 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7492 | case llvm::Triple::mips64: |
| 7493 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7494 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 7495 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 7496 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 7497 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7498 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7499 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7500 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7501 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7502 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7503 | } |
| 7504 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 7505 | case llvm::Triple::wasm32: |
| 7506 | case llvm::Triple::wasm64: |
| 7507 | return *(TheTargetCodeGenInfo = new WebAssemblyTargetCodeGenInfo(Types)); |
| 7508 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7509 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7510 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7511 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7512 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7513 | { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 7514 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7515 | TheTargetCodeGenInfo = |
| 7516 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP); |
| 7517 | return *TheTargetCodeGenInfo; |
| 7518 | } |
| 7519 | |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7520 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 7521 | StringRef ABIStr = getTarget().getABI(); |
| 7522 | if (ABIStr == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7523 | Kind = ARMABIInfo::APCS; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 7524 | else if (ABIStr == "aapcs16") |
| 7525 | Kind = ARMABIInfo::AAPCS16_VFP; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 7526 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7527 | (CodeGenOpts.FloatABI != "soft" && |
| 7528 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7529 | Kind = ARMABIInfo::AAPCS_VFP; |
| 7530 | |
Derek Schuff | 71658bd | 2015-01-29 00:47:04 +0000 | [diff] [blame] | 7531 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7532 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7533 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7534 | case llvm::Triple::ppc: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 7535 | return *(TheTargetCodeGenInfo = |
| 7536 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 7537 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7538 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7539 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7540 | if (getTarget().getABI() == "elfv2") |
| 7541 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7542 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7543 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7544 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7545 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7546 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 7547 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7548 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 7549 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7550 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7551 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7552 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7553 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7554 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7555 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7556 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7557 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7558 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 7559 | case llvm::Triple::nvptx: |
| 7560 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 7561 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 7562 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7563 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7564 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7565 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 7566 | case llvm::Triple::systemz: { |
| 7567 | bool HasVector = getTarget().getABI() == "vector"; |
| 7568 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types, |
| 7569 | HasVector)); |
| 7570 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7571 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7572 | case llvm::Triple::tce: |
| 7573 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 7574 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7575 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7576 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7577 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7578 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 7579 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 7580 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7581 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7582 | return *(TheTargetCodeGenInfo = new WinX86_32TargetCodeGenInfo( |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7583 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7584 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7585 | } else { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7586 | return *(TheTargetCodeGenInfo = new X86_32TargetCodeGenInfo( |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7587 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 7588 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 7589 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7590 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7591 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7592 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7593 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7594 | StringRef ABI = getTarget().getABI(); |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 7595 | X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512 : |
| 7596 | ABI == "avx" ? X86AVXABILevel::AVX : |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7597 | X86AVXABILevel::None); |
| 7598 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7599 | switch (Triple.getOS()) { |
| 7600 | case llvm::Triple::Win32: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7601 | return *(TheTargetCodeGenInfo = |
| 7602 | new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 7603 | case llvm::Triple::PS4: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7604 | return *(TheTargetCodeGenInfo = |
| 7605 | new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7606 | default: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7607 | return *(TheTargetCodeGenInfo = |
| 7608 | new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7609 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7610 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7611 | case llvm::Triple::hexagon: |
| 7612 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7613 | case llvm::Triple::r600: |
| 7614 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 7615 | case llvm::Triple::amdgcn: |
| 7616 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7617 | case llvm::Triple::sparcv9: |
| 7618 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7619 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7620 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7621 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7622 | } |