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 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 165 | /// Emit va_arg for a platform using the common void* representation, |
| 166 | /// where arguments are simply emitted in an array of slots on the stack. |
| 167 | /// |
| 168 | /// This version implements the core direct-value passing rules. |
| 169 | /// |
| 170 | /// \param SlotSize - The size and alignment of a stack slot. |
| 171 | /// Each argument will be allocated to a multiple of this number of |
| 172 | /// slots, and all the slots will be aligned to this value. |
| 173 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 174 | /// an argument type with an alignment greater than the slot size |
| 175 | /// will be emitted on a higher-alignment address, potentially |
| 176 | /// leaving one or more empty slots behind as padding. If this |
| 177 | /// is false, the returned address might be less-aligned than |
| 178 | /// DirectAlign. |
| 179 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 180 | Address VAListAddr, |
| 181 | llvm::Type *DirectTy, |
| 182 | CharUnits DirectSize, |
| 183 | CharUnits DirectAlign, |
| 184 | CharUnits SlotSize, |
| 185 | bool AllowHigherAlign) { |
| 186 | // Cast the element type to i8* if necessary. Some platforms define |
| 187 | // va_list as a struct containing an i8* instead of just an i8*. |
| 188 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 189 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 190 | |
| 191 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 192 | |
| 193 | // If the CC aligns values higher than the slot size, do so if needed. |
| 194 | Address Addr = Address::invalid(); |
| 195 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
| 196 | llvm::Value *PtrAsInt = Ptr; |
| 197 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 198 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 199 | llvm::ConstantInt::get(CGF.IntPtrTy, DirectAlign.getQuantity() - 1)); |
| 200 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 201 | llvm::ConstantInt::get(CGF.IntPtrTy, -DirectAlign.getQuantity())); |
| 202 | Addr = Address(CGF.Builder.CreateIntToPtr(PtrAsInt, Ptr->getType(), |
| 203 | "argp.cur.aligned"), |
| 204 | DirectAlign); |
| 205 | } else { |
| 206 | Addr = Address(Ptr, SlotSize); |
| 207 | } |
| 208 | |
| 209 | // Advance the pointer past the argument, then store that back. |
| 210 | CharUnits FullDirectSize = DirectSize.RoundUpToAlignment(SlotSize); |
| 211 | llvm::Value *NextPtr = |
| 212 | CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, |
| 213 | "argp.next"); |
| 214 | CGF.Builder.CreateStore(NextPtr, VAListAddr); |
| 215 | |
| 216 | // If the argument is smaller than a slot, and this is a big-endian |
| 217 | // target, the argument will be right-adjusted in its slot. |
| 218 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian()) { |
| 219 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 220 | } |
| 221 | |
| 222 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 223 | return Addr; |
| 224 | } |
| 225 | |
| 226 | /// Emit va_arg for a platform using the common void* representation, |
| 227 | /// where arguments are simply emitted in an array of slots on the stack. |
| 228 | /// |
| 229 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 230 | /// \param ValueInfo - The size and alignment of this type, generally |
| 231 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 232 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 233 | /// Each argument will be allocated to a multiple of this number of |
| 234 | /// slots, and all the slots will be aligned to this value. |
| 235 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 236 | /// an argument type with an alignment greater than the slot size |
| 237 | /// will be emitted on a higher-alignment address, potentially |
| 238 | /// leaving one or more empty slots behind as padding. |
| 239 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 240 | QualType ValueTy, bool IsIndirect, |
| 241 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 242 | CharUnits SlotSizeAndAlign, |
| 243 | bool AllowHigherAlign) { |
| 244 | // The size and alignment of the value that was passed directly. |
| 245 | CharUnits DirectSize, DirectAlign; |
| 246 | if (IsIndirect) { |
| 247 | DirectSize = CGF.getPointerSize(); |
| 248 | DirectAlign = CGF.getPointerAlign(); |
| 249 | } else { |
| 250 | DirectSize = ValueInfo.first; |
| 251 | DirectAlign = ValueInfo.second; |
| 252 | } |
| 253 | |
| 254 | // Cast the address we've calculated to the right type. |
| 255 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 256 | if (IsIndirect) |
| 257 | DirectTy = DirectTy->getPointerTo(0); |
| 258 | |
| 259 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 260 | DirectSize, DirectAlign, |
| 261 | SlotSizeAndAlign, |
| 262 | AllowHigherAlign); |
| 263 | |
| 264 | if (IsIndirect) { |
| 265 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 266 | } |
| 267 | |
| 268 | return Addr; |
| 269 | |
| 270 | } |
| 271 | |
| 272 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 273 | Address Addr1, llvm::BasicBlock *Block1, |
| 274 | Address Addr2, llvm::BasicBlock *Block2, |
| 275 | const llvm::Twine &Name = "") { |
| 276 | assert(Addr1.getType() == Addr2.getType()); |
| 277 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 278 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 279 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 280 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 281 | return Address(PHI, Align); |
| 282 | } |
| 283 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 284 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 285 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 286 | // If someone can figure out a general rule for this, that would be great. |
| 287 | // It's probably just doomed to be platform-dependent, though. |
| 288 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 289 | // Verified for: |
| 290 | // x86-64 FreeBSD, Linux, Darwin |
| 291 | // x86-32 FreeBSD, Linux, Darwin |
| 292 | // PowerPC Linux, Darwin |
| 293 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 294 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 295 | return 32; |
| 296 | } |
| 297 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 298 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 299 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 300 | // The following conventions are known to require this to be false: |
| 301 | // x86_stdcall |
| 302 | // MIPS |
| 303 | // For everything else, we just prefer false unless we opt out. |
| 304 | return false; |
| 305 | } |
| 306 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 307 | void |
| 308 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 309 | llvm::SmallString<24> &Opt) const { |
| 310 | // This assumes the user is passing a library name like "rt" instead of a |
| 311 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 312 | // dynamic. |
| 313 | Opt = "-l"; |
| 314 | Opt += Lib; |
| 315 | } |
| 316 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 317 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 318 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 319 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 320 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 321 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 322 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 323 | if (FD->isUnnamedBitfield()) |
| 324 | return true; |
| 325 | |
| 326 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 327 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 328 | // Constant arrays of empty records count as empty, strip them off. |
| 329 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 330 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 331 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 332 | if (AT->getSize() == 0) |
| 333 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 334 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 335 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 336 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 337 | const RecordType *RT = FT->getAs<RecordType>(); |
| 338 | if (!RT) |
| 339 | return false; |
| 340 | |
| 341 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 342 | // |
| 343 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 344 | // current ABI. |
| 345 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 346 | return false; |
| 347 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 348 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 351 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 352 | /// fields. Note that a structure with a flexible array member is not |
| 353 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 354 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 355 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 356 | if (!RT) |
| 357 | return 0; |
| 358 | const RecordDecl *RD = RT->getDecl(); |
| 359 | if (RD->hasFlexibleArrayMember()) |
| 360 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 361 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 362 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 363 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 364 | for (const auto &I : CXXRD->bases()) |
| 365 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 366 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 367 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 368 | for (const auto *I : RD->fields()) |
| 369 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 370 | return false; |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | /// isSingleElementStruct - Determine if a structure is a "single |
| 375 | /// element struct", i.e. it has exactly one non-empty field or |
| 376 | /// exactly one field which is itself a single element |
| 377 | /// struct. Structures with flexible array members are never |
| 378 | /// considered single element structs. |
| 379 | /// |
| 380 | /// \return The field declaration for the single non-empty field, if |
| 381 | /// it exists. |
| 382 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 383 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 384 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 385 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 386 | |
| 387 | const RecordDecl *RD = RT->getDecl(); |
| 388 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 389 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 390 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 391 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 392 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 393 | // If this is a C++ record, check the bases first. |
| 394 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 395 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 396 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 397 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 398 | continue; |
| 399 | |
| 400 | // If we already found an element then this isn't a single-element struct. |
| 401 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 402 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 403 | |
| 404 | // If this is non-empty and not a single element struct, the composite |
| 405 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 406 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 407 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 408 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 413 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 414 | QualType FT = FD->getType(); |
| 415 | |
| 416 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 417 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 418 | continue; |
| 419 | |
| 420 | // If we already found an element then this isn't a single-element |
| 421 | // struct. |
| 422 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 423 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 424 | |
| 425 | // Treat single element arrays as the element. |
| 426 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 427 | if (AT->getSize().getZExtValue() != 1) |
| 428 | break; |
| 429 | FT = AT->getElementType(); |
| 430 | } |
| 431 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 432 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 433 | Found = FT.getTypePtr(); |
| 434 | } else { |
| 435 | Found = isSingleElementStruct(FT, Context); |
| 436 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 437 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 441 | // We don't consider a struct a single-element struct if it has |
| 442 | // padding beyond the element type. |
| 443 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 444 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 445 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 446 | return Found; |
| 447 | } |
| 448 | |
| 449 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 450 | // Treat complex types as the element type. |
| 451 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 452 | Ty = CTy->getElementType(); |
| 453 | |
| 454 | // Check for a type which we know has a simple scalar argument-passing |
| 455 | // convention without any padding. (We're specifically looking for 32 |
| 456 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 457 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 458 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 459 | return false; |
| 460 | |
| 461 | uint64_t Size = Context.getTypeSize(Ty); |
| 462 | return Size == 32 || Size == 64; |
| 463 | } |
| 464 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 465 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 466 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 467 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 468 | /// inhibiting optimizations. |
| 469 | /// |
| 470 | // FIXME: This predicate is missing many cases, currently it just follows |
| 471 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 472 | // should probably make this smarter, or better yet make the LLVM backend |
| 473 | // capable of handling it. |
| 474 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 475 | // We can only expand structure types. |
| 476 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 477 | if (!RT) |
| 478 | return false; |
| 479 | |
| 480 | // We can only expand (C) structures. |
| 481 | // |
| 482 | // FIXME: This needs to be generalized to handle classes as well. |
| 483 | const RecordDecl *RD = RT->getDecl(); |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 484 | if (!RD->isStruct()) |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 485 | return false; |
| 486 | |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 487 | // We try to expand CLike CXXRecordDecl. |
| 488 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 489 | if (!CXXRD->isCLike()) |
| 490 | return false; |
| 491 | } |
| 492 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 493 | uint64_t Size = 0; |
| 494 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 495 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 496 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 497 | return false; |
| 498 | |
| 499 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 500 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 501 | // counts as "basic" is more complicated than what we were doing previously. |
| 502 | if (FD->isBitField()) |
| 503 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 504 | |
| 505 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 508 | // Make sure there are not any holes in the struct. |
| 509 | if (Size != Context.getTypeSize(Ty)) |
| 510 | return false; |
| 511 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 512 | return true; |
| 513 | } |
| 514 | |
| 515 | namespace { |
| 516 | /// DefaultABIInfo - The default implementation for ABI specific |
| 517 | /// details. This implementation provides information which results in |
| 518 | /// self-consistent and sensible LLVM IR generation, but does not |
| 519 | /// conform to any particular ABI. |
| 520 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 521 | public: |
| 522 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 524 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 525 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 526 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 527 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 528 | if (!getCXXABI().classifyReturnType(FI)) |
| 529 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 530 | for (auto &I : FI.arguments()) |
| 531 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 532 | } |
| 533 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 534 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 535 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 536 | }; |
| 537 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 538 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 539 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 540 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 541 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 542 | }; |
| 543 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 544 | Address DefaultABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 545 | QualType Ty) const { |
| 546 | return Address::invalid(); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 549 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 550 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 551 | |
| 552 | if (isAggregateTypeForABI(Ty)) { |
| 553 | // Records with non-trivial destructors/copy-constructors should not be |
| 554 | // passed by value. |
| 555 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 556 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 557 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 558 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 559 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 560 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 561 | // Treat an enum type as its underlying type. |
| 562 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 563 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 564 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 565 | return (Ty->isPromotableIntegerType() ? |
| 566 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 569 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 570 | if (RetTy->isVoidType()) |
| 571 | return ABIArgInfo::getIgnore(); |
| 572 | |
| 573 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 574 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 575 | |
| 576 | // Treat an enum type as its underlying type. |
| 577 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 578 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 579 | |
| 580 | return (RetTy->isPromotableIntegerType() ? |
| 581 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 582 | } |
| 583 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 584 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 585 | // WebAssembly ABI Implementation |
| 586 | // |
| 587 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 588 | //===----------------------------------------------------------------------===// |
| 589 | |
| 590 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 591 | public: |
| 592 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 593 | : DefaultABIInfo(CGT) {} |
| 594 | |
| 595 | private: |
| 596 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 597 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 598 | |
| 599 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 600 | // non-virtual, but computeInfo is virtual, so we overload that. |
| 601 | void computeInfo(CGFunctionInfo &FI) const override { |
| 602 | if (!getCXXABI().classifyReturnType(FI)) |
| 603 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 604 | for (auto &Arg : FI.arguments()) |
| 605 | Arg.info = classifyArgumentType(Arg.type); |
| 606 | } |
| 607 | }; |
| 608 | |
| 609 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 610 | public: |
| 611 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 612 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 613 | }; |
| 614 | |
| 615 | /// \brief Classify argument of given type \p Ty. |
| 616 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 617 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 618 | |
| 619 | if (isAggregateTypeForABI(Ty)) { |
| 620 | // Records with non-trivial destructors/copy-constructors should not be |
| 621 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 622 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 623 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 624 | // Ignore empty structs/unions. |
| 625 | if (isEmptyRecord(getContext(), Ty, true)) |
| 626 | return ABIArgInfo::getIgnore(); |
| 627 | // Lower single-element structs to just pass a regular value. TODO: We |
| 628 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 629 | // though watch out for things like bitfields. |
| 630 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 631 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // Otherwise just do the default thing. |
| 635 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 636 | } |
| 637 | |
| 638 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 639 | if (isAggregateTypeForABI(RetTy)) { |
| 640 | // Records with non-trivial destructors/copy-constructors should not be |
| 641 | // returned by value. |
| 642 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 643 | // Ignore empty structs/unions. |
| 644 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 645 | return ABIArgInfo::getIgnore(); |
| 646 | // Lower single-element structs to just return a regular value. TODO: We |
| 647 | // could do reasonable-size multiple-element structs too, using |
| 648 | // ABIArgInfo::getDirect(). |
| 649 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 650 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | // Otherwise just do the default thing. |
| 655 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 656 | } |
| 657 | |
| 658 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 659 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 660 | // |
| 661 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 662 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 663 | //===----------------------------------------------------------------------===// |
| 664 | |
| 665 | class PNaClABIInfo : public ABIInfo { |
| 666 | public: |
| 667 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 668 | |
| 669 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 670 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 671 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 672 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 673 | Address EmitVAArg(CodeGenFunction &CGF, |
| 674 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 675 | }; |
| 676 | |
| 677 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 678 | public: |
| 679 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 680 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 681 | }; |
| 682 | |
| 683 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 684 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 685 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 686 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 687 | for (auto &I : FI.arguments()) |
| 688 | I.info = classifyArgumentType(I.type); |
| 689 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 690 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 691 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 692 | QualType Ty) const { |
| 693 | return Address::invalid(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 696 | /// \brief Classify argument of given type \p Ty. |
| 697 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 698 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 699 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 700 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 701 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 702 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 703 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 704 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 705 | } else if (Ty->isFloatingType()) { |
| 706 | // Floating-point types don't go inreg. |
| 707 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 708 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 709 | |
| 710 | return (Ty->isPromotableIntegerType() ? |
| 711 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 715 | if (RetTy->isVoidType()) |
| 716 | return ABIArgInfo::getIgnore(); |
| 717 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 718 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 719 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 720 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 721 | |
| 722 | // Treat an enum type as its underlying type. |
| 723 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 724 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 725 | |
| 726 | return (RetTy->isPromotableIntegerType() ? |
| 727 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 728 | } |
| 729 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 730 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 731 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 732 | // 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] | 733 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 734 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 735 | IRType->getScalarSizeInBits() != 64; |
| 736 | } |
| 737 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 738 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 739 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 740 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 741 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 742 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 743 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 744 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 747 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 751 | return Ty; |
| 752 | } |
| 753 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 754 | /// Returns true if this type can be passed in SSE registers with the |
| 755 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 756 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 757 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 758 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 759 | return true; |
| 760 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 761 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 762 | // registers specially. |
| 763 | unsigned VecSize = Context.getTypeSize(VT); |
| 764 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 765 | return true; |
| 766 | } |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 771 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 772 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 773 | return NumMembers <= 4; |
| 774 | } |
| 775 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 776 | //===----------------------------------------------------------------------===// |
| 777 | // X86-32 ABI Implementation |
| 778 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 779 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 780 | /// \brief Similar to llvm::CCState, but for Clang. |
| 781 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 782 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 783 | |
| 784 | unsigned CC; |
| 785 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 786 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 787 | }; |
| 788 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 789 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 790 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 791 | enum Class { |
| 792 | Integer, |
| 793 | Float |
| 794 | }; |
| 795 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 796 | static const unsigned MinABIStackAlignInBytes = 4; |
| 797 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 798 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 799 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 800 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 801 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 802 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 803 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 804 | |
| 805 | static bool isRegisterSize(unsigned Size) { |
| 806 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 807 | } |
| 808 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 809 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 810 | // FIXME: Assumes vectorcall is in use. |
| 811 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 812 | } |
| 813 | |
| 814 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 815 | uint64_t NumMembers) const override { |
| 816 | // FIXME: Assumes vectorcall is in use. |
| 817 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 818 | } |
| 819 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 820 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 821 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 822 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 823 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 824 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 825 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 826 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 827 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 828 | /// \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] | 829 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 830 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 831 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 832 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 833 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 834 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 835 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 836 | /// \brief Rewrite the function info so that all memory arguments use |
| 837 | /// inalloca. |
| 838 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 839 | |
| 840 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 841 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 842 | QualType Type) const; |
| 843 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 844 | public: |
| 845 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 846 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 847 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 848 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 849 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 850 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 851 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 852 | unsigned NumRegisterParameters, bool SoftFloatABI) |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 853 | : ABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
| 854 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 855 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 856 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 857 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 858 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 859 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 860 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 861 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 862 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 863 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 864 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 865 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 866 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 867 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 868 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 869 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 870 | static bool isStructReturnInRegABI( |
| 871 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 872 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 873 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 874 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 875 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 876 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 877 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 878 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 879 | return 4; |
| 880 | } |
| 881 | |
| 882 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 883 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 884 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 885 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 886 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 887 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 888 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 889 | } |
| 890 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 891 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 892 | std::string &Constraints, |
| 893 | std::vector<llvm::Type *> &ResultRegTypes, |
| 894 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 895 | std::vector<LValue> &ResultRegDests, |
| 896 | std::string &AsmString, |
| 897 | unsigned NumOutputs) const override; |
| 898 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 899 | llvm::Constant * |
| 900 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 901 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 902 | (0x06 << 8) | // .+0x08 |
| 903 | ('F' << 16) | |
| 904 | ('T' << 24); |
| 905 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 906 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 907 | }; |
| 908 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 909 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 910 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 911 | /// Rewrite input constraint references after adding some output constraints. |
| 912 | /// In the case where there is one output and one input and we add one output, |
| 913 | /// we need to replace all operand references greater than or equal to 1: |
| 914 | /// mov $0, $1 |
| 915 | /// mov eax, $1 |
| 916 | /// The result will be: |
| 917 | /// mov $0, $2 |
| 918 | /// mov eax, $2 |
| 919 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 920 | unsigned NumNewOuts, |
| 921 | std::string &AsmString) { |
| 922 | std::string Buf; |
| 923 | llvm::raw_string_ostream OS(Buf); |
| 924 | size_t Pos = 0; |
| 925 | while (Pos < AsmString.size()) { |
| 926 | size_t DollarStart = AsmString.find('$', Pos); |
| 927 | if (DollarStart == std::string::npos) |
| 928 | DollarStart = AsmString.size(); |
| 929 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 930 | if (DollarEnd == std::string::npos) |
| 931 | DollarEnd = AsmString.size(); |
| 932 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 933 | Pos = DollarEnd; |
| 934 | size_t NumDollars = DollarEnd - DollarStart; |
| 935 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 936 | // We have an operand reference. |
| 937 | size_t DigitStart = Pos; |
| 938 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 939 | if (DigitEnd == std::string::npos) |
| 940 | DigitEnd = AsmString.size(); |
| 941 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 942 | unsigned OperandIndex; |
| 943 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 944 | if (OperandIndex >= FirstIn) |
| 945 | OperandIndex += NumNewOuts; |
| 946 | OS << OperandIndex; |
| 947 | } else { |
| 948 | OS << OperandStr; |
| 949 | } |
| 950 | Pos = DigitEnd; |
| 951 | } |
| 952 | } |
| 953 | AsmString = std::move(OS.str()); |
| 954 | } |
| 955 | |
| 956 | /// Add output constraints for EAX:EDX because they are return registers. |
| 957 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 958 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 959 | std::vector<llvm::Type *> &ResultRegTypes, |
| 960 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 961 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 962 | unsigned NumOutputs) const { |
| 963 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 964 | |
| 965 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 966 | // larger. |
| 967 | if (!Constraints.empty()) |
| 968 | Constraints += ','; |
| 969 | if (RetWidth <= 32) { |
| 970 | Constraints += "={eax}"; |
| 971 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 972 | } else { |
| 973 | // Use the 'A' constraint for EAX:EDX. |
| 974 | Constraints += "=A"; |
| 975 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 976 | } |
| 977 | |
| 978 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 979 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 980 | ResultTruncRegTypes.push_back(CoerceTy); |
| 981 | |
| 982 | // Coerce the integer by bitcasting the return slot pointer. |
| 983 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 984 | CoerceTy->getPointerTo())); |
| 985 | ResultRegDests.push_back(ReturnSlot); |
| 986 | |
| 987 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 988 | } |
| 989 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 990 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 991 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 992 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 993 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 994 | uint64_t Size = Context.getTypeSize(Ty); |
| 995 | |
| 996 | // Type must be register sized. |
| 997 | if (!isRegisterSize(Size)) |
| 998 | return false; |
| 999 | |
| 1000 | if (Ty->isVectorType()) { |
| 1001 | // 64- and 128- bit vectors inside structures are not returned in |
| 1002 | // registers. |
| 1003 | if (Size == 64 || Size == 128) |
| 1004 | return false; |
| 1005 | |
| 1006 | return true; |
| 1007 | } |
| 1008 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1009 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1010 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1011 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1012 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1013 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1014 | return true; |
| 1015 | |
| 1016 | // Arrays are treated like records. |
| 1017 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1018 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1019 | |
| 1020 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1021 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1022 | if (!RT) return false; |
| 1023 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1024 | // FIXME: Traverse bases here too. |
| 1025 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1026 | // Structure types are passed in register if all fields would be |
| 1027 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1028 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1029 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1030 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1031 | continue; |
| 1032 | |
| 1033 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1034 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1035 | return false; |
| 1036 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1037 | return true; |
| 1038 | } |
| 1039 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1040 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1041 | // If the return value is indirect, then the hidden argument is consuming one |
| 1042 | // integer register. |
| 1043 | if (State.FreeRegs) { |
| 1044 | --State.FreeRegs; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1045 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1046 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1047 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1050 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1051 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1052 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1053 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1054 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1055 | const Type *Base = nullptr; |
| 1056 | uint64_t NumElts = 0; |
| 1057 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1058 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1059 | // The LLVM struct type for such an aggregate should lower properly. |
| 1060 | return ABIArgInfo::getDirect(); |
| 1061 | } |
| 1062 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1063 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1064 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1065 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1066 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1067 | |
| 1068 | // 128-bit vectors are a special case; they are returned in |
| 1069 | // registers and we need to make sure to pick a type the LLVM |
| 1070 | // backend will like. |
| 1071 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1072 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1073 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1074 | |
| 1075 | // Always return in register if it fits in a general purpose |
| 1076 | // register, or if it is 64 bits and has a single element. |
| 1077 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1078 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1079 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1080 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1081 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1082 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1086 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1087 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1088 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1089 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1090 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1091 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1092 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1093 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1094 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1095 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1096 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1097 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1098 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1099 | // Small structures which are register sized are generally returned |
| 1100 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1101 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1102 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1103 | |
| 1104 | // As a special-case, if the struct is a "single-element" struct, and |
| 1105 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1106 | // floating-point register. (MSVC does not apply this special case.) |
| 1107 | // We apply a similar transformation for pointer types to improve the |
| 1108 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1109 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1110 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1111 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1112 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1113 | |
| 1114 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1115 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1116 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1119 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1120 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1121 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1122 | // Treat an enum type as its underlying type. |
| 1123 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1124 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1125 | |
| 1126 | return (RetTy->isPromotableIntegerType() ? |
| 1127 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1130 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1131 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1132 | } |
| 1133 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1134 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1135 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1136 | if (!RT) |
| 1137 | return 0; |
| 1138 | const RecordDecl *RD = RT->getDecl(); |
| 1139 | |
| 1140 | // If this is a C++ record, check the bases first. |
| 1141 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1142 | for (const auto &I : CXXRD->bases()) |
| 1143 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1144 | return false; |
| 1145 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1146 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1147 | QualType FT = i->getType(); |
| 1148 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1149 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1150 | return true; |
| 1151 | |
| 1152 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1153 | return true; |
| 1154 | } |
| 1155 | |
| 1156 | return false; |
| 1157 | } |
| 1158 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1159 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1160 | unsigned Align) const { |
| 1161 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1162 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1163 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1164 | return 0; // Use default alignment. |
| 1165 | |
| 1166 | // On non-Darwin, the stack type alignment is always 4. |
| 1167 | if (!IsDarwinVectorABI) { |
| 1168 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1169 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1170 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1171 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1172 | // 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] | 1173 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1174 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1175 | return 16; |
| 1176 | |
| 1177 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1180 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1181 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1182 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1183 | if (State.FreeRegs) { |
| 1184 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1185 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1186 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1187 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1188 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1189 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1190 | // Compute the byval alignment. |
| 1191 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1192 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1193 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1194 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1195 | |
| 1196 | // If the stack alignment is less than the type alignment, realign the |
| 1197 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1198 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1199 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1200 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1203 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1204 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1205 | if (!T) |
| 1206 | T = Ty.getTypePtr(); |
| 1207 | |
| 1208 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1209 | BuiltinType::Kind K = BT->getKind(); |
| 1210 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1211 | return Float; |
| 1212 | } |
| 1213 | return Integer; |
| 1214 | } |
| 1215 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1216 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 1217 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1218 | NeedsPadding = false; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1219 | if (!IsSoftFloatABI) { |
| 1220 | Class C = classify(Ty); |
| 1221 | if (C == Float) |
| 1222 | return false; |
| 1223 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1224 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1225 | unsigned Size = getContext().getTypeSize(Ty); |
| 1226 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1227 | |
| 1228 | if (SizeInRegs == 0) |
| 1229 | return false; |
| 1230 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1231 | if (!IsMCUABI) { |
| 1232 | if (SizeInRegs > State.FreeRegs) { |
| 1233 | State.FreeRegs = 0; |
| 1234 | return false; |
| 1235 | } |
| 1236 | } else { |
| 1237 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1238 | // earlier parameters that are passed on the stack. Also, |
| 1239 | // it does not allow passing >8-byte structs in-register, |
| 1240 | // even if there are 3 free registers available. |
| 1241 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1242 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1243 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1244 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1245 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1246 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1247 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1248 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1249 | if (Size > 32) |
| 1250 | return false; |
| 1251 | |
| 1252 | if (Ty->isIntegralOrEnumerationType()) |
| 1253 | return true; |
| 1254 | |
| 1255 | if (Ty->isPointerType()) |
| 1256 | return true; |
| 1257 | |
| 1258 | if (Ty->isReferenceType()) |
| 1259 | return true; |
| 1260 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1261 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1262 | NeedsPadding = true; |
| 1263 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1264 | return false; |
| 1265 | } |
| 1266 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1267 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1270 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1271 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1272 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1273 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1274 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1275 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1276 | // Check with the C++ ABI first. |
| 1277 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1278 | if (RT) { |
| 1279 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1280 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1281 | return getIndirectResult(Ty, false, State); |
| 1282 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1283 | // The field index doesn't matter, we'll fix it up later. |
| 1284 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1289 | // to other targets. |
| 1290 | const Type *Base = nullptr; |
| 1291 | uint64_t NumElts = 0; |
| 1292 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1293 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1294 | if (State.FreeSSERegs >= NumElts) { |
| 1295 | State.FreeSSERegs -= NumElts; |
| 1296 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1297 | return ABIArgInfo::getDirect(); |
| 1298 | return ABIArgInfo::getExpand(); |
| 1299 | } |
| 1300 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1301 | } |
| 1302 | |
| 1303 | if (isAggregateTypeForABI(Ty)) { |
| 1304 | if (RT) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1305 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1306 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1307 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1308 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1309 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1310 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1311 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1312 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1313 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 1314 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 1315 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1316 | return ABIArgInfo::getIgnore(); |
| 1317 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1318 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1319 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 1320 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1321 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1322 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1323 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1324 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 1325 | return ABIArgInfo::getDirectInReg(Result); |
| 1326 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1327 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1328 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1329 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1330 | // of those arguments will match the struct. This is important because the |
| 1331 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1332 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1333 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 1334 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1335 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1336 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1337 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1338 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1339 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1340 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1343 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1344 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1345 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1346 | if (IsDarwinVectorABI) { |
| 1347 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1348 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1349 | (Size == 64 && VT->getNumElements() == 1)) |
| 1350 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1351 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1352 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1353 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1354 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1355 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1356 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1357 | return ABIArgInfo::getDirect(); |
| 1358 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1359 | |
| 1360 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1361 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1362 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1363 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1364 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1365 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1366 | |
| 1367 | if (Ty->isPromotableIntegerType()) { |
| 1368 | if (InReg) |
| 1369 | return ABIArgInfo::getExtendInReg(); |
| 1370 | return ABIArgInfo::getExtend(); |
| 1371 | } |
| 1372 | if (InReg) |
| 1373 | return ABIArgInfo::getDirectInReg(); |
| 1374 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1377 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1378 | CCState State(FI.getCallingConvention()); |
| 1379 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 1380 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1381 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1382 | State.FreeRegs = 2; |
| 1383 | State.FreeSSERegs = 6; |
| 1384 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1385 | State.FreeRegs = FI.getRegParm(); |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1386 | else if (IsMCUABI) |
| 1387 | State.FreeRegs = 3; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1388 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1389 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1390 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1391 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1392 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1393 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1394 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1395 | // return value was sret and put it in a register ourselves if appropriate. |
| 1396 | if (State.FreeRegs) { |
| 1397 | --State.FreeRegs; // The sret parameter consumes a register. |
| 1398 | FI.getReturnInfo().setInReg(true); |
| 1399 | } |
| 1400 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1401 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1402 | // The chain argument effectively gives us another free register. |
| 1403 | if (FI.isChainCall()) |
| 1404 | ++State.FreeRegs; |
| 1405 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1406 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1407 | for (auto &I : FI.arguments()) { |
| 1408 | I.info = classifyArgumentType(I.type, State); |
| 1409 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1413 | // all the memory arguments to use inalloca. |
| 1414 | if (UsedInAlloca) |
| 1415 | rewriteWithInAlloca(FI); |
| 1416 | } |
| 1417 | |
| 1418 | void |
| 1419 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1420 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1421 | QualType Type) const { |
| 1422 | // Arguments are always 4-byte-aligned. |
| 1423 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1424 | |
| 1425 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1426 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1427 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1428 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1429 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1430 | // Insert padding bytes to respect alignment. |
| 1431 | CharUnits FieldEnd = StackOffset; |
| 1432 | StackOffset = FieldEnd.RoundUpToAlignment(FieldAlign); |
| 1433 | if (StackOffset != FieldEnd) { |
| 1434 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1435 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1436 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1437 | FrameFields.push_back(Ty); |
| 1438 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1441 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1442 | // Leave ignored and inreg arguments alone. |
| 1443 | switch (Info.getKind()) { |
| 1444 | case ABIArgInfo::InAlloca: |
| 1445 | return true; |
| 1446 | case ABIArgInfo::Indirect: |
| 1447 | assert(Info.getIndirectByVal()); |
| 1448 | return true; |
| 1449 | case ABIArgInfo::Ignore: |
| 1450 | return false; |
| 1451 | case ABIArgInfo::Direct: |
| 1452 | case ABIArgInfo::Extend: |
| 1453 | case ABIArgInfo::Expand: |
| 1454 | if (Info.getInReg()) |
| 1455 | return false; |
| 1456 | return true; |
| 1457 | } |
| 1458 | llvm_unreachable("invalid enum"); |
| 1459 | } |
| 1460 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1461 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1462 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1463 | |
| 1464 | // Build a packed struct type for all of the arguments in memory. |
| 1465 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1466 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1467 | // The stack alignment is always 4. |
| 1468 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1469 | |
| 1470 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1471 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1472 | |
| 1473 | // Put 'this' into the struct before 'sret', if necessary. |
| 1474 | bool IsThisCall = |
| 1475 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1476 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1477 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1478 | isArgInAlloca(I->info)) { |
| 1479 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1480 | ++I; |
| 1481 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1482 | |
| 1483 | // 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] | 1484 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1485 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1486 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1487 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1488 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1492 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1493 | ++I; |
| 1494 | |
| 1495 | // Put arguments passed in memory into the struct. |
| 1496 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1497 | if (isArgInAlloca(I->info)) |
| 1498 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1502 | /*isPacked=*/true), |
| 1503 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1506 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1507 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1508 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1509 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1510 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1511 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1512 | // |
| 1513 | // Just messing with TypeInfo like this works because we never pass |
| 1514 | // anything indirectly. |
| 1515 | TypeInfo.second = CharUnits::fromQuantity( |
| 1516 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1517 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1518 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1519 | TypeInfo, CharUnits::fromQuantity(4), |
| 1520 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1523 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1524 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1525 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1526 | |
| 1527 | switch (Opts.getStructReturnConvention()) { |
| 1528 | case CodeGenOptions::SRCK_Default: |
| 1529 | break; |
| 1530 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1531 | return false; |
| 1532 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1533 | return true; |
| 1534 | } |
| 1535 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1536 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1537 | return true; |
| 1538 | |
| 1539 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1540 | case llvm::Triple::DragonFly: |
| 1541 | case llvm::Triple::FreeBSD: |
| 1542 | case llvm::Triple::OpenBSD: |
| 1543 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1544 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1545 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1546 | default: |
| 1547 | return false; |
| 1548 | } |
| 1549 | } |
| 1550 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1551 | void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1552 | llvm::GlobalValue *GV, |
| 1553 | CodeGen::CodeGenModule &CGM) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1554 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1555 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1556 | // Get the LLVM function. |
| 1557 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1558 | |
| 1559 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1560 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1561 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1562 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1563 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1564 | llvm::AttributeSet::FunctionIndex, |
| 1565 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1570 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1571 | CodeGen::CodeGenFunction &CGF, |
| 1572 | llvm::Value *Address) const { |
| 1573 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1574 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1575 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1576 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1577 | // 0-7 are the eight integer registers; the order is different |
| 1578 | // on Darwin (for EH), but the range is the same. |
| 1579 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1580 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1581 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1582 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1583 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1584 | // These have size 16, which is sizeof(long double) on |
| 1585 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1586 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1587 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1588 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1589 | } else { |
| 1590 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1591 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1592 | Builder.CreateAlignedStore( |
| 1593 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1594 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1595 | |
| 1596 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1597 | // These have size 12, which is sizeof(long double) on |
| 1598 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1599 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1600 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1601 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1602 | |
| 1603 | return false; |
| 1604 | } |
| 1605 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1606 | //===----------------------------------------------------------------------===// |
| 1607 | // X86-64 ABI Implementation |
| 1608 | //===----------------------------------------------------------------------===// |
| 1609 | |
| 1610 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1611 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1612 | /// The AVX ABI level for X86 targets. |
| 1613 | enum class X86AVXABILevel { |
| 1614 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1615 | AVX, |
| 1616 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1617 | }; |
| 1618 | |
| 1619 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1620 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1621 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1622 | case X86AVXABILevel::AVX512: |
| 1623 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1624 | case X86AVXABILevel::AVX: |
| 1625 | return 256; |
| 1626 | case X86AVXABILevel::None: |
| 1627 | return 128; |
| 1628 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1629 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1632 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1633 | class X86_64ABIInfo : public ABIInfo { |
| 1634 | enum Class { |
| 1635 | Integer = 0, |
| 1636 | SSE, |
| 1637 | SSEUp, |
| 1638 | X87, |
| 1639 | X87Up, |
| 1640 | ComplexX87, |
| 1641 | NoClass, |
| 1642 | Memory |
| 1643 | }; |
| 1644 | |
| 1645 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1646 | /// |
| 1647 | /// Merge an accumulating classification \arg Accum with a field |
| 1648 | /// classification \arg Field. |
| 1649 | /// |
| 1650 | /// \param Accum - The accumulating classification. This should |
| 1651 | /// always be either NoClass or the result of a previous merge |
| 1652 | /// call. In addition, this should never be Memory (the caller |
| 1653 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1654 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1655 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1656 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1657 | /// |
| 1658 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1659 | /// final MEMORY or SSE classes when necessary. |
| 1660 | /// |
| 1661 | /// \param AggregateSize - The size of the current aggregate in |
| 1662 | /// the classification process. |
| 1663 | /// |
| 1664 | /// \param Lo - The classification for the parts of the type |
| 1665 | /// residing in the low word of the containing object. |
| 1666 | /// |
| 1667 | /// \param Hi - The classification for the parts of the type |
| 1668 | /// residing in the higher words of the containing object. |
| 1669 | /// |
| 1670 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1671 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1672 | /// classify - Determine the x86_64 register classes in which the |
| 1673 | /// given type T should be passed. |
| 1674 | /// |
| 1675 | /// \param Lo - The classification for the parts of the type |
| 1676 | /// residing in the low word of the containing object. |
| 1677 | /// |
| 1678 | /// \param Hi - The classification for the parts of the type |
| 1679 | /// residing in the high word of the containing object. |
| 1680 | /// |
| 1681 | /// \param OffsetBase - The bit offset of this type in the |
| 1682 | /// containing object. Some parameters are classified different |
| 1683 | /// depending on whether they straddle an eightbyte boundary. |
| 1684 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1685 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1686 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1687 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1688 | /// If a word is unused its result will be NoClass; if a type should |
| 1689 | /// be passed in Memory then at least the classification of \arg Lo |
| 1690 | /// will be Memory. |
| 1691 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1692 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1693 | /// |
| 1694 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1695 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1696 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1697 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1698 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1699 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1700 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1701 | unsigned IROffset, QualType SourceTy, |
| 1702 | unsigned SourceOffset) const; |
| 1703 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1704 | unsigned IROffset, QualType SourceTy, |
| 1705 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1706 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1707 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1708 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1709 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1710 | |
| 1711 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1712 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1713 | /// |
| 1714 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1715 | /// available. |
| 1716 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1717 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1718 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1719 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1720 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1721 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1722 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1723 | unsigned &neededSSE, |
| 1724 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1725 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1726 | bool IsIllegalVectorType(QualType Ty) const; |
| 1727 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1728 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1729 | /// unfortunately in ways that were not always consistent with |
| 1730 | /// certain previous compilers. In particular, platforms which |
| 1731 | /// required strict binary compatibility with older versions of GCC |
| 1732 | /// may need to exempt themselves. |
| 1733 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1734 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1737 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1738 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1739 | // 64-bit hardware. |
| 1740 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1741 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1742 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1743 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
| 1744 | ABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1745 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1746 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1747 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1748 | bool isPassedUsingAVXType(QualType type) const { |
| 1749 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1750 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1751 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1752 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1753 | if (info.isDirect()) { |
| 1754 | llvm::Type *ty = info.getCoerceToType(); |
| 1755 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1756 | return (vectorTy->getBitWidth() > 128); |
| 1757 | } |
| 1758 | return false; |
| 1759 | } |
| 1760 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1761 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1762 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1763 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1764 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 1765 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1766 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1767 | |
| 1768 | bool has64BitPointers() const { |
| 1769 | return Has64BitPointers; |
| 1770 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1771 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1772 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1773 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1774 | class WinX86_64ABIInfo : public ABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1775 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 1776 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
| 1777 | : ABIInfo(CGT), |
| 1778 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1779 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1780 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1781 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1782 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1783 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1784 | |
| 1785 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1786 | // FIXME: Assumes vectorcall is in use. |
| 1787 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1788 | } |
| 1789 | |
| 1790 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1791 | uint64_t NumMembers) const override { |
| 1792 | // FIXME: Assumes vectorcall is in use. |
| 1793 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1794 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 1795 | |
| 1796 | private: |
| 1797 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 1798 | bool IsReturnType) const; |
| 1799 | |
| 1800 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1801 | }; |
| 1802 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1803 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1804 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1805 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1806 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1807 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1808 | const X86_64ABIInfo &getABIInfo() const { |
| 1809 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1810 | } |
| 1811 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1812 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1813 | return 7; |
| 1814 | } |
| 1815 | |
| 1816 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1817 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1818 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1819 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1820 | // 0-15 are the 16 integer registers. |
| 1821 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1822 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1823 | return false; |
| 1824 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1825 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1826 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1827 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1828 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1829 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1830 | } |
| 1831 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1832 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1833 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1834 | // The default CC on x86-64 sets %al to the number of SSA |
| 1835 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1836 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1837 | // that when AVX types are involved: the ABI explicitly states it is |
| 1838 | // undefined, and it doesn't work in practice because of how the ABI |
| 1839 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1840 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1841 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1842 | for (CallArgList::const_iterator |
| 1843 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1844 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1845 | HasAVXType = true; |
| 1846 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1847 | } |
| 1848 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1849 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1850 | if (!HasAVXType) |
| 1851 | return true; |
| 1852 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1853 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1854 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1857 | llvm::Constant * |
| 1858 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1859 | unsigned Sig; |
| 1860 | if (getABIInfo().has64BitPointers()) |
| 1861 | Sig = (0xeb << 0) | // jmp rel8 |
| 1862 | (0x0a << 8) | // .+0x0c |
| 1863 | ('F' << 16) | |
| 1864 | ('T' << 24); |
| 1865 | else |
| 1866 | Sig = (0xeb << 0) | // jmp rel8 |
| 1867 | (0x06 << 8) | // .+0x08 |
| 1868 | ('F' << 16) | |
| 1869 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1870 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1871 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1872 | }; |
| 1873 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1874 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 1875 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1876 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 1877 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1878 | |
| 1879 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1880 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1881 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 1882 | // If the argument contains a space, enclose it in quotes. |
| 1883 | if (Lib.find(" ") != StringRef::npos) |
| 1884 | Opt += "\"" + Lib.str() + "\""; |
| 1885 | else |
| 1886 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1887 | } |
| 1888 | }; |
| 1889 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1890 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1891 | // If the argument does not end in .lib, automatically add the suffix. |
| 1892 | // If the argument contains a space, enclose it in quotes. |
| 1893 | // This matches the behavior of MSVC. |
| 1894 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 1895 | std::string ArgStr = Quote ? "\"" : ""; |
| 1896 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1897 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1898 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1899 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1900 | return ArgStr; |
| 1901 | } |
| 1902 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1903 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1904 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1905 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1906 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 1907 | unsigned NumRegisterParameters) |
| 1908 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1909 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1910 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1911 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1912 | CodeGen::CodeGenModule &CGM) const override; |
| 1913 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1914 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1915 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1916 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1917 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1918 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1919 | |
| 1920 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1921 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1922 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1923 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1924 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1925 | }; |
| 1926 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1927 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 1928 | llvm::GlobalValue *GV, |
| 1929 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1930 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1931 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 1932 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1933 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1934 | Fn->addFnAttr("stack-probe-size", |
| 1935 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | } |
| 1939 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1940 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1941 | llvm::GlobalValue *GV, |
| 1942 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1943 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1944 | |
| 1945 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1946 | } |
| 1947 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1948 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1949 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1950 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1951 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1952 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1953 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1954 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1955 | CodeGen::CodeGenModule &CGM) const override; |
| 1956 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1957 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1958 | return 7; |
| 1959 | } |
| 1960 | |
| 1961 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1962 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1963 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1964 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1965 | // 0-15 are the 16 integer registers. |
| 1966 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1967 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1968 | return false; |
| 1969 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1970 | |
| 1971 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1972 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1973 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1974 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1975 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1976 | |
| 1977 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1978 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1979 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1980 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1981 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1982 | }; |
| 1983 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1984 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1985 | llvm::GlobalValue *GV, |
| 1986 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1987 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1988 | |
| 1989 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1990 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1991 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1992 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1993 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1994 | Class &Hi) const { |
| 1995 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1996 | // |
| 1997 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1998 | // memory. |
| 1999 | // |
| 2000 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2001 | // memory. |
| 2002 | // |
| 2003 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2004 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2005 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2006 | // ABI working for processors that don't support the __m256 type. |
| 2007 | // |
| 2008 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2009 | // |
| 2010 | // Some of these are enforced by the merging logic. Others can arise |
| 2011 | // only with unions; for example: |
| 2012 | // union { _Complex double; unsigned; } |
| 2013 | // |
| 2014 | // Note that clauses (b) and (c) were added in 0.98. |
| 2015 | // |
| 2016 | if (Hi == Memory) |
| 2017 | Lo = Memory; |
| 2018 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2019 | Lo = Memory; |
| 2020 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2021 | Lo = Memory; |
| 2022 | if (Hi == SSEUp && Lo != SSE) |
| 2023 | Hi = SSE; |
| 2024 | } |
| 2025 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2026 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2027 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2028 | // classified recursively so that always two fields are |
| 2029 | // considered. The resulting class is calculated according to |
| 2030 | // the classes of the fields in the eightbyte: |
| 2031 | // |
| 2032 | // (a) If both classes are equal, this is the resulting class. |
| 2033 | // |
| 2034 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2035 | // the other class. |
| 2036 | // |
| 2037 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2038 | // class. |
| 2039 | // |
| 2040 | // (d) If one of the classes is INTEGER, the result is the |
| 2041 | // INTEGER. |
| 2042 | // |
| 2043 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2044 | // MEMORY is used as class. |
| 2045 | // |
| 2046 | // (f) Otherwise class SSE is used. |
| 2047 | |
| 2048 | // Accum should never be memory (we should have returned) or |
| 2049 | // ComplexX87 (because this cannot be passed in a structure). |
| 2050 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2051 | "Invalid accumulated classification during merge."); |
| 2052 | if (Accum == Field || Field == NoClass) |
| 2053 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2054 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2055 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2056 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2057 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2058 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2059 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2060 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2061 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2062 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2063 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2066 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2067 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2068 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2069 | // Class pairs with appropriate constructor methods for the various |
| 2070 | // situations. |
| 2071 | |
| 2072 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2073 | // shouldn't be passed in registers for example, so there is no chance they |
| 2074 | // can straddle an eightbyte. Verify & simplify. |
| 2075 | |
| 2076 | Lo = Hi = NoClass; |
| 2077 | |
| 2078 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2079 | Current = Memory; |
| 2080 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2081 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2082 | BuiltinType::Kind k = BT->getKind(); |
| 2083 | |
| 2084 | if (k == BuiltinType::Void) { |
| 2085 | Current = NoClass; |
| 2086 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2087 | Lo = Integer; |
| 2088 | Hi = Integer; |
| 2089 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2090 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2091 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2092 | Current = SSE; |
| 2093 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2094 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2095 | if (LDF == &llvm::APFloat::IEEEquad) { |
| 2096 | Lo = SSE; |
| 2097 | Hi = SSEUp; |
| 2098 | } else if (LDF == &llvm::APFloat::x87DoubleExtended) { |
| 2099 | Lo = X87; |
| 2100 | Hi = X87Up; |
| 2101 | } else if (LDF == &llvm::APFloat::IEEEdouble) { |
| 2102 | Current = SSE; |
| 2103 | } else |
| 2104 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2105 | } |
| 2106 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2107 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2108 | return; |
| 2109 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2110 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2111 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2112 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2113 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2114 | return; |
| 2115 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2116 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2117 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2118 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2119 | return; |
| 2120 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2122 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2123 | if (Ty->isMemberFunctionPointerType()) { |
| 2124 | if (Has64BitPointers) { |
| 2125 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2126 | // Lo and Hi now. |
| 2127 | Lo = Hi = Integer; |
| 2128 | } else { |
| 2129 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2130 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2131 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2132 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2133 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2134 | Lo = Hi = Integer; |
| 2135 | } else { |
| 2136 | Current = Integer; |
| 2137 | } |
| 2138 | } |
| 2139 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2140 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2141 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2142 | return; |
| 2143 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2144 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2145 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2146 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2147 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2148 | // gcc passes the following as integer: |
| 2149 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2150 | // 2 bytes - <2 x char>, <1 x short> |
| 2151 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2152 | Current = Integer; |
| 2153 | |
| 2154 | // If this type crosses an eightbyte boundary, it should be |
| 2155 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2156 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2157 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2158 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2159 | Hi = Lo; |
| 2160 | } else if (Size == 64) { |
| 2161 | // gcc passes <1 x double> in memory. :( |
| 2162 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 2163 | return; |
| 2164 | |
| 2165 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 2166 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 2167 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2168 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 2169 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2170 | Current = Integer; |
| 2171 | else |
| 2172 | Current = SSE; |
| 2173 | |
| 2174 | // If this type crosses an eightbyte boundary, it should be |
| 2175 | // split. |
| 2176 | if (OffsetBase && OffsetBase != 64) |
| 2177 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2178 | } else if (Size == 128 || |
| 2179 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2180 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2181 | // least significant one belongs to class SSE and all the others to class |
| 2182 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2183 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2184 | // This design isn't correct for 256-bits, but since there're no cases |
| 2185 | // where the upper parts would need to be inspected, avoid adding |
| 2186 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2187 | // |
| 2188 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2189 | // registers if they are "named", i.e. not part of the "..." of a |
| 2190 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2191 | // |
| 2192 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2193 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2194 | Lo = SSE; |
| 2195 | Hi = SSEUp; |
| 2196 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2197 | return; |
| 2198 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2199 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2200 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2201 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2202 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2203 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2204 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2205 | if (Size <= 64) |
| 2206 | Current = Integer; |
| 2207 | else if (Size <= 128) |
| 2208 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2209 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2210 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2211 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2212 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2213 | } else if (ET == getContext().LongDoubleTy) { |
| 2214 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2215 | if (LDF == &llvm::APFloat::IEEEquad) |
| 2216 | Current = Memory; |
| 2217 | else if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 2218 | Current = ComplexX87; |
| 2219 | else if (LDF == &llvm::APFloat::IEEEdouble) |
| 2220 | Lo = Hi = SSE; |
| 2221 | else |
| 2222 | llvm_unreachable("unexpected long double representation!"); |
| 2223 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2224 | |
| 2225 | // If this complex type crosses an eightbyte boundary then it |
| 2226 | // should be split. |
| 2227 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2228 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2229 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2230 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2231 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2232 | return; |
| 2233 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2234 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2235 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2236 | // Arrays are treated like structures. |
| 2237 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2238 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2239 | |
| 2240 | // 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] | 2241 | // than four eightbytes, ..., it has class MEMORY. |
| 2242 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2243 | return; |
| 2244 | |
| 2245 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2246 | // fields, it has class MEMORY. |
| 2247 | // |
| 2248 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2249 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2250 | return; |
| 2251 | |
| 2252 | // Otherwise implement simplified merge. We could be smarter about |
| 2253 | // this, but it isn't worth it and would be harder to verify. |
| 2254 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2255 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2256 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2257 | |
| 2258 | // The only case a 256-bit wide vector could be used is when the array |
| 2259 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2260 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2261 | if (Size > 128 && EltSize != 256) |
| 2262 | return; |
| 2263 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2264 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2265 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2266 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2267 | Lo = merge(Lo, FieldLo); |
| 2268 | Hi = merge(Hi, FieldHi); |
| 2269 | if (Lo == Memory || Hi == Memory) |
| 2270 | break; |
| 2271 | } |
| 2272 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2273 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2274 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2275 | return; |
| 2276 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2277 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2278 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2279 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2280 | |
| 2281 | // 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] | 2282 | // than four eightbytes, ..., it has class MEMORY. |
| 2283 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2284 | return; |
| 2285 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2286 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2287 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2288 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2289 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2290 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2291 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2292 | const RecordDecl *RD = RT->getDecl(); |
| 2293 | |
| 2294 | // Assume variable sized types are passed in memory. |
| 2295 | if (RD->hasFlexibleArrayMember()) |
| 2296 | return; |
| 2297 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2298 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2299 | |
| 2300 | // Reset Lo class, this will be recomputed. |
| 2301 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2302 | |
| 2303 | // If this is a C++ record, classify the bases first. |
| 2304 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2305 | for (const auto &I : CXXRD->bases()) { |
| 2306 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2307 | "Unexpected base class!"); |
| 2308 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2309 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2310 | |
| 2311 | // Classify this field. |
| 2312 | // |
| 2313 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2314 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2315 | // initialized to class NO_CLASS. |
| 2316 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2317 | uint64_t Offset = |
| 2318 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2319 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2320 | Lo = merge(Lo, FieldLo); |
| 2321 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2322 | if (Lo == Memory || Hi == Memory) { |
| 2323 | postMerge(Size, Lo, Hi); |
| 2324 | return; |
| 2325 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2330 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2331 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2332 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2333 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2334 | bool BitField = i->isBitField(); |
| 2335 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2336 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2337 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2338 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2339 | // The only case a 256-bit wide vector could be used is when the struct |
| 2340 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2341 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2342 | // |
| 2343 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2344 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2345 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2346 | return; |
| 2347 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2348 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2349 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2350 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2351 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2352 | return; |
| 2353 | } |
| 2354 | |
| 2355 | // Classify this field. |
| 2356 | // |
| 2357 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2358 | // exceeds a single eightbyte, each is classified |
| 2359 | // separately. Each eightbyte gets initialized to class |
| 2360 | // NO_CLASS. |
| 2361 | Class FieldLo, FieldHi; |
| 2362 | |
| 2363 | // Bit-fields require special handling, they do not force the |
| 2364 | // structure to be passed in memory even if unaligned, and |
| 2365 | // therefore they can straddle an eightbyte. |
| 2366 | if (BitField) { |
| 2367 | // Ignore padding bit-fields. |
| 2368 | if (i->isUnnamedBitfield()) |
| 2369 | continue; |
| 2370 | |
| 2371 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2372 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2373 | |
| 2374 | uint64_t EB_Lo = Offset / 64; |
| 2375 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2376 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2377 | if (EB_Lo) { |
| 2378 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2379 | FieldLo = NoClass; |
| 2380 | FieldHi = Integer; |
| 2381 | } else { |
| 2382 | FieldLo = Integer; |
| 2383 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2384 | } |
| 2385 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2386 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2387 | Lo = merge(Lo, FieldLo); |
| 2388 | Hi = merge(Hi, FieldHi); |
| 2389 | if (Lo == Memory || Hi == Memory) |
| 2390 | break; |
| 2391 | } |
| 2392 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2393 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2397 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2398 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2399 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2400 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2401 | // Treat an enum type as its underlying type. |
| 2402 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2403 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2404 | |
| 2405 | return (Ty->isPromotableIntegerType() ? |
| 2406 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2407 | } |
| 2408 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2409 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2412 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2413 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2414 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2415 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2416 | if (Size <= 64 || Size > LargestVector) |
| 2417 | return true; |
| 2418 | } |
| 2419 | |
| 2420 | return false; |
| 2421 | } |
| 2422 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2423 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2424 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2425 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2426 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2427 | // |
| 2428 | // This assumption is optimistic, as there could be free registers available |
| 2429 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2430 | // the argument in the free register. This does not seem to happen currently, |
| 2431 | // but this code would be much safer if we could mark the argument with |
| 2432 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2433 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2434 | // Treat an enum type as its underlying type. |
| 2435 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2436 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2437 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2438 | return (Ty->isPromotableIntegerType() ? |
| 2439 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2440 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2441 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2442 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2443 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2444 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2445 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2446 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2447 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2448 | |
| 2449 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2450 | // is important for good codegen. |
| 2451 | // |
| 2452 | // We do this by coercing the value into a scalar type which the backend can |
| 2453 | // handle naturally (i.e., without using byval). |
| 2454 | // |
| 2455 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2456 | // free integer registers. Doing this when there are free integer registers |
| 2457 | // would require more care, as we would have to ensure that the coerced value |
| 2458 | // did not claim the unused register. That would require either reording the |
| 2459 | // arguments to the function (so that any subsequent inreg values came first), |
| 2460 | // or only doing this optimization when there were no following arguments that |
| 2461 | // might be inreg. |
| 2462 | // |
| 2463 | // We currently expect it to be rare (particularly in well written code) for |
| 2464 | // arguments to be passed on the stack when there are still free integer |
| 2465 | // registers available (this would typically imply large structs being passed |
| 2466 | // by value), so this seems like a fair tradeoff for now. |
| 2467 | // |
| 2468 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2469 | // attributes. See PR12193. |
| 2470 | if (freeIntRegs == 0) { |
| 2471 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2472 | |
| 2473 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2474 | // type, which will end up on the stack (with alignment 8). |
| 2475 | if (Align == 8 && Size <= 64) |
| 2476 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2477 | Size)); |
| 2478 | } |
| 2479 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2480 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2483 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2484 | /// 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] | 2485 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2486 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2487 | // vectors; strip them off if present. |
| 2488 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2489 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2490 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2491 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2492 | if (isa<llvm::VectorType>(IRType) || |
| 2493 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2494 | return IRType; |
| 2495 | |
| 2496 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2497 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2498 | assert((Size == 128 || Size == 256) && "Invalid type found!"); |
| 2499 | |
| 2500 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2501 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2502 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2505 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2506 | /// is known to either be off the end of the specified type or being in |
| 2507 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2508 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2509 | /// classification that put one of the two halves in the INTEGER class. |
| 2510 | /// |
| 2511 | /// It is conservatively correct to return false. |
| 2512 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2513 | unsigned EndBit, ASTContext &Context) { |
| 2514 | // If the bytes being queried are off the end of the type, there is no user |
| 2515 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2516 | // types that don't contain interesting padding. |
| 2517 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2518 | if (TySize <= StartBit) |
| 2519 | return true; |
| 2520 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2521 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2522 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2523 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2524 | |
| 2525 | // Check each element to see if the element overlaps with the queried range. |
| 2526 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2527 | // If the element is after the span we care about, then we're done.. |
| 2528 | unsigned EltOffset = i*EltSize; |
| 2529 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2530 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2531 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2532 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2533 | EndBit-EltOffset, Context)) |
| 2534 | return false; |
| 2535 | } |
| 2536 | // If it overlaps no elements, then it is safe to process as padding. |
| 2537 | return true; |
| 2538 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2539 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2540 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2541 | const RecordDecl *RD = RT->getDecl(); |
| 2542 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2543 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2544 | // If this is a C++ record, check the bases first. |
| 2545 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2546 | for (const auto &I : CXXRD->bases()) { |
| 2547 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2548 | "Unexpected base class!"); |
| 2549 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2550 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2551 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2552 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2553 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2554 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2555 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2556 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2557 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2558 | EndBit-BaseOffset, Context)) |
| 2559 | return false; |
| 2560 | } |
| 2561 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2562 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2563 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2564 | // this could be sped up a lot by being smarter about queried fields, |
| 2565 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2566 | // much. |
| 2567 | unsigned idx = 0; |
| 2568 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2569 | i != e; ++i, ++idx) { |
| 2570 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2571 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2572 | // If we found a field after the region we care about, then we're done. |
| 2573 | if (FieldOffset >= EndBit) break; |
| 2574 | |
| 2575 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2576 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2577 | Context)) |
| 2578 | return false; |
| 2579 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2580 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2581 | // If nothing in this record overlapped the area of interest, then we're |
| 2582 | // clean. |
| 2583 | return true; |
| 2584 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2585 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2586 | return false; |
| 2587 | } |
| 2588 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2589 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2590 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2591 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2592 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2593 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2594 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2595 | // Base case if we find a float. |
| 2596 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2597 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2598 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2599 | // 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] | 2600 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2601 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2602 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2603 | IROffset -= SL->getElementOffset(Elt); |
| 2604 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2605 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2606 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2607 | // 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] | 2608 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2609 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2610 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2611 | IROffset -= IROffset/EltSize*EltSize; |
| 2612 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2613 | } |
| 2614 | |
| 2615 | return false; |
| 2616 | } |
| 2617 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2618 | |
| 2619 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2620 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2621 | llvm::Type *X86_64ABIInfo:: |
| 2622 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2623 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2624 | // 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] | 2625 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2626 | // structs that contain 3 floats. |
| 2627 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2628 | SourceOffset*8+64, getContext())) |
| 2629 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2630 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2631 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2632 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2633 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2634 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2635 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2636 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2637 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2638 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2639 | } |
| 2640 | |
| 2641 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2642 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2643 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2644 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2645 | /// 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] | 2646 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2647 | /// etc). |
| 2648 | /// |
| 2649 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2650 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2651 | /// the 8-byte value references. PrefType may be null. |
| 2652 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2653 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2654 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2655 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2656 | llvm::Type *X86_64ABIInfo:: |
| 2657 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2658 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2659 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2660 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2661 | if (IROffset == 0) { |
| 2662 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2663 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2664 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2665 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2666 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2667 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2668 | // goodness in the source type is just tail padding. This is allowed to |
| 2669 | // kick in for struct {double,int} on the int, but not on |
| 2670 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2671 | // have to do this analysis on the source type because we can't depend on |
| 2672 | // unions being lowered a specific way etc. |
| 2673 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2674 | IRType->isIntegerTy(32) || |
| 2675 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2676 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2677 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2678 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2679 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2680 | SourceOffset*8+64, getContext())) |
| 2681 | return IRType; |
| 2682 | } |
| 2683 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2684 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2685 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2686 | // 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] | 2687 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2688 | if (IROffset < SL->getSizeInBytes()) { |
| 2689 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2690 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2691 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2692 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2693 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2694 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2695 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2696 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2697 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2698 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2699 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2700 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2701 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2702 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2703 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2704 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2705 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2706 | // 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] | 2707 | unsigned TySizeInBytes = |
| 2708 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2709 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2710 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2711 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2712 | // It is always safe to classify this as an integer type up to i64 that |
| 2713 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2714 | return llvm::IntegerType::get(getVMContext(), |
| 2715 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2718 | |
| 2719 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2720 | /// be used as elements of a two register pair to pass or return, return a |
| 2721 | /// first class aggregate to represent them. For example, if the low part of |
| 2722 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2723 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2724 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2725 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2726 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2727 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2728 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2729 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2730 | // the second element at offset 8. Check for this: |
| 2731 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2732 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
David Majnemer | ed68407 | 2014-10-20 06:13:36 +0000 | [diff] [blame] | 2733 | unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2734 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2735 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2736 | // To handle this, we have to increase the size of the low part so that the |
| 2737 | // second element will start at an 8 byte offset. We can't increase the size |
| 2738 | // of the second element because it might make us access off the end of the |
| 2739 | // struct. |
| 2740 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 2741 | // There are usually two sorts of types the ABI generation code can produce |
| 2742 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 2743 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 2744 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2745 | // Promote these to a larger type. |
| 2746 | if (Lo->isFloatTy()) |
| 2747 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2748 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 2749 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 2750 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2751 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2752 | } |
| 2753 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2754 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2755 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2756 | |
| 2757 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2758 | // Verify that the second element is at an 8-byte offset. |
| 2759 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2760 | "Invalid x86-64 argument pair!"); |
| 2761 | return Result; |
| 2762 | } |
| 2763 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2764 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2765 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2766 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2767 | // classification algorithm. |
| 2768 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2769 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2770 | |
| 2771 | // Check some invariants. |
| 2772 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2773 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2774 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2775 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2776 | switch (Lo) { |
| 2777 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2778 | if (Hi == NoClass) |
| 2779 | return ABIArgInfo::getIgnore(); |
| 2780 | // If the low part is just padding, it takes no register, leave ResType |
| 2781 | // null. |
| 2782 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2783 | "Unknown missing lo part"); |
| 2784 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2785 | |
| 2786 | case SSEUp: |
| 2787 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2788 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2789 | |
| 2790 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2791 | // hidden argument. |
| 2792 | case Memory: |
| 2793 | return getIndirectReturnResult(RetTy); |
| 2794 | |
| 2795 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2796 | // available register of the sequence %rax, %rdx is used. |
| 2797 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2798 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2799 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2800 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2801 | // so that the parameter gets the right LLVM IR attributes. |
| 2802 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2803 | // Treat an enum type as its underlying type. |
| 2804 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2805 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2806 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2807 | if (RetTy->isIntegralOrEnumerationType() && |
| 2808 | RetTy->isPromotableIntegerType()) |
| 2809 | return ABIArgInfo::getExtend(); |
| 2810 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2811 | break; |
| 2812 | |
| 2813 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2814 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2815 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2816 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2817 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2818 | |
| 2819 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2820 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2821 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2822 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2823 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2824 | |
| 2825 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2826 | // part of the value is returned in %st0 and the imaginary part in |
| 2827 | // %st1. |
| 2828 | case ComplexX87: |
| 2829 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2830 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2831 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2832 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2833 | break; |
| 2834 | } |
| 2835 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2836 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2837 | switch (Hi) { |
| 2838 | // Memory was handled previously and X87 should |
| 2839 | // never occur as a hi class. |
| 2840 | case Memory: |
| 2841 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2842 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2843 | |
| 2844 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2845 | case NoClass: |
| 2846 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2847 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2848 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2849 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2850 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2851 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2852 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2853 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2854 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2855 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2856 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2857 | break; |
| 2858 | |
| 2859 | // 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] | 2860 | // is passed in the next available eightbyte chunk if the last used |
| 2861 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2862 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2863 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2864 | case SSEUp: |
| 2865 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2866 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2867 | break; |
| 2868 | |
| 2869 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2870 | // returned together with the previous X87 value in %st0. |
| 2871 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2872 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2873 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2874 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2875 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2876 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2877 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2878 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2879 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2880 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2881 | break; |
| 2882 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2883 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2884 | // 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] | 2885 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2886 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2887 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2888 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2889 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2890 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2893 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2894 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2895 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2896 | const |
| 2897 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 2898 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 2899 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2900 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2901 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2902 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2903 | // Check some invariants. |
| 2904 | // FIXME: Enforce these by construction. |
| 2905 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2906 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2907 | |
| 2908 | neededInt = 0; |
| 2909 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2910 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2911 | switch (Lo) { |
| 2912 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2913 | if (Hi == NoClass) |
| 2914 | return ABIArgInfo::getIgnore(); |
| 2915 | // If the low part is just padding, it takes no register, leave ResType |
| 2916 | // null. |
| 2917 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2918 | "Unknown missing lo part"); |
| 2919 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2920 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2921 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2922 | // on the stack. |
| 2923 | case Memory: |
| 2924 | |
| 2925 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2926 | // COMPLEX_X87, it is passed in memory. |
| 2927 | case X87: |
| 2928 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2929 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2930 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2931 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2932 | |
| 2933 | case SSEUp: |
| 2934 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2935 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2936 | |
| 2937 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2938 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2939 | // and %r9 is used. |
| 2940 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2941 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2942 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2943 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2944 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2945 | |
| 2946 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2947 | // so that the parameter gets the right LLVM IR attributes. |
| 2948 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2949 | // Treat an enum type as its underlying type. |
| 2950 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2951 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2952 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2953 | if (Ty->isIntegralOrEnumerationType() && |
| 2954 | Ty->isPromotableIntegerType()) |
| 2955 | return ABIArgInfo::getExtend(); |
| 2956 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2957 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2958 | break; |
| 2959 | |
| 2960 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2961 | // available SSE register is used, the registers are taken in the |
| 2962 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2963 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2964 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2965 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2966 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2967 | break; |
| 2968 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2969 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2970 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2971 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2972 | switch (Hi) { |
| 2973 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2974 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2975 | // which is passed in memory. |
| 2976 | case Memory: |
| 2977 | case X87: |
| 2978 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2979 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2980 | |
| 2981 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2982 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2983 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2984 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2985 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2986 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2987 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2988 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2989 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2990 | break; |
| 2991 | |
| 2992 | // X87Up generally doesn't occur here (long double is passed in |
| 2993 | // memory), except in situations involving unions. |
| 2994 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2995 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2996 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2997 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2998 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2999 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3000 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3001 | ++neededSSE; |
| 3002 | break; |
| 3003 | |
| 3004 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3005 | // 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] | 3006 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3007 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3008 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3009 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3010 | break; |
| 3011 | } |
| 3012 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3013 | // If a high part was specified, merge it together with the low part. It is |
| 3014 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3015 | // first class struct aggregate with the high and low part: {low, high} |
| 3016 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3017 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3018 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3019 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3022 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3023 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3024 | if (!getCXXABI().classifyReturnType(FI)) |
| 3025 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3026 | |
| 3027 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3028 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3029 | |
| 3030 | // If the return value is indirect, then the hidden argument is consuming one |
| 3031 | // integer register. |
| 3032 | if (FI.getReturnInfo().isIndirect()) |
| 3033 | --freeIntRegs; |
| 3034 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3035 | // The chain argument effectively gives us another free register. |
| 3036 | if (FI.isChainCall()) |
| 3037 | ++freeIntRegs; |
| 3038 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3039 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3040 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3041 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3042 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3043 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3044 | it != ie; ++it, ++ArgNo) { |
| 3045 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3046 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3047 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3048 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3049 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3050 | |
| 3051 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3052 | // eightbyte of an argument, the whole argument is passed on the |
| 3053 | // stack. If registers have already been assigned for some |
| 3054 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3055 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3056 | freeIntRegs -= neededInt; |
| 3057 | freeSSERegs -= neededSSE; |
| 3058 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3059 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3060 | } |
| 3061 | } |
| 3062 | } |
| 3063 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3064 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3065 | Address VAListAddr, QualType Ty) { |
| 3066 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3067 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3068 | llvm::Value *overflow_arg_area = |
| 3069 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3070 | |
| 3071 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3072 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3073 | // It isn't stated explicitly in the standard, but in practice we use |
| 3074 | // alignment greater than 16 where necessary. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3075 | uint64_t Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3076 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3077 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3078 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3079 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3080 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 3081 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3082 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3083 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3084 | overflow_arg_area = |
| 3085 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 3086 | overflow_arg_area->getType(), |
| 3087 | "overflow_arg_area.align"); |
| 3088 | } |
| 3089 | |
| 3090 | // 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] | 3091 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3092 | llvm::Value *Res = |
| 3093 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3094 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3095 | |
| 3096 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3097 | // l->overflow_arg_area + sizeof(type). |
| 3098 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3099 | // an 8 byte boundary. |
| 3100 | |
| 3101 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3102 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3103 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3104 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3105 | "overflow_arg_area.next"); |
| 3106 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3107 | |
| 3108 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3109 | return Address(Res, CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3112 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3113 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3114 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3115 | // struct { |
| 3116 | // i32 gp_offset; |
| 3117 | // i32 fp_offset; |
| 3118 | // i8* overflow_arg_area; |
| 3119 | // i8* reg_save_area; |
| 3120 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3121 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3122 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3123 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3124 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3125 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3126 | |
| 3127 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3128 | // in the registers. If not go to step 7. |
| 3129 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3130 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3131 | |
| 3132 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3133 | // general purpose registers needed to pass type and num_fp to hold |
| 3134 | // the number of floating point registers needed. |
| 3135 | |
| 3136 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3137 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3138 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3139 | // |
| 3140 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3141 | // register save space). |
| 3142 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3143 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3144 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3145 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3146 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3147 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3148 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3149 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3150 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3151 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3152 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3156 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3157 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3158 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3159 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3160 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3161 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3162 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3163 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3164 | } |
| 3165 | |
| 3166 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3167 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3168 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3169 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3170 | |
| 3171 | // Emit code to load the value if it was passed in registers. |
| 3172 | |
| 3173 | CGF.EmitBlock(InRegBlock); |
| 3174 | |
| 3175 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3176 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3177 | // copying to a temporary location in case the parameter is passed |
| 3178 | // in different register classes or requires an alignment greater |
| 3179 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3180 | // |
| 3181 | // FIXME: This really results in shameful code when we end up needing to |
| 3182 | // collect arguments from different places; often what should result in a |
| 3183 | // simple assembling of a structure from scattered addresses has many more |
| 3184 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3185 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3186 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3187 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3188 | "reg_save_area"); |
| 3189 | |
| 3190 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3191 | if (neededInt && neededSSE) { |
| 3192 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3193 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3194 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3195 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3196 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3197 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3198 | llvm::Type *TyLo = ST->getElementType(0); |
| 3199 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3200 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3201 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3202 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3203 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3204 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3205 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3206 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3207 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3208 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3209 | // Copy the first element. |
| 3210 | llvm::Value *V = |
| 3211 | CGF.Builder.CreateDefaultAlignedLoad( |
| 3212 | CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 3213 | CGF.Builder.CreateStore(V, |
| 3214 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3215 | |
| 3216 | // Copy the second element. |
| 3217 | V = CGF.Builder.CreateDefaultAlignedLoad( |
| 3218 | CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 3219 | CharUnits Offset = CharUnits::fromQuantity( |
| 3220 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3221 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3222 | |
| 3223 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3224 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3225 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3226 | CharUnits::fromQuantity(8)); |
| 3227 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3228 | |
| 3229 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3230 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3231 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3232 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3233 | CharUnits TyAlign = SizeAlign.second; |
| 3234 | |
| 3235 | // Copy into a temporary if the type is more aligned than the |
| 3236 | // register save area. |
| 3237 | if (TyAlign.getQuantity() > 8) { |
| 3238 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3239 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3240 | RegAddr = Tmp; |
| 3241 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3242 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3243 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3244 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3245 | CharUnits::fromQuantity(16)); |
| 3246 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3247 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3248 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3249 | // SSE registers are spaced 16 bytes apart in the register save |
| 3250 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3251 | // The ABI isn't explicit about this, but it seems reasonable |
| 3252 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3253 | // naturally 16-byte aligned and the prologue is expected to store |
| 3254 | // all the SSE registers to the RSA. |
| 3255 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3256 | CharUnits::fromQuantity(16)); |
| 3257 | Address RegAddrHi = |
| 3258 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3259 | CharUnits::fromQuantity(16)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3260 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3261 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3262 | llvm::Value *V; |
| 3263 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3264 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3265 | V = CGF.Builder.CreateLoad( |
| 3266 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3267 | CGF.Builder.CreateStore(V, |
| 3268 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3269 | V = CGF.Builder.CreateLoad( |
| 3270 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3271 | CGF.Builder.CreateStore(V, |
| 3272 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3273 | |
| 3274 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3278 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3279 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3280 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3281 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3282 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3283 | gp_offset_p); |
| 3284 | } |
| 3285 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3286 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3287 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3288 | fp_offset_p); |
| 3289 | } |
| 3290 | CGF.EmitBranch(ContBlock); |
| 3291 | |
| 3292 | // Emit code to load the value if it was passed in memory. |
| 3293 | |
| 3294 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3295 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3296 | |
| 3297 | // Return the appropriate result. |
| 3298 | |
| 3299 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3300 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3301 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3302 | return ResAddr; |
| 3303 | } |
| 3304 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3305 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3306 | QualType Ty) const { |
| 3307 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3308 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3309 | CharUnits::fromQuantity(8), |
| 3310 | /*allowHigherAlign*/ false); |
| 3311 | } |
| 3312 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3313 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 3314 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3315 | |
| 3316 | if (Ty->isVoidType()) |
| 3317 | return ABIArgInfo::getIgnore(); |
| 3318 | |
| 3319 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3320 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3321 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3322 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3323 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3324 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3325 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3326 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3327 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3328 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3329 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3330 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3334 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3335 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3336 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3337 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3338 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3339 | // other targets. |
| 3340 | const Type *Base = nullptr; |
| 3341 | uint64_t NumElts = 0; |
| 3342 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3343 | if (FreeSSERegs >= NumElts) { |
| 3344 | FreeSSERegs -= NumElts; |
| 3345 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3346 | return ABIArgInfo::getDirect(); |
| 3347 | return ABIArgInfo::getExpand(); |
| 3348 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3349 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3353 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3354 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3355 | // directly. |
| 3356 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3357 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3358 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3361 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3362 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3363 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3364 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3365 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3366 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3367 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3368 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3371 | // Bool type is always extended to the ABI, other builtin types are not |
| 3372 | // extended. |
| 3373 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3374 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3375 | return ABIArgInfo::getExtend(); |
| 3376 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3377 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3378 | // passes them indirectly through memory. |
| 3379 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3380 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 3381 | if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 3382 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3383 | } |
| 3384 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3385 | return ABIArgInfo::getDirect(); |
| 3386 | } |
| 3387 | |
| 3388 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3389 | bool IsVectorCall = |
| 3390 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3391 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3392 | // We can use up to 4 SSE return registers with vectorcall. |
| 3393 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3394 | if (!getCXXABI().classifyReturnType(FI)) |
| 3395 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3396 | |
| 3397 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3398 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3399 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3400 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3403 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3404 | QualType Ty) const { |
| 3405 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3406 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3407 | CharUnits::fromQuantity(8), |
| 3408 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3409 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3410 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3411 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3412 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3413 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3414 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3415 | public: |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3416 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 3417 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3418 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3419 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3420 | }; |
| 3421 | |
| 3422 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3423 | public: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3424 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) |
| 3425 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3426 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3427 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3428 | // This is recovered from gcc output. |
| 3429 | return 1; // r1 is the dedicated stack pointer |
| 3430 | } |
| 3431 | |
| 3432 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3433 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3434 | }; |
| 3435 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3436 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3437 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3438 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 3439 | QualType Ty) const { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3440 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3441 | // TODO: Implement this. For now ignore. |
| 3442 | (void)CTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3443 | return Address::invalid(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3446 | // struct __va_list_tag { |
| 3447 | // unsigned char gpr; |
| 3448 | // unsigned char fpr; |
| 3449 | // unsigned short reserved; |
| 3450 | // void *overflow_arg_area; |
| 3451 | // void *reg_save_area; |
| 3452 | // }; |
| 3453 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3454 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3455 | bool isInt = |
| 3456 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3457 | |
| 3458 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 3459 | // with the argument-lowering code. |
| 3460 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3461 | |
| 3462 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3463 | |
| 3464 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 3465 | Address NumRegsAddr = Address::invalid(); |
| 3466 | if (isInt) { |
| 3467 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 3468 | } else { |
| 3469 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3470 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3471 | |
| 3472 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 3473 | |
| 3474 | // "Align" the register count when TY is i64. |
| 3475 | if (isI64) { |
| 3476 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 3477 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 3478 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3479 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3480 | llvm::Value *CC = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3481 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(8), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3482 | |
| 3483 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3484 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3485 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3486 | |
| 3487 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3488 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3489 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 3490 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3491 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3492 | // Case 1: consume registers. |
| 3493 | Address RegAddr = Address::invalid(); |
| 3494 | { |
| 3495 | CGF.EmitBlock(UsingRegs); |
| 3496 | |
| 3497 | Address RegSaveAreaPtr = |
| 3498 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 3499 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 3500 | CharUnits::fromQuantity(8)); |
| 3501 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 3502 | |
| 3503 | // Floating-point registers start after the general-purpose registers. |
| 3504 | if (!isInt) { |
| 3505 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 3506 | CharUnits::fromQuantity(32)); |
| 3507 | } |
| 3508 | |
| 3509 | // Get the address of the saved value by scaling the number of |
| 3510 | // registers we've used by the number of |
| 3511 | CharUnits RegSize = CharUnits::fromQuantity(isInt ? 4 : 8); |
| 3512 | llvm::Value *RegOffset = |
| 3513 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 3514 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 3515 | RegAddr.getPointer(), RegOffset), |
| 3516 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 3517 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 3518 | |
| 3519 | // Increase the used-register count. |
| 3520 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(isI64 ? 2 : 1)); |
| 3521 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 3522 | |
| 3523 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3524 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3525 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3526 | // Case 2: consume space in the overflow area. |
| 3527 | Address MemAddr = Address::invalid(); |
| 3528 | { |
| 3529 | CGF.EmitBlock(UsingOverflow); |
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 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 3532 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 3533 | |
| 3534 | CharUnits Size; |
| 3535 | if (!isIndirect) { |
| 3536 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 3537 | Size = TypeInfo.first.RoundUpToAlignment(OverflowAreaAlign); |
| 3538 | } else { |
| 3539 | Size = CGF.getPointerSize(); |
| 3540 | } |
| 3541 | |
| 3542 | Address OverflowAreaAddr = |
| 3543 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
| 3544 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr), |
| 3545 | OverflowAreaAlign); |
| 3546 | |
| 3547 | // The current address is the address of the varargs element. |
| 3548 | // FIXME: do we not need to round up to alignment? |
| 3549 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 3550 | |
| 3551 | // Increase the overflow area. |
| 3552 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 3553 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 3554 | CGF.EmitBranch(Cont); |
| 3555 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3556 | |
| 3557 | CGF.EmitBlock(Cont); |
| 3558 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3559 | // Merge the cases with a phi. |
| 3560 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 3561 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3562 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3563 | // Load the pointer if the argument was passed indirectly. |
| 3564 | if (isIndirect) { |
| 3565 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 3566 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
| 3569 | return Result; |
| 3570 | } |
| 3571 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3572 | bool |
| 3573 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3574 | llvm::Value *Address) const { |
| 3575 | // This is calculated from the LLVM and GCC tables and verified |
| 3576 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3577 | |
| 3578 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3579 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3580 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3581 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3582 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3583 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3584 | |
| 3585 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3586 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3587 | |
| 3588 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3589 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3590 | |
| 3591 | // 64-76 are various 4-byte special-purpose registers: |
| 3592 | // 64: mq |
| 3593 | // 65: lr |
| 3594 | // 66: ctr |
| 3595 | // 67: ap |
| 3596 | // 68-75 cr0-7 |
| 3597 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3598 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3599 | |
| 3600 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3601 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3602 | |
| 3603 | // 109: vrsave |
| 3604 | // 110: vscr |
| 3605 | // 111: spe_acc |
| 3606 | // 112: spefscr |
| 3607 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3608 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3609 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3610 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3613 | // PowerPC-64 |
| 3614 | |
| 3615 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3616 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3617 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3618 | public: |
| 3619 | enum ABIKind { |
| 3620 | ELFv1 = 0, |
| 3621 | ELFv2 |
| 3622 | }; |
| 3623 | |
| 3624 | private: |
| 3625 | static const unsigned GPRBits = 64; |
| 3626 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3627 | bool HasQPX; |
| 3628 | |
| 3629 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3630 | // will be passed in a QPX register. |
| 3631 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3632 | if (!HasQPX) |
| 3633 | return false; |
| 3634 | |
| 3635 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3636 | unsigned NumElements = VT->getNumElements(); |
| 3637 | if (NumElements == 1) |
| 3638 | return false; |
| 3639 | |
| 3640 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3641 | if (getContext().getTypeSize(Ty) <= 256) |
| 3642 | return true; |
| 3643 | } else if (VT->getElementType()-> |
| 3644 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3645 | if (getContext().getTypeSize(Ty) <= 128) |
| 3646 | return true; |
| 3647 | } |
| 3648 | } |
| 3649 | |
| 3650 | return false; |
| 3651 | } |
| 3652 | |
| 3653 | bool IsQPXVectorTy(QualType Ty) const { |
| 3654 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3655 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3656 | |
| 3657 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3658 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
| 3659 | : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3660 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3661 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3662 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3663 | |
| 3664 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3665 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3666 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3667 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3668 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3669 | uint64_t Members) const override; |
| 3670 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3671 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3672 | // Example: For aggregate arguments that fit in a register, we could |
| 3673 | // use getDirectInReg (as is done below for structs containing a single |
| 3674 | // floating-point value) to avoid pushing them to memory on function |
| 3675 | // entry. This would require changing the logic in PPCISelLowering |
| 3676 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3677 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3678 | if (!getCXXABI().classifyReturnType(FI)) |
| 3679 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3680 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3681 | // We rely on the default argument classification for the most part. |
| 3682 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3683 | // 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] | 3684 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3685 | if (T) { |
| 3686 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3687 | if (IsQPXVectorTy(T) || |
| 3688 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3689 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3690 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3691 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3692 | continue; |
| 3693 | } |
| 3694 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3695 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3696 | } |
| 3697 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3698 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3699 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3700 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3701 | }; |
| 3702 | |
| 3703 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3704 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3705 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3706 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3707 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 3708 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3709 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3710 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3711 | // This is recovered from gcc output. |
| 3712 | return 1; // r1 is the dedicated stack pointer |
| 3713 | } |
| 3714 | |
| 3715 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3716 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3717 | }; |
| 3718 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3719 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3720 | public: |
| 3721 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3722 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3723 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3724 | // This is recovered from gcc output. |
| 3725 | return 1; // r1 is the dedicated stack pointer |
| 3726 | } |
| 3727 | |
| 3728 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3729 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3730 | }; |
| 3731 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3732 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3733 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3734 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3735 | // extended to 64 bits. |
| 3736 | bool |
| 3737 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3738 | // Treat an enum type as its underlying type. |
| 3739 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3740 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3741 | |
| 3742 | // Promotable integer types are required to be promoted by the ABI. |
| 3743 | if (Ty->isPromotableIntegerType()) |
| 3744 | return true; |
| 3745 | |
| 3746 | // In addition to the usual promotable integer types, we also need to |
| 3747 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3748 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3749 | switch (BT->getKind()) { |
| 3750 | case BuiltinType::Int: |
| 3751 | case BuiltinType::UInt: |
| 3752 | return true; |
| 3753 | default: |
| 3754 | break; |
| 3755 | } |
| 3756 | |
| 3757 | return false; |
| 3758 | } |
| 3759 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3760 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 3761 | /// higher alignment in the parameter area. Always returns at least 8. |
| 3762 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3763 | // Complex types are passed just like their elements. |
| 3764 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3765 | Ty = CTy->getElementType(); |
| 3766 | |
| 3767 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3768 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3769 | if (IsQPXVectorTy(Ty)) { |
| 3770 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3771 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3772 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3773 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3774 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3775 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3776 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3777 | |
| 3778 | // For single-element float/vector structs, we consider the whole type |
| 3779 | // to have the same alignment requirements as its single element. |
| 3780 | const Type *AlignAsType = nullptr; |
| 3781 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3782 | if (EltType) { |
| 3783 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3784 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3785 | getContext().getTypeSize(EltType) == 128) || |
| 3786 | (BT && BT->isFloatingPoint())) |
| 3787 | AlignAsType = EltType; |
| 3788 | } |
| 3789 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3790 | // Likewise for ELFv2 homogeneous aggregates. |
| 3791 | const Type *Base = nullptr; |
| 3792 | uint64_t Members = 0; |
| 3793 | if (!AlignAsType && Kind == ELFv2 && |
| 3794 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3795 | AlignAsType = Base; |
| 3796 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3797 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3798 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 3799 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3800 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3801 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3802 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3803 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3804 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3805 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3806 | |
| 3807 | // Otherwise, we only need alignment for any aggregate type that |
| 3808 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3809 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 3810 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3811 | return CharUnits::fromQuantity(32); |
| 3812 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3813 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3814 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3815 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3818 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3819 | /// aggregate. Base is set to the base element type, and Members is set |
| 3820 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3821 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3822 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3823 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3824 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3825 | if (NElements == 0) |
| 3826 | return false; |
| 3827 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3828 | return false; |
| 3829 | Members *= NElements; |
| 3830 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3831 | const RecordDecl *RD = RT->getDecl(); |
| 3832 | if (RD->hasFlexibleArrayMember()) |
| 3833 | return false; |
| 3834 | |
| 3835 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 3836 | |
| 3837 | // If this is a C++ record, check the bases first. |
| 3838 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 3839 | for (const auto &I : CXXRD->bases()) { |
| 3840 | // Ignore empty records. |
| 3841 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 3842 | continue; |
| 3843 | |
| 3844 | uint64_t FldMembers; |
| 3845 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 3846 | return false; |
| 3847 | |
| 3848 | Members += FldMembers; |
| 3849 | } |
| 3850 | } |
| 3851 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3852 | for (const auto *FD : RD->fields()) { |
| 3853 | // Ignore (non-zero arrays of) empty records. |
| 3854 | QualType FT = FD->getType(); |
| 3855 | while (const ConstantArrayType *AT = |
| 3856 | getContext().getAsConstantArrayType(FT)) { |
| 3857 | if (AT->getSize().getZExtValue() == 0) |
| 3858 | return false; |
| 3859 | FT = AT->getElementType(); |
| 3860 | } |
| 3861 | if (isEmptyRecord(getContext(), FT, true)) |
| 3862 | continue; |
| 3863 | |
| 3864 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3865 | if (getContext().getLangOpts().CPlusPlus && |
| 3866 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3867 | continue; |
| 3868 | |
| 3869 | uint64_t FldMembers; |
| 3870 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3871 | return false; |
| 3872 | |
| 3873 | Members = (RD->isUnion() ? |
| 3874 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3875 | } |
| 3876 | |
| 3877 | if (!Base) |
| 3878 | return false; |
| 3879 | |
| 3880 | // Ensure there is no padding. |
| 3881 | if (getContext().getTypeSize(Base) * Members != |
| 3882 | getContext().getTypeSize(Ty)) |
| 3883 | return false; |
| 3884 | } else { |
| 3885 | Members = 1; |
| 3886 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3887 | Members = 2; |
| 3888 | Ty = CT->getElementType(); |
| 3889 | } |
| 3890 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3891 | // Most ABIs only support float, double, and some vector type widths. |
| 3892 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3893 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3894 | |
| 3895 | // The base type must be the same for all members. Types that |
| 3896 | // agree in both total size and mode (float vs. vector) are |
| 3897 | // treated as being equivalent here. |
| 3898 | const Type *TyPtr = Ty.getTypePtr(); |
| 3899 | if (!Base) |
| 3900 | Base = TyPtr; |
| 3901 | |
| 3902 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3903 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3904 | return false; |
| 3905 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3906 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 3907 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3908 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3909 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 3910 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3911 | // double, long double, or 128-bit vectors. |
| 3912 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3913 | if (BT->getKind() == BuiltinType::Float || |
| 3914 | BT->getKind() == BuiltinType::Double || |
| 3915 | BT->getKind() == BuiltinType::LongDouble) |
| 3916 | return true; |
| 3917 | } |
| 3918 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3919 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3920 | return true; |
| 3921 | } |
| 3922 | return false; |
| 3923 | } |
| 3924 | |
| 3925 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 3926 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3927 | // Vector types require one register, floating point types require one |
| 3928 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3929 | uint32_t NumRegs = |
| 3930 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3931 | |
| 3932 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3933 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3936 | ABIArgInfo |
| 3937 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3938 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3939 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3940 | if (Ty->isAnyComplexType()) |
| 3941 | return ABIArgInfo::getDirect(); |
| 3942 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3943 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 3944 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3945 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3946 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3947 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3948 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3949 | else if (Size < 128) { |
| 3950 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3951 | return ABIArgInfo::getDirect(CoerceTy); |
| 3952 | } |
| 3953 | } |
| 3954 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3955 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3956 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3957 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3958 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3959 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 3960 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3961 | |
| 3962 | // ELFv2 homogeneous aggregates are passed as array types. |
| 3963 | const Type *Base = nullptr; |
| 3964 | uint64_t Members = 0; |
| 3965 | if (Kind == ELFv2 && |
| 3966 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 3967 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3968 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3969 | return ABIArgInfo::getDirect(CoerceTy); |
| 3970 | } |
| 3971 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 3972 | // If an aggregate may end up fully in registers, we do not |
| 3973 | // use the ByVal method, but pass the aggregate as array. |
| 3974 | // This is usually beneficial since we avoid forcing the |
| 3975 | // back-end to store the argument to memory. |
| 3976 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 3977 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 3978 | llvm::Type *CoerceTy; |
| 3979 | |
| 3980 | // Types up to 8 bytes are passed as integer type (which will be |
| 3981 | // properly aligned in the argument save area doubleword). |
| 3982 | if (Bits <= GPRBits) |
| 3983 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3984 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3985 | // Larger types are passed as arrays, with the base type selected |
| 3986 | // according to the required alignment in the save area. |
| 3987 | else { |
| 3988 | uint64_t RegBits = ABIAlign * 8; |
| 3989 | uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; |
| 3990 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 3991 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 3992 | } |
| 3993 | |
| 3994 | return ABIArgInfo::getDirect(CoerceTy); |
| 3995 | } |
| 3996 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3997 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3998 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 3999 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4000 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | return (isPromotableTypeForABI(Ty) ? |
| 4004 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4005 | } |
| 4006 | |
| 4007 | ABIArgInfo |
| 4008 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4009 | if (RetTy->isVoidType()) |
| 4010 | return ABIArgInfo::getIgnore(); |
| 4011 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4012 | if (RetTy->isAnyComplexType()) |
| 4013 | return ABIArgInfo::getDirect(); |
| 4014 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4015 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4016 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4017 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4018 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4019 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4020 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4021 | else if (Size < 128) { |
| 4022 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4023 | return ABIArgInfo::getDirect(CoerceTy); |
| 4024 | } |
| 4025 | } |
| 4026 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4027 | if (isAggregateTypeForABI(RetTy)) { |
| 4028 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4029 | const Type *Base = nullptr; |
| 4030 | uint64_t Members = 0; |
| 4031 | if (Kind == ELFv2 && |
| 4032 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4033 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4034 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4035 | return ABIArgInfo::getDirect(CoerceTy); |
| 4036 | } |
| 4037 | |
| 4038 | // ELFv2 small aggregates are returned in up to two registers. |
| 4039 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4040 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4041 | if (Bits == 0) |
| 4042 | return ABIArgInfo::getIgnore(); |
| 4043 | |
| 4044 | llvm::Type *CoerceTy; |
| 4045 | if (Bits > GPRBits) { |
| 4046 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 4047 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4048 | } else |
| 4049 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 4050 | llvm::RoundUpToAlignment(Bits, 8)); |
| 4051 | return ABIArgInfo::getDirect(CoerceTy); |
| 4052 | } |
| 4053 | |
| 4054 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4055 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4056 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4057 | |
| 4058 | return (isPromotableTypeForABI(RetTy) ? |
| 4059 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4060 | } |
| 4061 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4062 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4063 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4064 | QualType Ty) const { |
| 4065 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4066 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4067 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4068 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4069 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4070 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4071 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4072 | // in separate doublewords. However, Clang expects us to produce a |
| 4073 | // pointer to a structure with the two parts packed tightly. So generate |
| 4074 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4075 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4076 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4077 | CharUnits EltSize = TypeInfo.first / 2; |
| 4078 | if (EltSize < SlotSize) { |
| 4079 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4080 | SlotSize * 2, SlotSize, |
| 4081 | SlotSize, /*AllowHigher*/ true); |
| 4082 | |
| 4083 | Address RealAddr = Addr; |
| 4084 | Address ImagAddr = RealAddr; |
| 4085 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4086 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4087 | SlotSize - EltSize); |
| 4088 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4089 | 2 * SlotSize - EltSize); |
| 4090 | } else { |
| 4091 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4092 | } |
| 4093 | |
| 4094 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4095 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4096 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4097 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4098 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4099 | |
| 4100 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4101 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4102 | /*init*/ true); |
| 4103 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4104 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4107 | // Otherwise, just use the general rule. |
| 4108 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4109 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | static bool |
| 4113 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4114 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4115 | // This is calculated from the LLVM and GCC tables and verified |
| 4116 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4117 | |
| 4118 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4119 | |
| 4120 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4121 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4122 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4123 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4124 | |
| 4125 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4126 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4127 | |
| 4128 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4129 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4130 | |
| 4131 | // 64-76 are various 4-byte special-purpose registers: |
| 4132 | // 64: mq |
| 4133 | // 65: lr |
| 4134 | // 66: ctr |
| 4135 | // 67: ap |
| 4136 | // 68-75 cr0-7 |
| 4137 | // 76: xer |
| 4138 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 4139 | |
| 4140 | // 77-108: v0-31, the 16-byte vector registers |
| 4141 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4142 | |
| 4143 | // 109: vrsave |
| 4144 | // 110: vscr |
| 4145 | // 111: spe_acc |
| 4146 | // 112: spefscr |
| 4147 | // 113: sfp |
| 4148 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 4149 | |
| 4150 | return false; |
| 4151 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4152 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4153 | bool |
| 4154 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4155 | CodeGen::CodeGenFunction &CGF, |
| 4156 | llvm::Value *Address) const { |
| 4157 | |
| 4158 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4159 | } |
| 4160 | |
| 4161 | bool |
| 4162 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4163 | llvm::Value *Address) const { |
| 4164 | |
| 4165 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4166 | } |
| 4167 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4168 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4169 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4170 | //===----------------------------------------------------------------------===// |
| 4171 | |
| 4172 | namespace { |
| 4173 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4174 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4175 | public: |
| 4176 | enum ABIKind { |
| 4177 | AAPCS = 0, |
| 4178 | DarwinPCS |
| 4179 | }; |
| 4180 | |
| 4181 | private: |
| 4182 | ABIKind Kind; |
| 4183 | |
| 4184 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4185 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4186 | |
| 4187 | private: |
| 4188 | ABIKind getABIKind() const { return Kind; } |
| 4189 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4190 | |
| 4191 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4192 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4193 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4194 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4195 | uint64_t Members) const override; |
| 4196 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4197 | bool isIllegalVectorType(QualType Ty) const; |
| 4198 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4199 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4200 | if (!getCXXABI().classifyReturnType(FI)) |
| 4201 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4202 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4203 | for (auto &it : FI.arguments()) |
| 4204 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4207 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4208 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4209 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4210 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4211 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4212 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4213 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4214 | QualType Ty) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4215 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4216 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 4217 | } |
| 4218 | }; |
| 4219 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4220 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4221 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4222 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4223 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4224 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4225 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4226 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 4227 | } |
| 4228 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4229 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4230 | return 31; |
| 4231 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4232 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4233 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4234 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4235 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4236 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4237 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4238 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4239 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4240 | // Handle illegal vector types here. |
| 4241 | if (isIllegalVectorType(Ty)) { |
| 4242 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4243 | if (Size <= 32) { |
| 4244 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4245 | return ABIArgInfo::getDirect(ResType); |
| 4246 | } |
| 4247 | if (Size == 64) { |
| 4248 | llvm::Type *ResType = |
| 4249 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4250 | return ABIArgInfo::getDirect(ResType); |
| 4251 | } |
| 4252 | if (Size == 128) { |
| 4253 | llvm::Type *ResType = |
| 4254 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4255 | return ABIArgInfo::getDirect(ResType); |
| 4256 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4257 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4258 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4259 | |
| 4260 | if (!isAggregateTypeForABI(Ty)) { |
| 4261 | // Treat an enum type as its underlying type. |
| 4262 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4263 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4264 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4265 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4266 | ? ABIArgInfo::getExtend() |
| 4267 | : ABIArgInfo::getDirect()); |
| 4268 | } |
| 4269 | |
| 4270 | // Structures with either a non-trivial destructor or a non-trivial |
| 4271 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4272 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4273 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4274 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4278 | // elsewhere for GNU compatibility. |
| 4279 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4280 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4281 | return ABIArgInfo::getIgnore(); |
| 4282 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4283 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4284 | } |
| 4285 | |
| 4286 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4287 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4288 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4289 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4290 | return ABIArgInfo::getDirect( |
| 4291 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4295 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4296 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4297 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4298 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4299 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4300 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4301 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4302 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4303 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4304 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4305 | } |
| 4306 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4307 | } |
| 4308 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4309 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4312 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4313 | if (RetTy->isVoidType()) |
| 4314 | return ABIArgInfo::getIgnore(); |
| 4315 | |
| 4316 | // Large vector types should be returned via memory. |
| 4317 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4318 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4319 | |
| 4320 | if (!isAggregateTypeForABI(RetTy)) { |
| 4321 | // Treat an enum type as its underlying type. |
| 4322 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4323 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4324 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4325 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4326 | ? ABIArgInfo::getExtend() |
| 4327 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4330 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4331 | return ABIArgInfo::getIgnore(); |
| 4332 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4333 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4334 | uint64_t Members = 0; |
| 4335 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4336 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4337 | return ABIArgInfo::getDirect(); |
| 4338 | |
| 4339 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4340 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4341 | if (Size <= 128) { |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4342 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4343 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4344 | |
| 4345 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4346 | // For aggregates with 16-byte alignment, we use i128. |
| 4347 | if (Alignment < 128 && Size == 128) { |
| 4348 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4349 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4350 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4351 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4352 | } |
| 4353 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4354 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4355 | } |
| 4356 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4357 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4358 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4359 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4360 | // Check whether VT is legal. |
| 4361 | unsigned NumElements = VT->getNumElements(); |
| 4362 | uint64_t Size = getContext().getTypeSize(VT); |
| 4363 | // NumElements should be power of 2 between 1 and 16. |
| 4364 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 4365 | return true; |
| 4366 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4367 | } |
| 4368 | return false; |
| 4369 | } |
| 4370 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4371 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4372 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4373 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4374 | // but with the difference that any floating-point type is allowed, |
| 4375 | // including __fp16. |
| 4376 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4377 | if (BT->isFloatingPoint()) |
| 4378 | return true; |
| 4379 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4380 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4381 | if (VecSize == 64 || VecSize == 128) |
| 4382 | return true; |
| 4383 | } |
| 4384 | return false; |
| 4385 | } |
| 4386 | |
| 4387 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4388 | uint64_t Members) const { |
| 4389 | return Members <= 4; |
| 4390 | } |
| 4391 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4392 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4393 | QualType Ty, |
| 4394 | CodeGenFunction &CGF) const { |
| 4395 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4396 | bool IsIndirect = AI.isIndirect(); |
| 4397 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4398 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4399 | if (IsIndirect) |
| 4400 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4401 | else if (AI.getCoerceToType()) |
| 4402 | BaseTy = AI.getCoerceToType(); |
| 4403 | |
| 4404 | unsigned NumRegs = 1; |
| 4405 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4406 | BaseTy = ArrTy->getElementType(); |
| 4407 | NumRegs = ArrTy->getNumElements(); |
| 4408 | } |
| 4409 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4410 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4411 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4412 | // Standard, section B.4: |
| 4413 | // |
| 4414 | // struct { |
| 4415 | // void *__stack; |
| 4416 | // void *__gr_top; |
| 4417 | // void *__vr_top; |
| 4418 | // int __gr_offs; |
| 4419 | // int __vr_offs; |
| 4420 | // }; |
| 4421 | |
| 4422 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4423 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4424 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4425 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4426 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4427 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4428 | CharUnits TyAlign = TyInfo.second; |
| 4429 | |
| 4430 | Address reg_offs_p = Address::invalid(); |
| 4431 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4432 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4433 | CharUnits reg_top_offset; |
| 4434 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4435 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4436 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4437 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4438 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 4439 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4440 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4441 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4442 | reg_top_offset = CharUnits::fromQuantity(8); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4443 | RegSize = llvm::RoundUpToAlignment(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4444 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4445 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4446 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4447 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 4448 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4449 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4450 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4451 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4452 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
| 4455 | //======================================= |
| 4456 | // Find out where argument was passed |
| 4457 | //======================================= |
| 4458 | |
| 4459 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4460 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4461 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4462 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4463 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4464 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4465 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4466 | |
| 4467 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4468 | |
| 4469 | // 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] | 4470 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4471 | CGF.EmitBlock(MaybeRegBlock); |
| 4472 | |
| 4473 | // Integer arguments may need to correct register alignment (for example a |
| 4474 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4475 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4476 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 4477 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4478 | |
| 4479 | reg_offs = CGF.Builder.CreateAdd( |
| 4480 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4481 | "align_regoffs"); |
| 4482 | reg_offs = CGF.Builder.CreateAnd( |
| 4483 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4484 | "aligned_regoffs"); |
| 4485 | } |
| 4486 | |
| 4487 | // 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] | 4488 | // The fact that this is done unconditionally reflects the fact that |
| 4489 | // allocating an argument to the stack also uses up all the remaining |
| 4490 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4491 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4492 | NewOffset = CGF.Builder.CreateAdd( |
| 4493 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4494 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4495 | |
| 4496 | // Now we're in a position to decide whether this argument really was in |
| 4497 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4498 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4499 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4500 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4501 | |
| 4502 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4503 | |
| 4504 | //======================================= |
| 4505 | // Argument was in registers |
| 4506 | //======================================= |
| 4507 | |
| 4508 | // Now we emit the code for if the argument was originally passed in |
| 4509 | // registers. First start the appropriate block: |
| 4510 | CGF.EmitBlock(InRegBlock); |
| 4511 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4512 | llvm::Value *reg_top = nullptr; |
| 4513 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 4514 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4515 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4516 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 4517 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 4518 | Address RegAddr = Address::invalid(); |
| 4519 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4520 | |
| 4521 | if (IsIndirect) { |
| 4522 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4523 | // stored registers or on the stack will actually be a struct **. |
| 4524 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4525 | } |
| 4526 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4527 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4528 | uint64_t NumMembers = 0; |
| 4529 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4530 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4531 | // Homogeneous aggregates passed in registers will have their elements split |
| 4532 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4533 | // qN+1, ...). We reload and store into a temporary local variable |
| 4534 | // contiguously. |
| 4535 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4536 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4537 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4538 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4539 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 4540 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4541 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4542 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 4543 | int Offset = 0; |
| 4544 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4545 | BaseTyInfo.first.getQuantity() < 16) |
| 4546 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 4547 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4548 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4549 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 4550 | Address LoadAddr = |
| 4551 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 4552 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 4553 | |
| 4554 | Address StoreAddr = |
| 4555 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4556 | |
| 4557 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4558 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4559 | } |
| 4560 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4561 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4562 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4563 | // Otherwise the object is contiguous in memory. |
| 4564 | |
| 4565 | // It might be right-aligned in its slot. |
| 4566 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 4567 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4568 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4569 | TyInfo.first < SlotSize) { |
| 4570 | CharUnits Offset = SlotSize - TyInfo.first; |
| 4571 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4574 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | CGF.EmitBranch(ContBlock); |
| 4578 | |
| 4579 | //======================================= |
| 4580 | // Argument was on the stack |
| 4581 | //======================================= |
| 4582 | CGF.EmitBlock(OnStackBlock); |
| 4583 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4584 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 4585 | CharUnits::Zero(), "stack_p"); |
| 4586 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4587 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4588 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4589 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4590 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 4591 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4592 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4593 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4594 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4595 | OnStackPtr = CGF.Builder.CreateAdd( |
| 4596 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4597 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4598 | OnStackPtr = CGF.Builder.CreateAnd( |
| 4599 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4600 | "align_stack"); |
| 4601 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4602 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4603 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4604 | Address OnStackAddr(OnStackPtr, |
| 4605 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4606 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4607 | // All stack slots are multiples of 8 bytes. |
| 4608 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 4609 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4610 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4611 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4612 | else |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4613 | StackSize = TyInfo.first.RoundUpToAlignment(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4614 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4615 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4616 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4617 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4618 | |
| 4619 | // Write the new value of __stack for the next call to va_arg |
| 4620 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4621 | |
| 4622 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4623 | TyInfo.first < StackSlotSize) { |
| 4624 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 4625 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4628 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4629 | |
| 4630 | CGF.EmitBranch(ContBlock); |
| 4631 | |
| 4632 | //======================================= |
| 4633 | // Tidy up |
| 4634 | //======================================= |
| 4635 | CGF.EmitBlock(ContBlock); |
| 4636 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4637 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 4638 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4639 | |
| 4640 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4641 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 4642 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4643 | |
| 4644 | return ResAddr; |
| 4645 | } |
| 4646 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4647 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4648 | CodeGenFunction &CGF) const { |
| 4649 | // The backend's lowering doesn't support va_arg for aggregates or |
| 4650 | // illegal vector types. Lower VAArg here for these cases and use |
| 4651 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4652 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4653 | return Address::invalid(); |
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 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4656 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4657 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4658 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4659 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 4660 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 4661 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4664 | // The size of the actual thing passed, which might end up just |
| 4665 | // being a pointer for indirect types. |
| 4666 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4667 | |
| 4668 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 4669 | // aggregates should be passed indirectly. |
| 4670 | bool IsIndirect = false; |
| 4671 | if (TyInfo.first.getQuantity() > 16) { |
| 4672 | const Type *Base = nullptr; |
| 4673 | uint64_t Members = 0; |
| 4674 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4677 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 4678 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4682 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4683 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4684 | |
| 4685 | namespace { |
| 4686 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4687 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4688 | public: |
| 4689 | enum ABIKind { |
| 4690 | APCS = 0, |
| 4691 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4692 | AAPCS_VFP = 2, |
| 4693 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4694 | }; |
| 4695 | |
| 4696 | private: |
| 4697 | ABIKind Kind; |
| 4698 | |
| 4699 | public: |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4700 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4701 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4702 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4703 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4704 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4705 | switch (getTarget().getTriple().getEnvironment()) { |
| 4706 | case llvm::Triple::Android: |
| 4707 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4708 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4709 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4710 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4711 | return true; |
| 4712 | default: |
| 4713 | return false; |
| 4714 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4715 | } |
| 4716 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4717 | bool isEABIHF() const { |
| 4718 | switch (getTarget().getTriple().getEnvironment()) { |
| 4719 | case llvm::Triple::EABIHF: |
| 4720 | case llvm::Triple::GNUEABIHF: |
| 4721 | return true; |
| 4722 | default: |
| 4723 | return false; |
| 4724 | } |
| 4725 | } |
| 4726 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4727 | ABIKind getABIKind() const { return Kind; } |
| 4728 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4729 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4730 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4731 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4732 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4733 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4734 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4735 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4736 | uint64_t Members) const override; |
| 4737 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4738 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4739 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4740 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4741 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4742 | |
| 4743 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4744 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4745 | void setCCs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4746 | }; |
| 4747 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4748 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4749 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4750 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4751 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4752 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4753 | const ARMABIInfo &getABIInfo() const { |
| 4754 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4755 | } |
| 4756 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4757 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4758 | return 13; |
| 4759 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4760 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4761 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4762 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4763 | } |
| 4764 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4765 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4766 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4767 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4768 | |
| 4769 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4770 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4771 | return false; |
| 4772 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4773 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4774 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4775 | if (getABIInfo().isEABI()) return 88; |
| 4776 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4777 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4778 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4779 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4780 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 4781 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4782 | if (!FD) |
| 4783 | return; |
| 4784 | |
| 4785 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4786 | if (!Attr) |
| 4787 | return; |
| 4788 | |
| 4789 | const char *Kind; |
| 4790 | switch (Attr->getInterrupt()) { |
| 4791 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4792 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4793 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4794 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4795 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4796 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4797 | } |
| 4798 | |
| 4799 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4800 | |
| 4801 | Fn->addFnAttr("interrupt", Kind); |
| 4802 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4803 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 4804 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4805 | return; |
| 4806 | |
| 4807 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4808 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4809 | // the backend to perform a realignment as part of the function prologue. |
| 4810 | llvm::AttrBuilder B; |
| 4811 | B.addStackAlignmentAttr(8); |
| 4812 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4813 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4814 | llvm::AttributeSet::FunctionIndex, |
| 4815 | B)); |
| 4816 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4817 | }; |
| 4818 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4819 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
| 4820 | void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV, |
| 4821 | CodeGen::CodeGenModule &CGM) const; |
| 4822 | |
| 4823 | public: |
| 4824 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4825 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 4826 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4827 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4828 | CodeGen::CodeGenModule &CGM) const override; |
| 4829 | }; |
| 4830 | |
| 4831 | void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute( |
| 4832 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4833 | if (!isa<FunctionDecl>(D)) |
| 4834 | return; |
| 4835 | if (CGM.getCodeGenOpts().StackProbeSize == 4096) |
| 4836 | return; |
| 4837 | |
| 4838 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4839 | F->addFnAttr("stack-probe-size", |
| 4840 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 4841 | } |
| 4842 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4843 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4844 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4845 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4846 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 4847 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4848 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4849 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4850 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4851 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4852 | FI.getReturnInfo() = |
| 4853 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4854 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4855 | for (auto &I : FI.arguments()) |
| 4856 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4857 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4858 | // Always honor user-specified calling convention. |
| 4859 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4860 | return; |
| 4861 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4862 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4863 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4864 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4865 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4866 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4867 | /// Return the default calling convention that LLVM will use. |
| 4868 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4869 | // The default calling convention that LLVM will infer. |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4870 | if (isEABIHF() || getTarget().getTriple().isWatchOS()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4871 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4872 | else if (isEABI()) |
| 4873 | return llvm::CallingConv::ARM_AAPCS; |
| 4874 | else |
| 4875 | return llvm::CallingConv::ARM_APCS; |
| 4876 | } |
| 4877 | |
| 4878 | /// Return the calling convention that our ABI would like us to use |
| 4879 | /// as the C calling convention. |
| 4880 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4881 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4882 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4883 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4884 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4885 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4886 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4887 | llvm_unreachable("bad ABI kind"); |
| 4888 | } |
| 4889 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4890 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4891 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4892 | |
| 4893 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4894 | // they'd just match what LLVM will infer from the triple. |
| 4895 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4896 | if (abiCC != getLLVMDefaultCC()) |
| 4897 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4898 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4899 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 4900 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 4901 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
| 4902 | switch (getABIKind()) { |
| 4903 | case APCS: |
| 4904 | case AAPCS16_VFP: |
| 4905 | if (abiCC != getLLVMDefaultCC()) |
| 4906 | BuiltinCC = abiCC; |
| 4907 | break; |
| 4908 | case AAPCS: |
| 4909 | case AAPCS_VFP: |
| 4910 | BuiltinCC = llvm::CallingConv::ARM_AAPCS; |
| 4911 | break; |
| 4912 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4913 | } |
| 4914 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4915 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 4916 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4917 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4918 | // A single-precision floating-point type (including promoted |
| 4919 | // half-precision types); A double-precision floating-point type; |
| 4920 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4921 | // with a Base Type of a single- or double-precision floating-point type, |
| 4922 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4923 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4924 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4925 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4926 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4927 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4928 | // Handle illegal vector types here. |
| 4929 | if (isIllegalVectorType(Ty)) { |
| 4930 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4931 | if (Size <= 32) { |
| 4932 | llvm::Type *ResType = |
| 4933 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4934 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4935 | } |
| 4936 | if (Size == 64) { |
| 4937 | llvm::Type *ResType = llvm::VectorType::get( |
| 4938 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4939 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4940 | } |
| 4941 | if (Size == 128) { |
| 4942 | llvm::Type *ResType = llvm::VectorType::get( |
| 4943 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4944 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4945 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4946 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 4949 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 4950 | // unspecified. This is not done for OpenCL as it handles the half type |
| 4951 | // natively, and does not need to interwork with AAPCS code. |
| 4952 | if (Ty->isHalfType() && !getContext().getLangOpts().OpenCL) { |
| 4953 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 4954 | llvm::Type::getFloatTy(getVMContext()) : |
| 4955 | llvm::Type::getInt32Ty(getVMContext()); |
| 4956 | return ABIArgInfo::getDirect(ResType); |
| 4957 | } |
| 4958 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4959 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4960 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4961 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4962 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4963 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4964 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4965 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4966 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4967 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4968 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4969 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4970 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4971 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4972 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4973 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4974 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4975 | return ABIArgInfo::getIgnore(); |
| 4976 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4977 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4978 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4979 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4980 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4981 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4982 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4983 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4984 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4985 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4986 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4987 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 4988 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 4989 | // this convention even for a variadic function: the backend will use GPRs |
| 4990 | // if needed. |
| 4991 | const Type *Base = nullptr; |
| 4992 | uint64_t Members = 0; |
| 4993 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 4994 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 4995 | llvm::Type *Ty = |
| 4996 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 4997 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 4998 | } |
| 4999 | } |
| 5000 | |
| 5001 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5002 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5003 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5004 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5005 | // and a pointer is passed. |
| 5006 | return ABIArgInfo::getIndirect( |
| 5007 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5010 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5011 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5012 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5013 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5014 | uint64_t ABIAlign = 4; |
| 5015 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5016 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5017 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5018 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5019 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5020 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5021 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5022 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5023 | /*ByVal=*/true, |
| 5024 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5027 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5028 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5029 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5030 | // FIXME: Try to match the types of the arguments more accurately where |
| 5031 | // we can. |
| 5032 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5033 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5034 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5035 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5036 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5037 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5038 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5039 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5040 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5041 | } |
| 5042 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5043 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5044 | llvm::LLVMContext &VMContext) { |
| 5045 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5046 | // is called integer-like if its size is less than or equal to one word, and |
| 5047 | // the offset of each of its addressable sub-fields is zero. |
| 5048 | |
| 5049 | uint64_t Size = Context.getTypeSize(Ty); |
| 5050 | |
| 5051 | // Check that the type fits in a word. |
| 5052 | if (Size > 32) |
| 5053 | return false; |
| 5054 | |
| 5055 | // FIXME: Handle vector types! |
| 5056 | if (Ty->isVectorType()) |
| 5057 | return false; |
| 5058 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5059 | // Float types are never treated as "integer like". |
| 5060 | if (Ty->isRealFloatingType()) |
| 5061 | return false; |
| 5062 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5063 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5064 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5065 | return true; |
| 5066 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5067 | // Small complex integer types are "integer like". |
| 5068 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5069 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5070 | |
| 5071 | // Single element and zero sized arrays should be allowed, by the definition |
| 5072 | // above, but they are not. |
| 5073 | |
| 5074 | // Otherwise, it must be a record type. |
| 5075 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5076 | if (!RT) return false; |
| 5077 | |
| 5078 | // Ignore records with flexible arrays. |
| 5079 | const RecordDecl *RD = RT->getDecl(); |
| 5080 | if (RD->hasFlexibleArrayMember()) |
| 5081 | return false; |
| 5082 | |
| 5083 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5084 | // like". |
| 5085 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5086 | |
| 5087 | bool HadField = false; |
| 5088 | unsigned idx = 0; |
| 5089 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5090 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5091 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5092 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5093 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5094 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5095 | // struct { int : 0; int x } |
| 5096 | // is non-integer like according to gcc. |
| 5097 | if (FD->isBitField()) { |
| 5098 | if (!RD->isUnion()) |
| 5099 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5100 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5101 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5102 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5103 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5104 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5107 | // Check if this field is at offset 0. |
| 5108 | if (Layout.getFieldOffset(idx) != 0) |
| 5109 | return false; |
| 5110 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5111 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5112 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5113 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5114 | // Only allow at most one field in a structure. This doesn't match the |
| 5115 | // wording above, but follows gcc in situations with a field following an |
| 5116 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5117 | if (!RD->isUnion()) { |
| 5118 | if (HadField) |
| 5119 | return false; |
| 5120 | |
| 5121 | HadField = true; |
| 5122 | } |
| 5123 | } |
| 5124 | |
| 5125 | return true; |
| 5126 | } |
| 5127 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5128 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5129 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5130 | bool IsEffectivelyAAPCS_VFP = |
| 5131 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5132 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5133 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5134 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5135 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5136 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5137 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5138 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5139 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5140 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5141 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5142 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5143 | // natively, and does not need to interwork with AAPCS code. |
| 5144 | if (RetTy->isHalfType() && !getContext().getLangOpts().OpenCL) { |
| 5145 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5146 | llvm::Type::getFloatTy(getVMContext()) : |
| 5147 | llvm::Type::getInt32Ty(getVMContext()); |
| 5148 | return ABIArgInfo::getDirect(ResType); |
| 5149 | } |
| 5150 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5151 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5152 | // Treat an enum type as its underlying type. |
| 5153 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5154 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5155 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5156 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5157 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5158 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5159 | |
| 5160 | // Are we following APCS? |
| 5161 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5162 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5163 | return ABIArgInfo::getIgnore(); |
| 5164 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5165 | // Complex types are all returned as packed integers. |
| 5166 | // |
| 5167 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5168 | // correctly. |
| 5169 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5170 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5171 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5172 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5173 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5174 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5175 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5176 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5177 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5178 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5179 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5180 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5181 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5185 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5186 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5187 | |
| 5188 | // Otherwise this is an AAPCS variant. |
| 5189 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5190 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5191 | return ABIArgInfo::getIgnore(); |
| 5192 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5193 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5194 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5195 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5196 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5197 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5198 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5199 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5200 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5201 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5202 | } |
| 5203 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5204 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5205 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5206 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5207 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5208 | if (getDataLayout().isBigEndian()) |
| 5209 | // 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] | 5210 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5211 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5212 | // Return in the smallest viable integer type. |
| 5213 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5214 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5215 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5216 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5217 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5218 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5219 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5220 | llvm::Type *CoerceTy = |
| 5221 | llvm::ArrayType::get(Int32Ty, llvm::RoundUpToAlignment(Size, 32) / 32); |
| 5222 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5223 | } |
| 5224 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5225 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5226 | } |
| 5227 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5228 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5229 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 5230 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5231 | // Check whether VT is legal. |
| 5232 | unsigned NumElements = VT->getNumElements(); |
| 5233 | uint64_t Size = getContext().getTypeSize(VT); |
| 5234 | // NumElements should be power of 2. |
| 5235 | if ((NumElements & (NumElements - 1)) != 0) |
| 5236 | return true; |
| 5237 | // Size should be greater than 32 bits. |
| 5238 | return Size <= 32; |
| 5239 | } |
| 5240 | return false; |
| 5241 | } |
| 5242 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5243 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5244 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 5245 | // double, or 64-bit or 128-bit vectors. |
| 5246 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5247 | if (BT->getKind() == BuiltinType::Float || |
| 5248 | BT->getKind() == BuiltinType::Double || |
| 5249 | BT->getKind() == BuiltinType::LongDouble) |
| 5250 | return true; |
| 5251 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5252 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5253 | if (VecSize == 64 || VecSize == 128) |
| 5254 | return true; |
| 5255 | } |
| 5256 | return false; |
| 5257 | } |
| 5258 | |
| 5259 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5260 | uint64_t Members) const { |
| 5261 | return Members <= 4; |
| 5262 | } |
| 5263 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5264 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5265 | QualType Ty) const { |
| 5266 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5267 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5268 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5269 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5270 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 5271 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5272 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5273 | } |
| 5274 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5275 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5276 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5277 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5278 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 5279 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5280 | const Type *Base = nullptr; |
| 5281 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5282 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 5283 | IsIndirect = true; |
| 5284 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5285 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 5286 | // allocated by the caller. |
| 5287 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 5288 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5289 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 5290 | IsIndirect = true; |
| 5291 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5292 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5293 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 5294 | // 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] | 5295 | // Our callers should be prepared to handle an under-aligned address. |
| 5296 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 5297 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5298 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5299 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 5300 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5301 | // ARMv7k allows type alignment up to 16 bytes. |
| 5302 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5303 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5304 | } else { |
| 5305 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5306 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5307 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5308 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5309 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 5310 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5313 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5314 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5315 | //===----------------------------------------------------------------------===// |
| 5316 | |
| 5317 | namespace { |
| 5318 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5319 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5320 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5321 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5322 | |
| 5323 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5324 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5325 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5326 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5327 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5328 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5329 | }; |
| 5330 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5331 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5332 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5333 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5334 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5335 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5336 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5337 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5338 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5339 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5340 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5341 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5342 | }; |
| 5343 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5344 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5345 | if (RetTy->isVoidType()) |
| 5346 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5347 | |
| 5348 | // note: this is different from default ABI |
| 5349 | if (!RetTy->isScalarType()) |
| 5350 | return ABIArgInfo::getDirect(); |
| 5351 | |
| 5352 | // Treat an enum type as its underlying type. |
| 5353 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5354 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5355 | |
| 5356 | return (RetTy->isPromotableIntegerType() ? |
| 5357 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5358 | } |
| 5359 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5360 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5361 | // Treat an enum type as its underlying type. |
| 5362 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5363 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5364 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5365 | // Return aggregates type as indirect by value |
| 5366 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5367 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5368 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5369 | return (Ty->isPromotableIntegerType() ? |
| 5370 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5371 | } |
| 5372 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5373 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5374 | if (!getCXXABI().classifyReturnType(FI)) |
| 5375 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5376 | for (auto &I : FI.arguments()) |
| 5377 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5378 | |
| 5379 | // Always honor user-specified calling convention. |
| 5380 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5381 | return; |
| 5382 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5383 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5384 | } |
| 5385 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5386 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5387 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5388 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5389 | } |
| 5390 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5391 | void NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5392 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5393 | CodeGen::CodeGenModule &M) const{ |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5394 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5395 | if (!FD) return; |
| 5396 | |
| 5397 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5398 | |
| 5399 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5400 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5401 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5402 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5403 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5404 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5405 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5406 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5407 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5408 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5409 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5410 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5411 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5412 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5413 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5414 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5415 | // __global__ functions cannot be called from the device, we do not |
| 5416 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5417 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5418 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5419 | addNVVMMetadata(F, "kernel", 1); |
| 5420 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5421 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5422 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5423 | llvm::APSInt MaxThreads(32); |
| 5424 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5425 | if (MaxThreads > 0) |
| 5426 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5427 | |
| 5428 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5429 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5430 | // we don't have to add a PTX directive. |
| 5431 | if (Attr->getMinBlocks()) { |
| 5432 | llvm::APSInt MinBlocks(32); |
| 5433 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5434 | if (MinBlocks > 0) |
| 5435 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5436 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5437 | } |
| 5438 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5439 | } |
| 5440 | } |
| 5441 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5442 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5443 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5444 | llvm::Module *M = F->getParent(); |
| 5445 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5446 | |
| 5447 | // Get "nvvm.annotations" metadata node |
| 5448 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5449 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5450 | llvm::Metadata *MDVals[] = { |
| 5451 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5452 | llvm::ConstantAsMetadata::get( |
| 5453 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5454 | // Append metadata to nvvm.annotations |
| 5455 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5456 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5457 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5458 | |
| 5459 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5460 | // SystemZ ABI Implementation |
| 5461 | //===----------------------------------------------------------------------===// |
| 5462 | |
| 5463 | namespace { |
| 5464 | |
| 5465 | class SystemZABIInfo : public ABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5466 | bool HasVector; |
| 5467 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5468 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5469 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
| 5470 | : ABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5471 | |
| 5472 | bool isPromotableIntegerType(QualType Ty) const; |
| 5473 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5474 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5475 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5476 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5477 | |
| 5478 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5479 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5480 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5481 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5482 | if (!getCXXABI().classifyReturnType(FI)) |
| 5483 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5484 | for (auto &I : FI.arguments()) |
| 5485 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5486 | } |
| 5487 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5488 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5489 | QualType Ty) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5490 | }; |
| 5491 | |
| 5492 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5493 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5494 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5495 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5496 | }; |
| 5497 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5498 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5499 | |
| 5500 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5501 | // Treat an enum type as its underlying type. |
| 5502 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5503 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5504 | |
| 5505 | // Promotable integer types are required to be promoted by the ABI. |
| 5506 | if (Ty->isPromotableIntegerType()) |
| 5507 | return true; |
| 5508 | |
| 5509 | // 32-bit values must also be promoted. |
| 5510 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5511 | switch (BT->getKind()) { |
| 5512 | case BuiltinType::Int: |
| 5513 | case BuiltinType::UInt: |
| 5514 | return true; |
| 5515 | default: |
| 5516 | return false; |
| 5517 | } |
| 5518 | return false; |
| 5519 | } |
| 5520 | |
| 5521 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5522 | return (Ty->isAnyComplexType() || |
| 5523 | Ty->isVectorType() || |
| 5524 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5525 | } |
| 5526 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5527 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5528 | return (HasVector && |
| 5529 | Ty->isVectorType() && |
| 5530 | getContext().getTypeSize(Ty) <= 128); |
| 5531 | } |
| 5532 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5533 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5534 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5535 | switch (BT->getKind()) { |
| 5536 | case BuiltinType::Float: |
| 5537 | case BuiltinType::Double: |
| 5538 | return true; |
| 5539 | default: |
| 5540 | return false; |
| 5541 | } |
| 5542 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5543 | return false; |
| 5544 | } |
| 5545 | |
| 5546 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5547 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5548 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5549 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5550 | |
| 5551 | // If this is a C++ record, check the bases first. |
| 5552 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5553 | for (const auto &I : CXXRD->bases()) { |
| 5554 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5555 | |
| 5556 | // Empty bases don't affect things either way. |
| 5557 | if (isEmptyRecord(getContext(), Base, true)) |
| 5558 | continue; |
| 5559 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5560 | if (!Found.isNull()) |
| 5561 | return Ty; |
| 5562 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5563 | } |
| 5564 | |
| 5565 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5566 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5567 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5568 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5569 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5570 | if (getContext().getLangOpts().CPlusPlus && |
| 5571 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5572 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5573 | |
| 5574 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5575 | // Nested structures still do though. |
| 5576 | if (!Found.isNull()) |
| 5577 | return Ty; |
| 5578 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5582 | // 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] | 5583 | if (!Found.isNull()) |
| 5584 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5585 | } |
| 5586 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5587 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5590 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5591 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5592 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5593 | // struct { |
| 5594 | // i64 __gpr; |
| 5595 | // i64 __fpr; |
| 5596 | // i8 *__overflow_arg_area; |
| 5597 | // i8 *__reg_save_area; |
| 5598 | // }; |
| 5599 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5600 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5601 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5602 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5603 | Ty = getContext().getCanonicalType(Ty); |
| 5604 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5605 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5606 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5607 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5608 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5609 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5610 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5611 | CharUnits UnpaddedSize; |
| 5612 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5613 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5614 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 5615 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5616 | } else { |
| 5617 | if (AI.getCoerceToType()) |
| 5618 | ArgTy = AI.getCoerceToType(); |
| 5619 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5620 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5621 | UnpaddedSize = TyInfo.first; |
| 5622 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5623 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5624 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 5625 | if (IsVector && UnpaddedSize > PaddedSize) |
| 5626 | PaddedSize = CharUnits::fromQuantity(16); |
| 5627 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5628 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5629 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5630 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5631 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5632 | llvm::Value *PaddedSizeV = |
| 5633 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5634 | |
| 5635 | if (IsVector) { |
| 5636 | // Work out the address of a vector argument on the stack. |
| 5637 | // Vector arguments are always passed in the high bits of a |
| 5638 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5639 | Address OverflowArgAreaPtr = |
| 5640 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5641 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5642 | Address OverflowArgArea = |
| 5643 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5644 | TyInfo.second); |
| 5645 | Address MemAddr = |
| 5646 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5647 | |
| 5648 | // Update overflow_arg_area_ptr pointer |
| 5649 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5650 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5651 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5652 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5653 | |
| 5654 | return MemAddr; |
| 5655 | } |
| 5656 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5657 | assert(PaddedSize.getQuantity() == 8); |
| 5658 | |
| 5659 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 5660 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5661 | if (InFPRs) { |
| 5662 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5663 | RegCountField = 1; // __fpr |
| 5664 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5665 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5666 | } else { |
| 5667 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5668 | RegCountField = 0; // __gpr |
| 5669 | RegSaveIndex = 2; // save offset for r2 |
| 5670 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5671 | } |
| 5672 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5673 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 5674 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 5675 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5676 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5677 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5678 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5679 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5680 | |
| 5681 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5682 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5683 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5684 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5685 | |
| 5686 | // Emit code to load the value if it was passed in registers. |
| 5687 | CGF.EmitBlock(InRegBlock); |
| 5688 | |
| 5689 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5690 | llvm::Value *ScaledRegCount = |
| 5691 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5692 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5693 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 5694 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5695 | llvm::Value *RegOffset = |
| 5696 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5697 | Address RegSaveAreaPtr = |
| 5698 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5699 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5700 | llvm::Value *RegSaveArea = |
| 5701 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5702 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 5703 | "raw_reg_addr"), |
| 5704 | PaddedSize); |
| 5705 | Address RegAddr = |
| 5706 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5707 | |
| 5708 | // Update the register count |
| 5709 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5710 | llvm::Value *NewRegCount = |
| 5711 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5712 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5713 | CGF.EmitBranch(ContBlock); |
| 5714 | |
| 5715 | // Emit code to load the value if it was passed in memory. |
| 5716 | CGF.EmitBlock(InMemBlock); |
| 5717 | |
| 5718 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5719 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 5720 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 5721 | Address OverflowArgArea = |
| 5722 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5723 | PaddedSize); |
| 5724 | Address RawMemAddr = |
| 5725 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 5726 | Address MemAddr = |
| 5727 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5728 | |
| 5729 | // Update overflow_arg_area_ptr pointer |
| 5730 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5731 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5732 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5733 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5734 | CGF.EmitBranch(ContBlock); |
| 5735 | |
| 5736 | // Return the appropriate result. |
| 5737 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5738 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5739 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5740 | |
| 5741 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5742 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 5743 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5744 | |
| 5745 | return ResAddr; |
| 5746 | } |
| 5747 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5748 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5749 | if (RetTy->isVoidType()) |
| 5750 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5751 | if (isVectorArgumentType(RetTy)) |
| 5752 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5753 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5754 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5755 | return (isPromotableIntegerType(RetTy) ? |
| 5756 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5757 | } |
| 5758 | |
| 5759 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5760 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5761 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5762 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5763 | |
| 5764 | // Integers and enums are extended to full register width. |
| 5765 | if (isPromotableIntegerType(Ty)) |
| 5766 | return ABIArgInfo::getExtend(); |
| 5767 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5768 | // Handle vector types and vector-like structure types. Note that |
| 5769 | // as opposed to float-like structure types, we do not allow any |
| 5770 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5771 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5772 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 5773 | if (isVectorArgumentType(SingleElementTy) && |
| 5774 | getContext().getTypeSize(SingleElementTy) == Size) |
| 5775 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 5776 | |
| 5777 | // 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] | 5778 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5779 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5780 | |
| 5781 | // Handle small structures. |
| 5782 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5783 | // Structures with flexible arrays have variable length, so really |
| 5784 | // fail the size test above. |
| 5785 | const RecordDecl *RD = RT->getDecl(); |
| 5786 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5787 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5788 | |
| 5789 | // The structure is passed as an unextended integer, a float, or a double. |
| 5790 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5791 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5792 | assert(Size == 32 || Size == 64); |
| 5793 | if (Size == 32) |
| 5794 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5795 | else |
| 5796 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5797 | } else |
| 5798 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5799 | return ABIArgInfo::getDirect(PassTy); |
| 5800 | } |
| 5801 | |
| 5802 | // Non-structure compounds are passed indirectly. |
| 5803 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5804 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5805 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5806 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
| 5809 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5810 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5811 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5812 | |
| 5813 | namespace { |
| 5814 | |
| 5815 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5816 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5817 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5818 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5819 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5820 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5821 | }; |
| 5822 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5823 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5824 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5825 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5826 | llvm::GlobalValue *GV, |
| 5827 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5828 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5829 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5830 | // Handle 'interrupt' attribute: |
| 5831 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5832 | |
| 5833 | // Step 1: Set ISR calling convention. |
| 5834 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5835 | |
| 5836 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5837 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5838 | |
| 5839 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5840 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5841 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5842 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5843 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5844 | } |
| 5845 | } |
| 5846 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5847 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5848 | // MIPS ABI Implementation. This works for both little-endian and |
| 5849 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5850 | //===----------------------------------------------------------------------===// |
| 5851 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5852 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5853 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5854 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5855 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5856 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5857 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5858 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5859 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5860 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5861 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5862 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5863 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5864 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5865 | |
| 5866 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5867 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5868 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5869 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5870 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5871 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5872 | }; |
| 5873 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5874 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5875 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5876 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5877 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5878 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5879 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5880 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5881 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5882 | return 29; |
| 5883 | } |
| 5884 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5885 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5886 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5887 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5888 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5889 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5890 | if (FD->hasAttr<Mips16Attr>()) { |
| 5891 | Fn->addFnAttr("mips16"); |
| 5892 | } |
| 5893 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5894 | Fn->addFnAttr("nomips16"); |
| 5895 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5896 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5897 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5898 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5899 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5900 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5901 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5902 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5903 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5904 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5905 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5906 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5907 | void MipsABIInfo::CoerceToIntArgs( |
| 5908 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5909 | llvm::IntegerType *IntTy = |
| 5910 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5911 | |
| 5912 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5913 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5914 | ArgList.push_back(IntTy); |
| 5915 | |
| 5916 | // If necessary, add one more integer type to ArgList. |
| 5917 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5918 | |
| 5919 | if (R) |
| 5920 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5921 | } |
| 5922 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5923 | // In N32/64, an aligned double precision floating point field is passed in |
| 5924 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5925 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5926 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5927 | |
| 5928 | if (IsO32) { |
| 5929 | CoerceToIntArgs(TySize, ArgList); |
| 5930 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5931 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5932 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5933 | if (Ty->isComplexType()) |
| 5934 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5935 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5936 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5937 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5938 | // Unions/vectors are passed in integer registers. |
| 5939 | if (!RT || !RT->isStructureOrClassType()) { |
| 5940 | CoerceToIntArgs(TySize, ArgList); |
| 5941 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5942 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5943 | |
| 5944 | const RecordDecl *RD = RT->getDecl(); |
| 5945 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5946 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5947 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5948 | uint64_t LastOffset = 0; |
| 5949 | unsigned idx = 0; |
| 5950 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5951 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5952 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5953 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5954 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5955 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5956 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5957 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5958 | |
| 5959 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5960 | continue; |
| 5961 | |
| 5962 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5963 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5964 | continue; |
| 5965 | |
| 5966 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5967 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5968 | ArgList.push_back(I64); |
| 5969 | |
| 5970 | // Add double type. |
| 5971 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5972 | LastOffset = Offset + 64; |
| 5973 | } |
| 5974 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5975 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5976 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5977 | |
| 5978 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5979 | } |
| 5980 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5981 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5982 | uint64_t Offset) const { |
| 5983 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5984 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5985 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5986 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5987 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5988 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5989 | ABIArgInfo |
| 5990 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 5991 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5992 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5993 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5994 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5995 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5996 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5997 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5998 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5999 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 6000 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6001 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6002 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6003 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6004 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6005 | return ABIArgInfo::getIgnore(); |
| 6006 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6007 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6008 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6009 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6010 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6011 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6012 | // If we have reached here, aggregates are passed directly by coercing to |
| 6013 | // another structure type. Padding is inserted if the offset of the |
| 6014 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6015 | ABIArgInfo ArgInfo = |
| 6016 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6017 | getPaddingType(OrigOffset, CurrOffset)); |
| 6018 | ArgInfo.setInReg(true); |
| 6019 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6020 | } |
| 6021 | |
| 6022 | // Treat an enum type as its underlying type. |
| 6023 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6024 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6025 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6026 | // All integral types are promoted to the GPR width. |
| 6027 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6028 | return ABIArgInfo::getExtend(); |
| 6029 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6030 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6031 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6032 | } |
| 6033 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6034 | llvm::Type* |
| 6035 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6036 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6037 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6038 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6039 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6040 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6041 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6042 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6043 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6044 | // N32/64 returns struct/classes in floating point registers if the |
| 6045 | // following conditions are met: |
| 6046 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6047 | // 2. The struct/class has one or two fields all of which are floating |
| 6048 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6049 | // 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] | 6050 | // |
| 6051 | // Any other composite results are returned in integer registers. |
| 6052 | // |
| 6053 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6054 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6055 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6056 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6057 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6058 | if (!BT || !BT->isFloatingPoint()) |
| 6059 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6060 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6061 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6062 | } |
| 6063 | |
| 6064 | if (b == e) |
| 6065 | return llvm::StructType::get(getVMContext(), RTList, |
| 6066 | RD->hasAttr<PackedAttr>()); |
| 6067 | |
| 6068 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6069 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6072 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6073 | return llvm::StructType::get(getVMContext(), RTList); |
| 6074 | } |
| 6075 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6076 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6077 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6078 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6079 | if (RetTy->isVoidType()) |
| 6080 | return ABIArgInfo::getIgnore(); |
| 6081 | |
| 6082 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6083 | // However, N32/N64 ignores zero sized return values. |
| 6084 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6085 | return ABIArgInfo::getIgnore(); |
| 6086 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6087 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6088 | if (Size <= 128) { |
| 6089 | if (RetTy->isAnyComplexType()) |
| 6090 | return ABIArgInfo::getDirect(); |
| 6091 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6092 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6093 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6094 | if (!IsO32 || |
| 6095 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6096 | ABIArgInfo ArgInfo = |
| 6097 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6098 | ArgInfo.setInReg(true); |
| 6099 | return ArgInfo; |
| 6100 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6101 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6102 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6103 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6104 | } |
| 6105 | |
| 6106 | // Treat an enum type as its underlying type. |
| 6107 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6108 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6109 | |
| 6110 | return (RetTy->isPromotableIntegerType() ? |
| 6111 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6112 | } |
| 6113 | |
| 6114 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6115 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6116 | if (!getCXXABI().classifyReturnType(FI)) |
| 6117 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6118 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6119 | // 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] | 6120 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6121 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6122 | for (auto &I : FI.arguments()) |
| 6123 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6124 | } |
| 6125 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6126 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6127 | QualType OrigTy) const { |
| 6128 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6129 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6130 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6131 | // 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] | 6132 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6133 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6134 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6135 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6136 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6137 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6138 | DidPromote = true; |
| 6139 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6140 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6141 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6142 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6143 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6144 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6145 | // The alignment of things in the argument area is never larger than |
| 6146 | // StackAlignInBytes. |
| 6147 | TyInfo.second = |
| 6148 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6149 | |
| 6150 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6151 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6152 | |
| 6153 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6154 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6155 | |
| 6156 | |
| 6157 | // If there was a promotion, "unpromote" into a temporary. |
| 6158 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6159 | if (DidPromote) { |
| 6160 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6161 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6162 | |
| 6163 | // Truncate down to the right width. |
| 6164 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6165 | : CGF.IntPtrTy); |
| 6166 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6167 | if (OrigTy->isPointerType()) |
| 6168 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6169 | |
| 6170 | CGF.Builder.CreateStore(V, Temp); |
| 6171 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6172 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6173 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6174 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6177 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6178 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6179 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6180 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 6181 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 6182 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6183 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6184 | return false; |
| 6185 | } |
| 6186 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6187 | bool |
| 6188 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6189 | llvm::Value *Address) const { |
| 6190 | // This information comes from gcc's implementation, which seems to |
| 6191 | // as canonical as it gets. |
| 6192 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6193 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 6194 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6195 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6196 | |
| 6197 | // 0-31 are the general purpose registers, $0 - $31. |
| 6198 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 6199 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 6200 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6201 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6202 | |
| 6203 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 6204 | // They are one bit wide and ignored here. |
| 6205 | |
| 6206 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 6207 | // (coprocessor 1 is the FP unit) |
| 6208 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 6209 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 6210 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6211 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6212 | return false; |
| 6213 | } |
| 6214 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6215 | //===----------------------------------------------------------------------===// |
| 6216 | // 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] | 6217 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6218 | // handling. |
| 6219 | //===----------------------------------------------------------------------===// |
| 6220 | |
| 6221 | namespace { |
| 6222 | |
| 6223 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 6224 | public: |
| 6225 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 6226 | : DefaultTargetCodeGenInfo(CGT) {} |
| 6227 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6228 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6229 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6230 | }; |
| 6231 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6232 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6233 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6234 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6235 | if (!FD) return; |
| 6236 | |
| 6237 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6238 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6239 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6240 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 6241 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6242 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 6243 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 6244 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6245 | // Convert the reqd_work_group_size() attributes to metadata. |
| 6246 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6247 | llvm::NamedMDNode *OpenCLMetadata = |
| 6248 | M.getModule().getOrInsertNamedMetadata( |
| 6249 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6250 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6251 | SmallVector<llvm::Metadata *, 5> Operands; |
| 6252 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6253 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6254 | Operands.push_back( |
| 6255 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6256 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 6257 | Operands.push_back( |
| 6258 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6259 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 6260 | Operands.push_back( |
| 6261 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6262 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6263 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6264 | // Add a boolean constant operand for "required" (true) or "hint" |
| 6265 | // (false) for implementing the work_group_size_hint attr later. |
| 6266 | // 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] | 6267 | Operands.push_back( |
| 6268 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6269 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 6270 | } |
| 6271 | } |
| 6272 | } |
| 6273 | } |
| 6274 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6275 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6276 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6277 | //===----------------------------------------------------------------------===// |
| 6278 | // Hexagon ABI Implementation |
| 6279 | //===----------------------------------------------------------------------===// |
| 6280 | |
| 6281 | namespace { |
| 6282 | |
| 6283 | class HexagonABIInfo : public ABIInfo { |
| 6284 | |
| 6285 | |
| 6286 | public: |
| 6287 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6288 | |
| 6289 | private: |
| 6290 | |
| 6291 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6292 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 6293 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6294 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6295 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6296 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6297 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6298 | }; |
| 6299 | |
| 6300 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6301 | public: |
| 6302 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6303 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 6304 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6305 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6306 | return 29; |
| 6307 | } |
| 6308 | }; |
| 6309 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6310 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6311 | |
| 6312 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6313 | if (!getCXXABI().classifyReturnType(FI)) |
| 6314 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6315 | for (auto &I : FI.arguments()) |
| 6316 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6317 | } |
| 6318 | |
| 6319 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 6320 | if (!isAggregateTypeForABI(Ty)) { |
| 6321 | // Treat an enum type as its underlying type. |
| 6322 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6323 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6324 | |
| 6325 | return (Ty->isPromotableIntegerType() ? |
| 6326 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6327 | } |
| 6328 | |
| 6329 | // Ignore empty records. |
| 6330 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6331 | return ABIArgInfo::getIgnore(); |
| 6332 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6333 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6334 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6335 | |
| 6336 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6337 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6338 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6339 | // Pass in the smallest viable integer type. |
| 6340 | else if (Size > 32) |
| 6341 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6342 | else if (Size > 16) |
| 6343 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6344 | else if (Size > 8) |
| 6345 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6346 | else |
| 6347 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6348 | } |
| 6349 | |
| 6350 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6351 | if (RetTy->isVoidType()) |
| 6352 | return ABIArgInfo::getIgnore(); |
| 6353 | |
| 6354 | // Large vector types should be returned via memory. |
| 6355 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6356 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6357 | |
| 6358 | if (!isAggregateTypeForABI(RetTy)) { |
| 6359 | // Treat an enum type as its underlying type. |
| 6360 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6361 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6362 | |
| 6363 | return (RetTy->isPromotableIntegerType() ? |
| 6364 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6365 | } |
| 6366 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6367 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6368 | return ABIArgInfo::getIgnore(); |
| 6369 | |
| 6370 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6371 | // are returned indirectly. |
| 6372 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6373 | if (Size <= 64) { |
| 6374 | // Return in the smallest viable integer type. |
| 6375 | if (Size <= 8) |
| 6376 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6377 | if (Size <= 16) |
| 6378 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6379 | if (Size <= 32) |
| 6380 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6381 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6382 | } |
| 6383 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6384 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6387 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6388 | QualType Ty) const { |
| 6389 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 6390 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6391 | getContext().getTypeInfoInChars(Ty), |
| 6392 | CharUnits::fromQuantity(4), |
| 6393 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6394 | } |
| 6395 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6396 | //===----------------------------------------------------------------------===// |
| 6397 | // AMDGPU ABI Implementation |
| 6398 | //===----------------------------------------------------------------------===// |
| 6399 | |
| 6400 | namespace { |
| 6401 | |
| 6402 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6403 | public: |
| 6404 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6405 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6406 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6407 | CodeGen::CodeGenModule &M) const override; |
| 6408 | }; |
| 6409 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6410 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6411 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6412 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6413 | const Decl *D, |
| 6414 | llvm::GlobalValue *GV, |
| 6415 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6416 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6417 | if (!FD) |
| 6418 | return; |
| 6419 | |
| 6420 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6421 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6422 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6423 | if (NumVGPR != 0) |
| 6424 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6425 | } |
| 6426 | |
| 6427 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6428 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6429 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6430 | if (NumSGPR != 0) |
| 6431 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6432 | } |
| 6433 | } |
| 6434 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6435 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6436 | //===----------------------------------------------------------------------===// |
| 6437 | // SPARC v9 ABI Implementation. |
| 6438 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6439 | // |
| 6440 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6441 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6442 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6443 | // |
| 6444 | // One case requires special care: |
| 6445 | // |
| 6446 | // struct mixed { |
| 6447 | // int i; |
| 6448 | // float f; |
| 6449 | // }; |
| 6450 | // |
| 6451 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6452 | // parameter array, but the int is passed in an integer register, and the float |
| 6453 | // is passed in a floating point register. This is represented as two arguments |
| 6454 | // with the LLVM IR inreg attribute: |
| 6455 | // |
| 6456 | // declare void f(i32 inreg %i, float inreg %f) |
| 6457 | // |
| 6458 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6459 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6460 | // bytes. |
| 6461 | // |
| 6462 | namespace { |
| 6463 | class SparcV9ABIInfo : public ABIInfo { |
| 6464 | public: |
| 6465 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6466 | |
| 6467 | private: |
| 6468 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6469 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6470 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6471 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6472 | |
| 6473 | // Coercion type builder for structs passed in registers. The coercion type |
| 6474 | // serves two purposes: |
| 6475 | // |
| 6476 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6477 | // in registers. |
| 6478 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6479 | // code generator knows to pass them in floating point registers. |
| 6480 | // |
| 6481 | // We also compute the InReg flag which indicates that the struct contains |
| 6482 | // aligned 32-bit floats. |
| 6483 | // |
| 6484 | struct CoerceBuilder { |
| 6485 | llvm::LLVMContext &Context; |
| 6486 | const llvm::DataLayout &DL; |
| 6487 | SmallVector<llvm::Type*, 8> Elems; |
| 6488 | uint64_t Size; |
| 6489 | bool InReg; |
| 6490 | |
| 6491 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6492 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6493 | |
| 6494 | // Pad Elems with integers until Size is ToSize. |
| 6495 | void pad(uint64_t ToSize) { |
| 6496 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6497 | if (ToSize == Size) |
| 6498 | return; |
| 6499 | |
| 6500 | // Finish the current 64-bit word. |
| 6501 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 6502 | if (Aligned > Size && Aligned <= ToSize) { |
| 6503 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6504 | Size = Aligned; |
| 6505 | } |
| 6506 | |
| 6507 | // Add whole 64-bit words. |
| 6508 | while (Size + 64 <= ToSize) { |
| 6509 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6510 | Size += 64; |
| 6511 | } |
| 6512 | |
| 6513 | // Final in-word padding. |
| 6514 | if (Size < ToSize) { |
| 6515 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6516 | Size = ToSize; |
| 6517 | } |
| 6518 | } |
| 6519 | |
| 6520 | // Add a floating point element at Offset. |
| 6521 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6522 | // Unaligned floats are treated as integers. |
| 6523 | if (Offset % Bits) |
| 6524 | return; |
| 6525 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6526 | if (Bits < 64) |
| 6527 | InReg = true; |
| 6528 | pad(Offset); |
| 6529 | Elems.push_back(Ty); |
| 6530 | Size = Offset + Bits; |
| 6531 | } |
| 6532 | |
| 6533 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6534 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6535 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6536 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6537 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6538 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6539 | switch (ElemTy->getTypeID()) { |
| 6540 | case llvm::Type::StructTyID: |
| 6541 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6542 | break; |
| 6543 | case llvm::Type::FloatTyID: |
| 6544 | addFloat(ElemOffset, ElemTy, 32); |
| 6545 | break; |
| 6546 | case llvm::Type::DoubleTyID: |
| 6547 | addFloat(ElemOffset, ElemTy, 64); |
| 6548 | break; |
| 6549 | case llvm::Type::FP128TyID: |
| 6550 | addFloat(ElemOffset, ElemTy, 128); |
| 6551 | break; |
| 6552 | case llvm::Type::PointerTyID: |
| 6553 | if (ElemOffset % 64 == 0) { |
| 6554 | pad(ElemOffset); |
| 6555 | Elems.push_back(ElemTy); |
| 6556 | Size += 64; |
| 6557 | } |
| 6558 | break; |
| 6559 | default: |
| 6560 | break; |
| 6561 | } |
| 6562 | } |
| 6563 | } |
| 6564 | |
| 6565 | // Check if Ty is a usable substitute for the coercion type. |
| 6566 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 6567 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6568 | } |
| 6569 | |
| 6570 | // Get the coercion type as a literal struct type. |
| 6571 | llvm::Type *getType() const { |
| 6572 | if (Elems.size() == 1) |
| 6573 | return Elems.front(); |
| 6574 | else |
| 6575 | return llvm::StructType::get(Context, Elems); |
| 6576 | } |
| 6577 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6578 | }; |
| 6579 | } // end anonymous namespace |
| 6580 | |
| 6581 | ABIArgInfo |
| 6582 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6583 | if (Ty->isVoidType()) |
| 6584 | return ABIArgInfo::getIgnore(); |
| 6585 | |
| 6586 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6587 | |
| 6588 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6589 | // pointer / sret pointer. |
| 6590 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6591 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6592 | |
| 6593 | // Treat an enum type as its underlying type. |
| 6594 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6595 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6596 | |
| 6597 | // Integer types smaller than a register are extended. |
| 6598 | if (Size < 64 && Ty->isIntegerType()) |
| 6599 | return ABIArgInfo::getExtend(); |
| 6600 | |
| 6601 | // Other non-aggregates go in registers. |
| 6602 | if (!isAggregateTypeForABI(Ty)) |
| 6603 | return ABIArgInfo::getDirect(); |
| 6604 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6605 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6606 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 6607 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6608 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6609 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6610 | // 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] | 6611 | // Build a coercion type from the LLVM struct type. |
| 6612 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6613 | if (!StrTy) |
| 6614 | return ABIArgInfo::getDirect(); |
| 6615 | |
| 6616 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6617 | CB.addStruct(0, StrTy); |
| 6618 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 6619 | |
| 6620 | // Try to use the original type for coercion. |
| 6621 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6622 | |
| 6623 | if (CB.InReg) |
| 6624 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6625 | else |
| 6626 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6627 | } |
| 6628 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6629 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6630 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6631 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6632 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6633 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6634 | AI.setCoerceToType(ArgTy); |
| 6635 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6636 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6637 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6638 | CGBuilderTy &Builder = CGF.Builder; |
| 6639 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 6640 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6641 | |
| 6642 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 6643 | |
| 6644 | Address ArgAddr = Address::invalid(); |
| 6645 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6646 | switch (AI.getKind()) { |
| 6647 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6648 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6649 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6650 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6651 | case ABIArgInfo::Extend: { |
| 6652 | Stride = SlotSize; |
| 6653 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 6654 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6655 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6656 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6657 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6658 | case ABIArgInfo::Direct: { |
| 6659 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6660 | Stride = CharUnits::fromQuantity(AllocSize).RoundUpToAlignment(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6661 | ArgAddr = Addr; |
| 6662 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6663 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6664 | |
| 6665 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6666 | Stride = SlotSize; |
| 6667 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 6668 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 6669 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6670 | break; |
| 6671 | |
| 6672 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6673 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6674 | } |
| 6675 | |
| 6676 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6677 | llvm::Value *NextPtr = |
| 6678 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 6679 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6680 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6681 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6682 | } |
| 6683 | |
| 6684 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6685 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6686 | for (auto &I : FI.arguments()) |
| 6687 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6688 | } |
| 6689 | |
| 6690 | namespace { |
| 6691 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6692 | public: |
| 6693 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6694 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6695 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6696 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6697 | return 14; |
| 6698 | } |
| 6699 | |
| 6700 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6701 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6702 | }; |
| 6703 | } // end anonymous namespace |
| 6704 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6705 | bool |
| 6706 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6707 | llvm::Value *Address) const { |
| 6708 | // This is calculated from the LLVM and GCC tables and verified |
| 6709 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6710 | |
| 6711 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6712 | |
| 6713 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6714 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6715 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6716 | |
| 6717 | // 0-31: the 8-byte general-purpose registers |
| 6718 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6719 | |
| 6720 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6721 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6722 | |
| 6723 | // Y = 64 |
| 6724 | // PSR = 65 |
| 6725 | // WIM = 66 |
| 6726 | // TBR = 67 |
| 6727 | // PC = 68 |
| 6728 | // NPC = 69 |
| 6729 | // FSR = 70 |
| 6730 | // CSR = 71 |
| 6731 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6732 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6733 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6734 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6735 | |
| 6736 | return false; |
| 6737 | } |
| 6738 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6739 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6740 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6741 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6742 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6743 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6744 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6745 | |
| 6746 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6747 | /// it by reference between functions that append to it. |
| 6748 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6749 | |
| 6750 | /// TypeStringCache caches the meta encodings of Types. |
| 6751 | /// |
| 6752 | /// The reason for caching TypeStrings is two fold: |
| 6753 | /// 1. To cache a type's encoding for later uses; |
| 6754 | /// 2. As a means to break recursive member type inclusion. |
| 6755 | /// |
| 6756 | /// A cache Entry can have a Status of: |
| 6757 | /// NonRecursive: The type encoding is not recursive; |
| 6758 | /// Recursive: The type encoding is recursive; |
| 6759 | /// Incomplete: An incomplete TypeString; |
| 6760 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6761 | /// Recursive type encoding. |
| 6762 | /// |
| 6763 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6764 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6765 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6766 | /// the type is encountered. |
| 6767 | /// |
| 6768 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6769 | /// possible. The type itself is recursive and it may contain other types which |
| 6770 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6771 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6772 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6773 | /// |
| 6774 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6775 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6776 | /// are placed into the cache during type expansion as a means to identify and |
| 6777 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6778 | /// the entry becomes IncompleteUsed. |
| 6779 | /// |
| 6780 | /// During the expansion of a RecordType's members: |
| 6781 | /// |
| 6782 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6783 | /// cached encoding is used; |
| 6784 | /// |
| 6785 | /// If the cache contains a Recursive encoding for the member type, the |
| 6786 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6787 | /// |
| 6788 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6789 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6790 | /// |
| 6791 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6792 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6793 | /// it is swapped back in; |
| 6794 | /// |
| 6795 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6796 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6797 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6798 | /// |
| 6799 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6800 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6801 | /// Else the member is part of a recursive type and thus the recursion has |
| 6802 | /// been exited too soon for the encoding to be correct for the member. |
| 6803 | /// |
| 6804 | class TypeStringCache { |
| 6805 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6806 | struct Entry { |
| 6807 | std::string Str; // The encoded TypeString for the type. |
| 6808 | enum Status State; // Information about the encoding in 'Str'. |
| 6809 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6810 | // during the expansion of RecordType's members. |
| 6811 | }; |
| 6812 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6813 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6814 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6815 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 6816 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6817 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6818 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6819 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6820 | bool IsRecursive); |
| 6821 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6822 | }; |
| 6823 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6824 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6825 | /// FieldEncoding is a helper for this ordering process. |
| 6826 | class FieldEncoding { |
| 6827 | bool HasName; |
| 6828 | std::string Enc; |
| 6829 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 6830 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
| 6831 | StringRef str() {return Enc.c_str();} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6832 | bool operator<(const FieldEncoding &rhs) const { |
| 6833 | if (HasName != rhs.HasName) return HasName; |
| 6834 | return Enc < rhs.Enc; |
| 6835 | } |
| 6836 | }; |
| 6837 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6838 | class XCoreABIInfo : public DefaultABIInfo { |
| 6839 | public: |
| 6840 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6841 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6842 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6843 | }; |
| 6844 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6845 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6846 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6847 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6848 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6849 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6850 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6851 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6852 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6853 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6854 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6855 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6856 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6857 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6858 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6859 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6860 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6861 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 6862 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6863 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6864 | // Handle the argument. |
| 6865 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6866 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6867 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6868 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6869 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6870 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6871 | |
| 6872 | Address Val = Address::invalid(); |
| 6873 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6874 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6875 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6876 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6877 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6878 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6879 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 6880 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6881 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6882 | case ABIArgInfo::Extend: |
| 6883 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6884 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 6885 | ArgSize = CharUnits::fromQuantity( |
| 6886 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
| 6887 | ArgSize = ArgSize.RoundUpToAlignment(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6888 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6889 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6890 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 6891 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 6892 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6893 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6894 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6895 | |
| 6896 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6897 | if (!ArgSize.isZero()) { |
| 6898 | llvm::Value *APN = |
| 6899 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 6900 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6901 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6902 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6903 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6904 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6905 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6906 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6907 | /// into the cache as a means to identify and break recursion. |
| 6908 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6909 | /// be reinserted by removeIncomplete(). |
| 6910 | /// All other types of encoding should have been used rather than arriving here. |
| 6911 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6912 | std::string StubEnc) { |
| 6913 | if (!ID) |
| 6914 | return; |
| 6915 | Entry &E = Map[ID]; |
| 6916 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6917 | "Incorrectly use of addIncomplete"); |
| 6918 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6919 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6920 | E.Str.swap(StubEnc); |
| 6921 | E.State = Incomplete; |
| 6922 | ++IncompleteCount; |
| 6923 | } |
| 6924 | |
| 6925 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6926 | /// must be removed from the cache. |
| 6927 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6928 | /// Returns true if the RecordType was defined recursively. |
| 6929 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6930 | if (!ID) |
| 6931 | return false; |
| 6932 | auto I = Map.find(ID); |
| 6933 | assert(I != Map.end() && "Entry not present"); |
| 6934 | Entry &E = I->second; |
| 6935 | assert( (E.State == Incomplete || |
| 6936 | E.State == IncompleteUsed) && |
| 6937 | "Entry must be an incomplete type"); |
| 6938 | bool IsRecursive = false; |
| 6939 | if (E.State == IncompleteUsed) { |
| 6940 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6941 | IsRecursive = true; |
| 6942 | --IncompleteUsedCount; |
| 6943 | } |
| 6944 | if (E.Swapped.empty()) |
| 6945 | Map.erase(I); |
| 6946 | else { |
| 6947 | // Swap the Recursive back. |
| 6948 | E.Swapped.swap(E.Str); |
| 6949 | E.Swapped.clear(); |
| 6950 | E.State = Recursive; |
| 6951 | } |
| 6952 | --IncompleteCount; |
| 6953 | return IsRecursive; |
| 6954 | } |
| 6955 | |
| 6956 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 6957 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 6958 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6959 | bool IsRecursive) { |
| 6960 | if (!ID || IncompleteUsedCount) |
| 6961 | return; // No key or it is is an incomplete sub-type so don't add. |
| 6962 | Entry &E = Map[ID]; |
| 6963 | if (IsRecursive && !E.Str.empty()) { |
| 6964 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 6965 | "This is not the same Recursive entry"); |
| 6966 | // The parent container was not recursive after all, so we could have used |
| 6967 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 6968 | // we started viz: IncompleteCount!=0. |
| 6969 | return; |
| 6970 | } |
| 6971 | assert(E.Str.empty() && "Entry already present"); |
| 6972 | E.Str = Str.str(); |
| 6973 | E.State = IsRecursive? Recursive : NonRecursive; |
| 6974 | } |
| 6975 | |
| 6976 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 6977 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 6978 | /// encoding is Recursive, return an empty StringRef. |
| 6979 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 6980 | if (!ID) |
| 6981 | return StringRef(); // We have no key. |
| 6982 | auto I = Map.find(ID); |
| 6983 | if (I == Map.end()) |
| 6984 | return StringRef(); // We have no encoding. |
| 6985 | Entry &E = I->second; |
| 6986 | if (E.State == Recursive && IncompleteCount) |
| 6987 | return StringRef(); // We don't use Recursive encodings for member types. |
| 6988 | |
| 6989 | if (E.State == Incomplete) { |
| 6990 | // The incomplete type is being used to break out of recursion. |
| 6991 | E.State = IncompleteUsed; |
| 6992 | ++IncompleteUsedCount; |
| 6993 | } |
| 6994 | return E.Str.c_str(); |
| 6995 | } |
| 6996 | |
| 6997 | /// The XCore ABI includes a type information section that communicates symbol |
| 6998 | /// type information to the linker. The linker uses this information to verify |
| 6999 | /// safety/correctness of things such as array bound and pointers et al. |
| 7000 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 7001 | /// This type information (TypeString) is emitted into meta data for all global |
| 7002 | /// symbols: definitions, declarations, functions & variables. |
| 7003 | /// |
| 7004 | /// The TypeString carries type, qualifier, name, size & value details. |
| 7005 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7006 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7007 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 7008 | /// |
| 7009 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7010 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 7011 | |
| 7012 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 7013 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7014 | CodeGen::CodeGenModule &CGM) const { |
| 7015 | SmallStringEnc Enc; |
| 7016 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 7017 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7018 | llvm::SmallVector<llvm::Metadata *, 2> MDVals; |
| 7019 | MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7020 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 7021 | llvm::NamedMDNode *MD = |
| 7022 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 7023 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 7024 | } |
| 7025 | } |
| 7026 | |
| 7027 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7028 | const CodeGen::CodeGenModule &CGM, |
| 7029 | TypeStringCache &TSC); |
| 7030 | |
| 7031 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7032 | /// Builds a SmallVector containing the encoded field types in declaration |
| 7033 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7034 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 7035 | const RecordDecl *RD, |
| 7036 | const CodeGen::CodeGenModule &CGM, |
| 7037 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7038 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7039 | SmallStringEnc Enc; |
| 7040 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7041 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7042 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7043 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7044 | Enc += "b("; |
| 7045 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7046 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7047 | Enc += ':'; |
| 7048 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7049 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7050 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7051 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7052 | Enc += ')'; |
| 7053 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 7054 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7055 | } |
| 7056 | return true; |
| 7057 | } |
| 7058 | |
| 7059 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 7060 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 7061 | /// Union types have their fields ordered according to the ABI. |
| 7062 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 7063 | const CodeGen::CodeGenModule &CGM, |
| 7064 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 7065 | // Append the cached TypeString if we have one. |
| 7066 | StringRef TypeString = TSC.lookupStr(ID); |
| 7067 | if (!TypeString.empty()) { |
| 7068 | Enc += TypeString; |
| 7069 | return true; |
| 7070 | } |
| 7071 | |
| 7072 | // Start to emit an incomplete TypeString. |
| 7073 | size_t Start = Enc.size(); |
| 7074 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 7075 | Enc += '('; |
| 7076 | if (ID) |
| 7077 | Enc += ID->getName(); |
| 7078 | Enc += "){"; |
| 7079 | |
| 7080 | // We collect all encoded fields and order as necessary. |
| 7081 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7082 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 7083 | if (RD && !RD->field_empty()) { |
| 7084 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 7085 | // so that recursive calls to this RecordType will use it whilst building a |
| 7086 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7087 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7088 | std::string StubEnc(Enc.substr(Start).str()); |
| 7089 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 7090 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 7091 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 7092 | (void) TSC.removeIncomplete(ID); |
| 7093 | return false; |
| 7094 | } |
| 7095 | IsRecursive = TSC.removeIncomplete(ID); |
| 7096 | // The ABI requires unions to be sorted but not structures. |
| 7097 | // See FieldEncoding::operator< for sort algorithm. |
| 7098 | if (RT->isUnionType()) |
| 7099 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7100 | // We can now complete the TypeString. |
| 7101 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7102 | for (unsigned I = 0; I != E; ++I) { |
| 7103 | if (I) |
| 7104 | Enc += ','; |
| 7105 | Enc += FE[I].str(); |
| 7106 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7107 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7108 | Enc += '}'; |
| 7109 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 7110 | return true; |
| 7111 | } |
| 7112 | |
| 7113 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 7114 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 7115 | TypeStringCache &TSC, |
| 7116 | const IdentifierInfo *ID) { |
| 7117 | // Append the cached TypeString if we have one. |
| 7118 | StringRef TypeString = TSC.lookupStr(ID); |
| 7119 | if (!TypeString.empty()) { |
| 7120 | Enc += TypeString; |
| 7121 | return true; |
| 7122 | } |
| 7123 | |
| 7124 | size_t Start = Enc.size(); |
| 7125 | Enc += "e("; |
| 7126 | if (ID) |
| 7127 | Enc += ID->getName(); |
| 7128 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7129 | |
| 7130 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7131 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7132 | SmallVector<FieldEncoding, 16> FE; |
| 7133 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 7134 | ++I) { |
| 7135 | SmallStringEnc EnumEnc; |
| 7136 | EnumEnc += "m("; |
| 7137 | EnumEnc += I->getName(); |
| 7138 | EnumEnc += "){"; |
| 7139 | I->getInitVal().toString(EnumEnc); |
| 7140 | EnumEnc += '}'; |
| 7141 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 7142 | } |
| 7143 | std::sort(FE.begin(), FE.end()); |
| 7144 | unsigned E = FE.size(); |
| 7145 | for (unsigned I = 0; I != E; ++I) { |
| 7146 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7147 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7148 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7149 | } |
| 7150 | } |
| 7151 | Enc += '}'; |
| 7152 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 7153 | return true; |
| 7154 | } |
| 7155 | |
| 7156 | /// Appends type's qualifier to Enc. |
| 7157 | /// This is done prior to appending the type's encoding. |
| 7158 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 7159 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 7160 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7161 | int Lookup = 0; |
| 7162 | if (QT.isConstQualified()) |
| 7163 | Lookup += 1<<0; |
| 7164 | if (QT.isRestrictQualified()) |
| 7165 | Lookup += 1<<1; |
| 7166 | if (QT.isVolatileQualified()) |
| 7167 | Lookup += 1<<2; |
| 7168 | Enc += Table[Lookup]; |
| 7169 | } |
| 7170 | |
| 7171 | /// Appends built-in types to Enc. |
| 7172 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 7173 | const char *EncType; |
| 7174 | switch (BT->getKind()) { |
| 7175 | case BuiltinType::Void: |
| 7176 | EncType = "0"; |
| 7177 | break; |
| 7178 | case BuiltinType::Bool: |
| 7179 | EncType = "b"; |
| 7180 | break; |
| 7181 | case BuiltinType::Char_U: |
| 7182 | EncType = "uc"; |
| 7183 | break; |
| 7184 | case BuiltinType::UChar: |
| 7185 | EncType = "uc"; |
| 7186 | break; |
| 7187 | case BuiltinType::SChar: |
| 7188 | EncType = "sc"; |
| 7189 | break; |
| 7190 | case BuiltinType::UShort: |
| 7191 | EncType = "us"; |
| 7192 | break; |
| 7193 | case BuiltinType::Short: |
| 7194 | EncType = "ss"; |
| 7195 | break; |
| 7196 | case BuiltinType::UInt: |
| 7197 | EncType = "ui"; |
| 7198 | break; |
| 7199 | case BuiltinType::Int: |
| 7200 | EncType = "si"; |
| 7201 | break; |
| 7202 | case BuiltinType::ULong: |
| 7203 | EncType = "ul"; |
| 7204 | break; |
| 7205 | case BuiltinType::Long: |
| 7206 | EncType = "sl"; |
| 7207 | break; |
| 7208 | case BuiltinType::ULongLong: |
| 7209 | EncType = "ull"; |
| 7210 | break; |
| 7211 | case BuiltinType::LongLong: |
| 7212 | EncType = "sll"; |
| 7213 | break; |
| 7214 | case BuiltinType::Float: |
| 7215 | EncType = "ft"; |
| 7216 | break; |
| 7217 | case BuiltinType::Double: |
| 7218 | EncType = "d"; |
| 7219 | break; |
| 7220 | case BuiltinType::LongDouble: |
| 7221 | EncType = "ld"; |
| 7222 | break; |
| 7223 | default: |
| 7224 | return false; |
| 7225 | } |
| 7226 | Enc += EncType; |
| 7227 | return true; |
| 7228 | } |
| 7229 | |
| 7230 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 7231 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 7232 | const CodeGen::CodeGenModule &CGM, |
| 7233 | TypeStringCache &TSC) { |
| 7234 | Enc += "p("; |
| 7235 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 7236 | return false; |
| 7237 | Enc += ')'; |
| 7238 | return true; |
| 7239 | } |
| 7240 | |
| 7241 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7242 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 7243 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7244 | const CodeGen::CodeGenModule &CGM, |
| 7245 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 7246 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 7247 | return false; |
| 7248 | Enc += "a("; |
| 7249 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 7250 | CAT->getSize().toStringUnsigned(Enc); |
| 7251 | else |
| 7252 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 7253 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7254 | // The Qualifiers should be attached to the type rather than the array. |
| 7255 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7256 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 7257 | return false; |
| 7258 | Enc += ')'; |
| 7259 | return true; |
| 7260 | } |
| 7261 | |
| 7262 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 7263 | /// and the arguments. |
| 7264 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 7265 | const CodeGen::CodeGenModule &CGM, |
| 7266 | TypeStringCache &TSC) { |
| 7267 | Enc += "f{"; |
| 7268 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 7269 | return false; |
| 7270 | Enc += "}("; |
| 7271 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 7272 | // N.B. we are only interested in the adjusted param types. |
| 7273 | auto I = FPT->param_type_begin(); |
| 7274 | auto E = FPT->param_type_end(); |
| 7275 | if (I != E) { |
| 7276 | do { |
| 7277 | if (!appendType(Enc, *I, CGM, TSC)) |
| 7278 | return false; |
| 7279 | ++I; |
| 7280 | if (I != E) |
| 7281 | Enc += ','; |
| 7282 | } while (I != E); |
| 7283 | if (FPT->isVariadic()) |
| 7284 | Enc += ",va"; |
| 7285 | } else { |
| 7286 | if (FPT->isVariadic()) |
| 7287 | Enc += "va"; |
| 7288 | else |
| 7289 | Enc += '0'; |
| 7290 | } |
| 7291 | } |
| 7292 | Enc += ')'; |
| 7293 | return true; |
| 7294 | } |
| 7295 | |
| 7296 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 7297 | /// type encodings. |
| 7298 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7299 | const CodeGen::CodeGenModule &CGM, |
| 7300 | TypeStringCache &TSC) { |
| 7301 | |
| 7302 | QualType QT = QType.getCanonicalType(); |
| 7303 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7304 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 7305 | // The Qualifiers should be attached to the type rather than the array. |
| 7306 | // Thus we don't call appendQualifier() here. |
| 7307 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 7308 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7309 | appendQualifier(Enc, QT); |
| 7310 | |
| 7311 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 7312 | return appendBuiltinType(Enc, BT); |
| 7313 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7314 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 7315 | return appendPointerType(Enc, PT, CGM, TSC); |
| 7316 | |
| 7317 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 7318 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 7319 | |
| 7320 | if (const RecordType *RT = QT->getAsStructureType()) |
| 7321 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7322 | |
| 7323 | if (const RecordType *RT = QT->getAsUnionType()) |
| 7324 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7325 | |
| 7326 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7327 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7328 | |
| 7329 | return false; |
| 7330 | } |
| 7331 | |
| 7332 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7333 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 7334 | if (!D) |
| 7335 | return false; |
| 7336 | |
| 7337 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7338 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 7339 | return false; |
| 7340 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 7341 | } |
| 7342 | |
| 7343 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7344 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 7345 | return false; |
| 7346 | QualType QT = VD->getType().getCanonicalType(); |
| 7347 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 7348 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7349 | // The Qualifiers should be attached to the type rather than the array. |
| 7350 | // Thus we don't call appendQualifier() here. |
| 7351 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7352 | } |
| 7353 | return appendType(Enc, QT, CGM, TSC); |
| 7354 | } |
| 7355 | return false; |
| 7356 | } |
| 7357 | |
| 7358 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7359 | //===----------------------------------------------------------------------===// |
| 7360 | // Driver code |
| 7361 | //===----------------------------------------------------------------------===// |
| 7362 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 7363 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 7364 | return getTarget().getTriple(); |
| 7365 | } |
| 7366 | |
| 7367 | bool CodeGenModule::supportsCOMDAT() const { |
| 7368 | return !getTriple().isOSBinFormatMachO(); |
| 7369 | } |
| 7370 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7371 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7372 | if (TheTargetCodeGenInfo) |
| 7373 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7374 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7375 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 7376 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7377 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7378 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7379 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 7380 | case llvm::Triple::le32: |
| 7381 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7382 | case llvm::Triple::mips: |
| 7383 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 7384 | if (Triple.getOS() == llvm::Triple::NaCl) |
| 7385 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7386 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 7387 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7388 | case llvm::Triple::mips64: |
| 7389 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7390 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 7391 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 7392 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 7393 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7394 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7395 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7396 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7397 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7398 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7399 | } |
| 7400 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 7401 | case llvm::Triple::wasm32: |
| 7402 | case llvm::Triple::wasm64: |
| 7403 | return *(TheTargetCodeGenInfo = new WebAssemblyTargetCodeGenInfo(Types)); |
| 7404 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7405 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7406 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7407 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7408 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7409 | { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 7410 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7411 | TheTargetCodeGenInfo = |
| 7412 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP); |
| 7413 | return *TheTargetCodeGenInfo; |
| 7414 | } |
| 7415 | |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7416 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 7417 | StringRef ABIStr = getTarget().getABI(); |
| 7418 | if (ABIStr == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7419 | Kind = ARMABIInfo::APCS; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 7420 | else if (ABIStr == "aapcs16") |
| 7421 | Kind = ARMABIInfo::AAPCS16_VFP; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 7422 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7423 | (CodeGenOpts.FloatABI != "soft" && |
| 7424 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7425 | Kind = ARMABIInfo::AAPCS_VFP; |
| 7426 | |
Derek Schuff | 71658bd | 2015-01-29 00:47:04 +0000 | [diff] [blame] | 7427 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7428 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7429 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7430 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7431 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 7432 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7433 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7434 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7435 | if (getTarget().getABI() == "elfv2") |
| 7436 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7437 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7438 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7439 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7440 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7441 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 7442 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7443 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 7444 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7445 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7446 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7447 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7448 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7449 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7450 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7451 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7452 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7453 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 7454 | case llvm::Triple::nvptx: |
| 7455 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 7456 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 7457 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7458 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7459 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7460 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 7461 | case llvm::Triple::systemz: { |
| 7462 | bool HasVector = getTarget().getABI() == "vector"; |
| 7463 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types, |
| 7464 | HasVector)); |
| 7465 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7466 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7467 | case llvm::Triple::tce: |
| 7468 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 7469 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7470 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7471 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7472 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7473 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 7474 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 7475 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7476 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7477 | return *(TheTargetCodeGenInfo = new WinX86_32TargetCodeGenInfo( |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7478 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7479 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7480 | } else { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7481 | return *(TheTargetCodeGenInfo = new X86_32TargetCodeGenInfo( |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7482 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 7483 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 7484 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7485 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7486 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7487 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7488 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7489 | StringRef ABI = getTarget().getABI(); |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 7490 | X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512 : |
| 7491 | ABI == "avx" ? X86AVXABILevel::AVX : |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7492 | X86AVXABILevel::None); |
| 7493 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7494 | switch (Triple.getOS()) { |
| 7495 | case llvm::Triple::Win32: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7496 | return *(TheTargetCodeGenInfo = |
| 7497 | new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 7498 | case llvm::Triple::PS4: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7499 | return *(TheTargetCodeGenInfo = |
| 7500 | new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7501 | default: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7502 | return *(TheTargetCodeGenInfo = |
| 7503 | new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7504 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7505 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7506 | case llvm::Triple::hexagon: |
| 7507 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7508 | case llvm::Triple::r600: |
| 7509 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 7510 | case llvm::Triple::amdgcn: |
| 7511 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7512 | case llvm::Triple::sparcv9: |
| 7513 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7514 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7515 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7516 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7517 | } |