blob: efa56397848036bc1f722a3e7867ccd7ebcc2ae0 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
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 Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +000017#include "CGCXXABI.h"
Stephen Hines176edba2014-12-01 14:53:08 -080018#include "CGValue.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000019#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Mark Lacey8b549992013-10-30 21:53:58 +000021#include "clang/CodeGen/CGFunctionInfo.h"
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070022#include "clang/CodeGen/SwiftCallingConv.h"
Sandeep Patel34c1af82011-04-05 00:23:47 +000023#include "clang/Frontend/CodeGenOptions.h"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070024#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000025#include "llvm/ADT/Triple.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/Type.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000028#include "llvm/Support/raw_ostream.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070029#include <algorithm> // std::sort
30
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000031using namespace clang;
32using namespace CodeGen;
33
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070034// Helper for coercing an aggregate argument or return value into an integer
35// array of the same size (including padding) and alignment. This alternate
36// coercion happens only for the RenderScript ABI and can be removed after
37// runtimes that rely on it are no longer supported.
Matt Wala1d151512015-08-10 15:58:40 -070038//
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070039// RenderScript assumes that the size of the argument / return value in the IR
40// is the same as the size of the corresponding qualified type. This helper
41// coerces the aggregate type into an array of the same size (including
42// padding). This coercion is used in lieu of expansion of struct members or
43// other canonical coercions that return a coerced-type of larger size.
Matt Wala1d151512015-08-10 15:58:40 -070044//
45// Ty - The argument / return value type
46// Context - The associated ASTContext
47// LLVMContext - The associated LLVMContext
48static ABIArgInfo coerceToIntArray(QualType Ty,
49 ASTContext &Context,
50 llvm::LLVMContext &LLVMContext) {
51 // Alignment and Size are measured in bits.
52 const uint64_t Size = Context.getTypeSize(Ty);
53 const uint64_t Alignment = Context.getTypeAlign(Ty);
54 llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
55 const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
56 return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
57}
58
John McCallaeeb7012010-05-27 06:19:26 +000059static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
60 llvm::Value *Array,
61 llvm::Value *Value,
62 unsigned FirstIndex,
63 unsigned LastIndex) {
64 // Alternatively, we could emit this as a loop in the source.
65 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -070066 llvm::Value *Cell =
67 Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080068 Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
John McCallaeeb7012010-05-27 06:19:26 +000069 }
70}
71
John McCalld608cdb2010-08-22 10:59:02 +000072static bool isAggregateTypeForABI(QualType T) {
John McCall9d232c82013-03-07 21:37:08 +000073 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalld608cdb2010-08-22 10:59:02 +000074 T->isMemberFunctionPointerType();
75}
76
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -080077ABIArgInfo
78ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign,
79 llvm::Type *Padding) const {
80 return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
81 ByRef, Realign, Padding);
82}
83
84ABIArgInfo
85ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
86 return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
87 /*ByRef*/ false, Realign);
88}
89
90Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
91 QualType Ty) const {
92 return Address::invalid();
93}
94
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000095ABIInfo::~ABIInfo() {}
96
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -070097/// Does the given lowering require more than the given number of
98/// registers when expanded?
99///
100/// This is intended to be the basis of a reasonable basic implementation
101/// of should{Pass,Return}IndirectlyForSwift.
102///
103/// For most targets, a limit of four total registers is reasonable; this
104/// limits the amount of code required in order to move around the value
105/// in case it wasn't produced immediately prior to the call by the caller
106/// (or wasn't produced in exactly the right registers) or isn't used
107/// immediately within the callee. But some targets may need to further
108/// limit the register count due to an inability to support that many
109/// return registers.
110static bool occupiesMoreThan(CodeGenTypes &cgt,
111 ArrayRef<llvm::Type*> scalarTypes,
112 unsigned maxAllRegisters) {
113 unsigned intCount = 0, fpCount = 0;
114 for (llvm::Type *type : scalarTypes) {
115 if (type->isPointerTy()) {
116 intCount++;
117 } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
118 auto ptrWidth = cgt.getTarget().getPointerWidth(0);
119 intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
120 } else {
121 assert(type->isVectorTy() || type->isFloatingPointTy());
122 fpCount++;
123 }
124 }
125
126 return (intCount + fpCount > maxAllRegisters);
127}
128
129bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
130 llvm::Type *eltTy,
131 unsigned numElts) const {
132 // The default implementation of this assumes that the target guarantees
133 // 128-bit SIMD support but nothing more.
134 return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16);
135}
136
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000137static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
Mark Lacey23630722013-10-06 01:33:34 +0000138 CGCXXABI &CXXABI) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000139 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
140 if (!RD)
141 return CGCXXABI::RAA_Default;
Mark Lacey23630722013-10-06 01:33:34 +0000142 return CXXABI.getRecordArgABI(RD);
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000143}
144
145static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
Mark Lacey23630722013-10-06 01:33:34 +0000146 CGCXXABI &CXXABI) {
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000147 const RecordType *RT = T->getAs<RecordType>();
148 if (!RT)
149 return CGCXXABI::RAA_Default;
Mark Lacey23630722013-10-06 01:33:34 +0000150 return getRecordArgABI(RT, CXXABI);
151}
152
Stephen Hines176edba2014-12-01 14:53:08 -0800153/// Pass transparent unions as if they were the type of the first element. Sema
154/// should ensure that all elements of the union have the same "machine type".
155static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
156 if (const RecordType *UT = Ty->getAsUnionType()) {
157 const RecordDecl *UD = UT->getDecl();
158 if (UD->hasAttr<TransparentUnionAttr>()) {
159 assert(!UD->field_empty() && "sema created an empty transparent union");
160 return UD->field_begin()->getType();
161 }
162 }
163 return Ty;
164}
165
Mark Lacey23630722013-10-06 01:33:34 +0000166CGCXXABI &ABIInfo::getCXXABI() const {
167 return CGT.getCXXABI();
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000168}
169
Chris Lattnerea044322010-07-29 02:01:43 +0000170ASTContext &ABIInfo::getContext() const {
171 return CGT.getContext();
172}
173
174llvm::LLVMContext &ABIInfo::getVMContext() const {
175 return CGT.getLLVMContext();
176}
177
Micah Villmow25a6a842012-10-08 16:25:52 +0000178const llvm::DataLayout &ABIInfo::getDataLayout() const {
179 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +0000180}
181
John McCall64aa4b32013-04-16 22:48:15 +0000182const TargetInfo &ABIInfo::getTarget() const {
183 return CGT.getTarget();
184}
Chris Lattnerea044322010-07-29 02:01:43 +0000185
Pirama Arumuga Nainar77f30022016-10-11 14:44:05 -0700186bool ABIInfo:: isAndroid() const {
187 return getTarget().getTriple().isAndroid() ||
188 getContext().getLangOpts().RenderScript;
189}
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700190
Stephen Hines176edba2014-12-01 14:53:08 -0800191bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
192 return false;
193}
194
195bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
196 uint64_t Members) const {
197 return false;
198}
199
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700200bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
201 return false;
202}
203
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700204LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000205 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000206 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000207 switch (TheKind) {
208 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000209 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +0000210 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +0000211 Ty->print(OS);
212 else
213 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000214 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000215 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000216 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000217 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000218 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000219 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000220 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700221 case InAlloca:
222 OS << "InAlloca Offset=" << getInAllocaFieldIndex();
223 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000224 case Indirect:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800225 OS << "Indirect Align=" << getIndirectAlign().getQuantity()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +0000226 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000227 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000228 break;
229 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000230 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000231 break;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700232 case CoerceAndExpand:
233 OS << "CoerceAndExpand Type=";
234 getCoerceAndExpandType()->print(OS);
235 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000236 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +0000237 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000238}
239
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800240// Dynamically round a pointer up to a multiple of the given alignment.
241static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
242 llvm::Value *Ptr,
243 CharUnits Align) {
244 llvm::Value *PtrAsInt = Ptr;
245 // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
246 PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
247 PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
248 llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
249 PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
250 llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
251 PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
252 Ptr->getType(),
253 Ptr->getName() + ".aligned");
254 return PtrAsInt;
255}
256
257/// Emit va_arg for a platform using the common void* representation,
258/// where arguments are simply emitted in an array of slots on the stack.
259///
260/// This version implements the core direct-value passing rules.
261///
262/// \param SlotSize - The size and alignment of a stack slot.
263/// Each argument will be allocated to a multiple of this number of
264/// slots, and all the slots will be aligned to this value.
265/// \param AllowHigherAlign - The slot alignment is not a cap;
266/// an argument type with an alignment greater than the slot size
267/// will be emitted on a higher-alignment address, potentially
268/// leaving one or more empty slots behind as padding. If this
269/// is false, the returned address might be less-aligned than
270/// DirectAlign.
271static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
272 Address VAListAddr,
273 llvm::Type *DirectTy,
274 CharUnits DirectSize,
275 CharUnits DirectAlign,
276 CharUnits SlotSize,
277 bool AllowHigherAlign) {
278 // Cast the element type to i8* if necessary. Some platforms define
279 // va_list as a struct containing an i8* instead of just an i8*.
280 if (VAListAddr.getElementType() != CGF.Int8PtrTy)
281 VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
282
283 llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
284
285 // If the CC aligns values higher than the slot size, do so if needed.
286 Address Addr = Address::invalid();
287 if (AllowHigherAlign && DirectAlign > SlotSize) {
288 Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
289 DirectAlign);
290 } else {
291 Addr = Address(Ptr, SlotSize);
292 }
293
294 // Advance the pointer past the argument, then store that back.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700295 CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800296 llvm::Value *NextPtr =
297 CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize,
298 "argp.next");
299 CGF.Builder.CreateStore(NextPtr, VAListAddr);
300
301 // If the argument is smaller than a slot, and this is a big-endian
302 // target, the argument will be right-adjusted in its slot.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700303 if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
304 !DirectTy->isStructTy()) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800305 Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
306 }
307
308 Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
309 return Addr;
310}
311
312/// Emit va_arg for a platform using the common void* representation,
313/// where arguments are simply emitted in an array of slots on the stack.
314///
315/// \param IsIndirect - Values of this type are passed indirectly.
316/// \param ValueInfo - The size and alignment of this type, generally
317/// computed with getContext().getTypeInfoInChars(ValueTy).
318/// \param SlotSizeAndAlign - The size and alignment of a stack slot.
319/// Each argument will be allocated to a multiple of this number of
320/// slots, and all the slots will be aligned to this value.
321/// \param AllowHigherAlign - The slot alignment is not a cap;
322/// an argument type with an alignment greater than the slot size
323/// will be emitted on a higher-alignment address, potentially
324/// leaving one or more empty slots behind as padding.
325static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
326 QualType ValueTy, bool IsIndirect,
327 std::pair<CharUnits, CharUnits> ValueInfo,
328 CharUnits SlotSizeAndAlign,
329 bool AllowHigherAlign) {
330 // The size and alignment of the value that was passed directly.
331 CharUnits DirectSize, DirectAlign;
332 if (IsIndirect) {
333 DirectSize = CGF.getPointerSize();
334 DirectAlign = CGF.getPointerAlign();
335 } else {
336 DirectSize = ValueInfo.first;
337 DirectAlign = ValueInfo.second;
338 }
339
340 // Cast the address we've calculated to the right type.
341 llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
342 if (IsIndirect)
343 DirectTy = DirectTy->getPointerTo(0);
344
345 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
346 DirectSize, DirectAlign,
347 SlotSizeAndAlign,
348 AllowHigherAlign);
349
350 if (IsIndirect) {
351 Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second);
352 }
353
354 return Addr;
355
356}
357
358static Address emitMergePHI(CodeGenFunction &CGF,
359 Address Addr1, llvm::BasicBlock *Block1,
360 Address Addr2, llvm::BasicBlock *Block2,
361 const llvm::Twine &Name = "") {
362 assert(Addr1.getType() == Addr2.getType());
363 llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
364 PHI->addIncoming(Addr1.getPointer(), Block1);
365 PHI->addIncoming(Addr2.getPointer(), Block2);
366 CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
367 return Address(PHI, Align);
368}
369
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000370TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
371
John McCall49e34be2011-08-30 01:42:09 +0000372// If someone can figure out a general rule for this, that would be great.
373// It's probably just doomed to be platform-dependent, though.
374unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
375 // Verified for:
376 // x86-64 FreeBSD, Linux, Darwin
377 // x86-32 FreeBSD, Linux, Darwin
378 // PowerPC Linux, Darwin
379 // ARM Darwin (*not* EABI)
Tim Northoverc264e162013-01-31 12:13:10 +0000380 // AArch64 Linux
John McCall49e34be2011-08-30 01:42:09 +0000381 return 32;
382}
383
John McCallde5d3c72012-02-17 03:33:10 +0000384bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
385 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000386 // The following conventions are known to require this to be false:
387 // x86_stdcall
388 // MIPS
389 // For everything else, we just prefer false unless we opt out.
390 return false;
391}
392
Reid Kleckner3190ca92013-05-08 13:44:39 +0000393void
394TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
395 llvm::SmallString<24> &Opt) const {
396 // This assumes the user is passing a library name like "rt" instead of a
397 // filename like "librt.a/so", and that they don't care whether it's static or
398 // dynamic.
399 Opt = "-l";
400 Opt += Lib;
401}
402
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700403unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
404 return llvm::CallingConv::C;
405}
Daniel Dunbar98303b92009-09-13 08:03:58 +0000406static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000407
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000408/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000409/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000410static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
411 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000412 if (FD->isUnnamedBitfield())
413 return true;
414
415 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000416
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000417 // Constant arrays of empty records count as empty, strip them off.
418 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000419 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000420 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
421 if (AT->getSize() == 0)
422 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000423 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000424 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000425
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000426 const RecordType *RT = FT->getAs<RecordType>();
427 if (!RT)
428 return false;
429
430 // C++ record fields are never empty, at least in the Itanium ABI.
431 //
432 // FIXME: We should use a predicate for whether this behavior is true in the
433 // current ABI.
434 if (isa<CXXRecordDecl>(RT->getDecl()))
435 return false;
436
Daniel Dunbar98303b92009-09-13 08:03:58 +0000437 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000438}
439
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000440/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000441/// fields. Note that a structure with a flexible array member is not
442/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000443static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000444 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000445 if (!RT)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700446 return false;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000447 const RecordDecl *RD = RT->getDecl();
448 if (RD->hasFlexibleArrayMember())
449 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000450
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000451 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000452 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Stephen Hines651f13c2014-04-23 16:59:28 -0700453 for (const auto &I : CXXRD->bases())
454 if (!isEmptyRecord(Context, I.getType(), true))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000455 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000456
Stephen Hines651f13c2014-04-23 16:59:28 -0700457 for (const auto *I : RD->fields())
458 if (!isEmptyField(Context, I, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000459 return false;
460 return true;
461}
462
463/// isSingleElementStruct - Determine if a structure is a "single
464/// element struct", i.e. it has exactly one non-empty field or
465/// exactly one field which is itself a single element
466/// struct. Structures with flexible array members are never
467/// considered single element structs.
468///
469/// \return The field declaration for the single non-empty field, if
470/// it exists.
471static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700472 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000473 if (!RT)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700474 return nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000475
476 const RecordDecl *RD = RT->getDecl();
477 if (RD->hasFlexibleArrayMember())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700478 return nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000479
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700480 const Type *Found = nullptr;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000481
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000482 // If this is a C++ record, check the bases first.
483 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700484 for (const auto &I : CXXRD->bases()) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000485 // Ignore empty records.
Stephen Hines651f13c2014-04-23 16:59:28 -0700486 if (isEmptyRecord(Context, I.getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000487 continue;
488
489 // If we already found an element then this isn't a single-element struct.
490 if (Found)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700491 return nullptr;
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000492
493 // If this is non-empty and not a single element struct, the composite
494 // cannot be a single element struct.
Stephen Hines651f13c2014-04-23 16:59:28 -0700495 Found = isSingleElementStruct(I.getType(), Context);
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000496 if (!Found)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700497 return nullptr;
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000498 }
499 }
500
501 // Check for single element.
Stephen Hines651f13c2014-04-23 16:59:28 -0700502 for (const auto *FD : RD->fields()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000503 QualType FT = FD->getType();
504
505 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000506 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000507 continue;
508
509 // If we already found an element then this isn't a single-element
510 // struct.
511 if (Found)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700512 return nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000513
514 // Treat single element arrays as the element.
515 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
516 if (AT->getSize().getZExtValue() != 1)
517 break;
518 FT = AT->getElementType();
519 }
520
John McCalld608cdb2010-08-22 10:59:02 +0000521 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000522 Found = FT.getTypePtr();
523 } else {
524 Found = isSingleElementStruct(FT, Context);
525 if (!Found)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700526 return nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000527 }
528 }
529
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000530 // We don't consider a struct a single-element struct if it has
531 // padding beyond the element type.
532 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700533 return nullptr;
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000534
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000535 return Found;
536}
537
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000538namespace {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700539Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
540 const ABIArgInfo &AI) {
541 // This default implementation defers to the llvm backend's va_arg
542 // instruction. It can handle only passing arguments directly
543 // (typically only handled in the backend for primitive types), or
544 // aggregates passed indirectly by pointer (NOTE: if the "byval"
545 // flag has ABI impact in the callee, this implementation cannot
546 // work.)
547
548 // Only a few cases are covered here at the moment -- those needed
549 // by the default abi.
550 llvm::Value *Val;
551
552 if (AI.isIndirect()) {
553 assert(!AI.getPaddingType() &&
554 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
555 assert(
556 !AI.getIndirectRealign() &&
557 "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
558
559 auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
560 CharUnits TyAlignForABI = TyInfo.second;
561
562 llvm::Type *BaseTy =
563 llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
564 llvm::Value *Addr =
565 CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
566 return Address(Addr, TyAlignForABI);
567 } else {
568 assert((AI.isDirect() || AI.isExtend()) &&
569 "Unexpected ArgInfo Kind in generic VAArg emitter!");
570
571 assert(!AI.getInReg() &&
572 "Unexpected InReg seen in arginfo in generic VAArg emitter!");
573 assert(!AI.getPaddingType() &&
574 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
575 assert(!AI.getDirectOffset() &&
576 "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
577 assert(!AI.getCoerceToType() &&
578 "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
579
580 Address Temp = CGF.CreateMemTemp(Ty, "varet");
581 Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
582 CGF.Builder.CreateStore(Val, Temp);
583 return Temp;
584 }
585}
586
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000587/// DefaultABIInfo - The default implementation for ABI specific
588/// details. This implementation provides information which results in
589/// self-consistent and sensible LLVM IR generation, but does not
590/// conform to any particular ABI.
591class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000592public:
593 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000594
Chris Lattnera3c109b2010-07-29 02:16:43 +0000595 ABIArgInfo classifyReturnType(QualType RetTy) const;
596 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000597
Stephen Hines651f13c2014-04-23 16:59:28 -0700598 void computeInfo(CGFunctionInfo &FI) const override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700599 if (!getCXXABI().classifyReturnType(FI))
600 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines651f13c2014-04-23 16:59:28 -0700601 for (auto &I : FI.arguments())
602 I.info = classifyArgumentType(I.type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000603 }
604
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800605 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700606 QualType Ty) const override {
607 return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
608 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000609};
610
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000611class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
612public:
Chris Lattnerea044322010-07-29 02:01:43 +0000613 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
614 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000615};
616
Chris Lattnera3c109b2010-07-29 02:16:43 +0000617ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700618 Ty = useFirstFieldIfTransparentUnion(Ty);
619
620 if (isAggregateTypeForABI(Ty)) {
621 // Records with non-trivial destructors/copy-constructors should not be
622 // passed by value.
623 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800624 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700625
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800626 return getNaturalAlignIndirect(Ty);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700627 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000628
Chris Lattnera14db752010-03-11 18:19:55 +0000629 // Treat an enum type as its underlying type.
630 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
631 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000632
Chris Lattnera14db752010-03-11 18:19:55 +0000633 return (Ty->isPromotableIntegerType() ?
634 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000635}
636
Bob Wilson0024f942011-01-10 23:54:17 +0000637ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
638 if (RetTy->isVoidType())
639 return ABIArgInfo::getIgnore();
640
641 if (isAggregateTypeForABI(RetTy))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800642 return getNaturalAlignIndirect(RetTy);
Bob Wilson0024f942011-01-10 23:54:17 +0000643
644 // Treat an enum type as its underlying type.
645 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
646 RetTy = EnumTy->getDecl()->getIntegerType();
647
648 return (RetTy->isPromotableIntegerType() ?
649 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
650}
651
Derek Schuff9ed63f82012-09-06 17:37:28 +0000652//===----------------------------------------------------------------------===//
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800653// WebAssembly ABI Implementation
654//
655// This is a very simple ABI that relies a lot on DefaultABIInfo.
656//===----------------------------------------------------------------------===//
657
658class WebAssemblyABIInfo final : public DefaultABIInfo {
659public:
660 explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT)
661 : DefaultABIInfo(CGT) {}
662
663private:
664 ABIArgInfo classifyReturnType(QualType RetTy) const;
665 ABIArgInfo classifyArgumentType(QualType Ty) const;
666
667 // DefaultABIInfo's classifyReturnType and classifyArgumentType are
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700668 // non-virtual, but computeInfo and EmitVAArg are virtual, so we
669 // overload them.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800670 void computeInfo(CGFunctionInfo &FI) const override {
671 if (!getCXXABI().classifyReturnType(FI))
672 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
673 for (auto &Arg : FI.arguments())
674 Arg.info = classifyArgumentType(Arg.type);
675 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700676
677 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
678 QualType Ty) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800679};
680
681class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
682public:
683 explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
684 : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
685};
686
687/// \brief Classify argument of given type \p Ty.
688ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
689 Ty = useFirstFieldIfTransparentUnion(Ty);
690
691 if (isAggregateTypeForABI(Ty)) {
692 // Records with non-trivial destructors/copy-constructors should not be
693 // passed by value.
694 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
695 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
696 // Ignore empty structs/unions.
697 if (isEmptyRecord(getContext(), Ty, true))
698 return ABIArgInfo::getIgnore();
699 // Lower single-element structs to just pass a regular value. TODO: We
700 // could do reasonable-size multiple-element structs too, using getExpand(),
701 // though watch out for things like bitfields.
702 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
703 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
704 }
705
706 // Otherwise just do the default thing.
707 return DefaultABIInfo::classifyArgumentType(Ty);
708}
709
710ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
711 if (isAggregateTypeForABI(RetTy)) {
712 // Records with non-trivial destructors/copy-constructors should not be
713 // returned by value.
714 if (!getRecordArgABI(RetTy, getCXXABI())) {
715 // Ignore empty structs/unions.
716 if (isEmptyRecord(getContext(), RetTy, true))
717 return ABIArgInfo::getIgnore();
718 // Lower single-element structs to just return a regular value. TODO: We
719 // could do reasonable-size multiple-element structs too, using
720 // ABIArgInfo::getDirect().
721 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
722 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
723 }
724 }
725
726 // Otherwise just do the default thing.
727 return DefaultABIInfo::classifyReturnType(RetTy);
728}
729
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700730Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
731 QualType Ty) const {
732 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false,
733 getContext().getTypeInfoInChars(Ty),
734 CharUnits::fromQuantity(4),
735 /*AllowHigherAlign=*/ true);
736}
737
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800738//===----------------------------------------------------------------------===//
Derek Schuff9ed63f82012-09-06 17:37:28 +0000739// le32/PNaCl bitcode ABI Implementation
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000740//
741// This is a simplified version of the x86_32 ABI. Arguments and return values
742// are always passed on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000743//===----------------------------------------------------------------------===//
744
745class PNaClABIInfo : public ABIInfo {
746 public:
747 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
748
749 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000750 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff9ed63f82012-09-06 17:37:28 +0000751
Stephen Hines651f13c2014-04-23 16:59:28 -0700752 void computeInfo(CGFunctionInfo &FI) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800753 Address EmitVAArg(CodeGenFunction &CGF,
754 Address VAListAddr, QualType Ty) const override;
Derek Schuff9ed63f82012-09-06 17:37:28 +0000755};
756
757class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
758 public:
759 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
760 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
761};
762
763void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700764 if (!getCXXABI().classifyReturnType(FI))
Derek Schuff9ed63f82012-09-06 17:37:28 +0000765 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
766
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700767 for (auto &I : FI.arguments())
768 I.info = classifyArgumentType(I.type);
769}
Derek Schuff9ed63f82012-09-06 17:37:28 +0000770
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800771Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
772 QualType Ty) const {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700773 // The PNaCL ABI is a bit odd, in that varargs don't use normal
774 // function classification. Structs get passed directly for varargs
775 // functions, through a rewriting transform in
776 // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
777 // this target to actually support a va_arg instructions with an
778 // aggregate type, unlike other targets.
779 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Derek Schuff9ed63f82012-09-06 17:37:28 +0000780}
781
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000782/// \brief Classify argument of given type \p Ty.
783ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff9ed63f82012-09-06 17:37:28 +0000784 if (isAggregateTypeForABI(Ty)) {
Mark Lacey23630722013-10-06 01:33:34 +0000785 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800786 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
787 return getNaturalAlignIndirect(Ty);
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000788 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
789 // Treat an enum type as its underlying type.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000790 Ty = EnumTy->getDecl()->getIntegerType();
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000791 } else if (Ty->isFloatingType()) {
792 // Floating-point types don't go inreg.
793 return ABIArgInfo::getDirect();
Derek Schuff9ed63f82012-09-06 17:37:28 +0000794 }
Eli Benderskyc0783dc2013-04-08 21:31:01 +0000795
796 return (Ty->isPromotableIntegerType() ?
797 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff9ed63f82012-09-06 17:37:28 +0000798}
799
800ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
801 if (RetTy->isVoidType())
802 return ABIArgInfo::getIgnore();
803
Eli Benderskye45dfd12013-04-04 22:49:35 +0000804 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff9ed63f82012-09-06 17:37:28 +0000805 if (isAggregateTypeForABI(RetTy))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800806 return getNaturalAlignIndirect(RetTy);
Derek Schuff9ed63f82012-09-06 17:37:28 +0000807
808 // Treat an enum type as its underlying type.
809 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
810 RetTy = EnumTy->getDecl()->getIntegerType();
811
812 return (RetTy->isPromotableIntegerType() ?
813 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
814}
815
Chad Rosier1f1df1f2013-03-25 21:00:27 +0000816/// IsX86_MMXType - Return true if this is an MMX type.
817bool IsX86_MMXType(llvm::Type *IRType) {
818 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
Bill Wendlingbb465d72010-10-18 03:41:31 +0000819 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
820 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
821 IRType->getScalarSizeInBits() != 64;
822}
823
Jay Foadef6de3d2011-07-11 09:56:20 +0000824static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000825 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000826 llvm::Type* Ty) {
Tim Northover1bea6532013-06-07 00:04:50 +0000827 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
828 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
829 // Invalid MMX constraint
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700830 return nullptr;
Tim Northover1bea6532013-06-07 00:04:50 +0000831 }
832
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000833 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover1bea6532013-06-07 00:04:50 +0000834 }
835
836 // No operation needed
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000837 return Ty;
838}
839
Stephen Hines176edba2014-12-01 14:53:08 -0800840/// Returns true if this type can be passed in SSE registers with the
841/// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
842static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
843 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
844 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half)
845 return true;
846 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
847 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
848 // registers specially.
849 unsigned VecSize = Context.getTypeSize(VT);
850 if (VecSize == 128 || VecSize == 256 || VecSize == 512)
851 return true;
852 }
853 return false;
854}
855
856/// Returns true if this aggregate is small enough to be passed in SSE registers
857/// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
858static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
859 return NumMembers <= 4;
860}
861
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000862//===----------------------------------------------------------------------===//
863// X86-32 ABI Implementation
864//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000865
Stephen Hines651f13c2014-04-23 16:59:28 -0700866/// \brief Similar to llvm::CCState, but for Clang.
867struct CCState {
Stephen Hines176edba2014-12-01 14:53:08 -0800868 CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
Stephen Hines651f13c2014-04-23 16:59:28 -0700869
870 unsigned CC;
871 unsigned FreeRegs;
Stephen Hines176edba2014-12-01 14:53:08 -0800872 unsigned FreeSSERegs;
Stephen Hines651f13c2014-04-23 16:59:28 -0700873};
874
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000875/// X86_32ABIInfo - The X86-32 ABI information.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700876class X86_32ABIInfo : public SwiftABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000877 enum Class {
878 Integer,
879 Float
880 };
881
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000882 static const unsigned MinABIStackAlignInBytes = 4;
883
David Chisnall1e4249c2009-08-17 23:08:21 +0000884 bool IsDarwinVectorABI;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800885 bool IsRetSmallStructInRegABI;
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +0000886 bool IsWin32StructABI;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800887 bool IsSoftFloatABI;
888 bool IsMCUABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000889 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000890
891 static bool isRegisterSize(unsigned Size) {
892 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
893 }
894
Stephen Hines176edba2014-12-01 14:53:08 -0800895 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
896 // FIXME: Assumes vectorcall is in use.
897 return isX86VectorTypeForVectorCall(getContext(), Ty);
898 }
899
900 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
901 uint64_t NumMembers) const override {
902 // FIXME: Assumes vectorcall is in use.
903 return isX86VectorCallAggregateSmallEnough(NumMembers);
904 }
905
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700906 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000907
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000908 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
909 /// such that the argument will be passed in memory.
Stephen Hines651f13c2014-04-23 16:59:28 -0700910 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
911
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800912 ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000913
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000914 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000915 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000916
Rafael Espindolab48280b2012-07-31 02:44:24 +0000917 Class classify(QualType Ty) const;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700918 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
Stephen Hines651f13c2014-04-23 16:59:28 -0700919 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700920 /// \brief Updates the number of available free registers, returns
921 /// true if any registers were allocated.
922 bool updateFreeRegs(QualType Ty, CCState &State) const;
923
924 bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
925 bool &NeedsPadding) const;
926 bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
927
928 bool canExpandIndirectArgument(QualType Ty) const;
Stephen Hines651f13c2014-04-23 16:59:28 -0700929
930 /// \brief Rewrite the function info so that all memory arguments use
931 /// inalloca.
932 void rewriteWithInAlloca(CGFunctionInfo &FI) const;
933
934 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800935 CharUnits &StackOffset, ABIArgInfo &Info,
Stephen Hines651f13c2014-04-23 16:59:28 -0700936 QualType Type) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000937
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000938public:
939
Stephen Hines651f13c2014-04-23 16:59:28 -0700940 void computeInfo(CGFunctionInfo &FI) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800941 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
942 QualType Ty) const override;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000943
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800944 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
945 bool RetSmallStructInRegABI, bool Win32StructABI,
946 unsigned NumRegisterParameters, bool SoftFloatABI)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700947 : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800948 IsRetSmallStructInRegABI(RetSmallStructInRegABI),
949 IsWin32StructABI(Win32StructABI),
950 IsSoftFloatABI(SoftFloatABI),
951 IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
952 DefaultNumRegisterParameters(NumRegisterParameters) {}
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -0700953
954 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
955 ArrayRef<llvm::Type*> scalars,
956 bool asReturnValue) const override {
957 // LLVM's x86-32 lowering currently only assigns up to three
958 // integer registers and three fp registers. Oddly, it'll use up to
959 // four vector registers for vectors, but those can overlap with the
960 // scalar registers.
961 return occupiesMoreThan(CGT, scalars, /*total*/ 3);
962 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000963};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000964
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000965class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
966public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -0800967 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
968 bool RetSmallStructInRegABI, bool Win32StructABI,
969 unsigned NumRegisterParameters, bool SoftFloatABI)
970 : TargetCodeGenInfo(new X86_32ABIInfo(
971 CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
972 NumRegisterParameters, SoftFloatABI)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000973
John McCallb8b52972013-06-18 02:46:29 +0000974 static bool isStructReturnInRegABI(
975 const llvm::Triple &Triple, const CodeGenOptions &Opts);
976
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -0700977 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -0700978 CodeGen::CodeGenModule &CGM) const override;
John McCall6374c332010-03-06 00:35:14 +0000979
Stephen Hines651f13c2014-04-23 16:59:28 -0700980 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCall6374c332010-03-06 00:35:14 +0000981 // Darwin uses different dwarf register numbers for EH.
John McCall64aa4b32013-04-16 22:48:15 +0000982 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCall6374c332010-03-06 00:35:14 +0000983 return 4;
984 }
985
986 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -0700987 llvm::Value *Address) const override;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000988
Jay Foadef6de3d2011-07-11 09:56:20 +0000989 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000990 StringRef Constraint,
Stephen Hines651f13c2014-04-23 16:59:28 -0700991 llvm::Type* Ty) const override {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000992 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
993 }
994
Stephen Hines176edba2014-12-01 14:53:08 -0800995 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
996 std::string &Constraints,
997 std::vector<llvm::Type *> &ResultRegTypes,
998 std::vector<llvm::Type *> &ResultTruncRegTypes,
999 std::vector<LValue> &ResultRegDests,
1000 std::string &AsmString,
1001 unsigned NumOutputs) const override;
1002
Stephen Hines651f13c2014-04-23 16:59:28 -07001003 llvm::Constant *
1004 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Peter Collingbourneb914e872013-10-20 21:29:19 +00001005 unsigned Sig = (0xeb << 0) | // jmp rel8
1006 (0x06 << 8) | // .+0x08
1007 ('F' << 16) |
1008 ('T' << 24);
1009 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1010 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001011
1012 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
1013 return "movl\t%ebp, %ebp"
1014 "\t\t## marker for objc_retainAutoreleaseReturnValue";
1015 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001016};
1017
1018}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001019
Stephen Hines176edba2014-12-01 14:53:08 -08001020/// Rewrite input constraint references after adding some output constraints.
1021/// In the case where there is one output and one input and we add one output,
1022/// we need to replace all operand references greater than or equal to 1:
1023/// mov $0, $1
1024/// mov eax, $1
1025/// The result will be:
1026/// mov $0, $2
1027/// mov eax, $2
1028static void rewriteInputConstraintReferences(unsigned FirstIn,
1029 unsigned NumNewOuts,
1030 std::string &AsmString) {
1031 std::string Buf;
1032 llvm::raw_string_ostream OS(Buf);
1033 size_t Pos = 0;
1034 while (Pos < AsmString.size()) {
1035 size_t DollarStart = AsmString.find('$', Pos);
1036 if (DollarStart == std::string::npos)
1037 DollarStart = AsmString.size();
1038 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1039 if (DollarEnd == std::string::npos)
1040 DollarEnd = AsmString.size();
1041 OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1042 Pos = DollarEnd;
1043 size_t NumDollars = DollarEnd - DollarStart;
1044 if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1045 // We have an operand reference.
1046 size_t DigitStart = Pos;
1047 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1048 if (DigitEnd == std::string::npos)
1049 DigitEnd = AsmString.size();
1050 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1051 unsigned OperandIndex;
1052 if (!OperandStr.getAsInteger(10, OperandIndex)) {
1053 if (OperandIndex >= FirstIn)
1054 OperandIndex += NumNewOuts;
1055 OS << OperandIndex;
1056 } else {
1057 OS << OperandStr;
1058 }
1059 Pos = DigitEnd;
1060 }
1061 }
1062 AsmString = std::move(OS.str());
1063}
1064
1065/// Add output constraints for EAX:EDX because they are return registers.
1066void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1067 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1068 std::vector<llvm::Type *> &ResultRegTypes,
1069 std::vector<llvm::Type *> &ResultTruncRegTypes,
1070 std::vector<LValue> &ResultRegDests, std::string &AsmString,
1071 unsigned NumOutputs) const {
1072 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1073
1074 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1075 // larger.
1076 if (!Constraints.empty())
1077 Constraints += ',';
1078 if (RetWidth <= 32) {
1079 Constraints += "={eax}";
1080 ResultRegTypes.push_back(CGF.Int32Ty);
1081 } else {
1082 // Use the 'A' constraint for EAX:EDX.
1083 Constraints += "=A";
1084 ResultRegTypes.push_back(CGF.Int64Ty);
1085 }
1086
1087 // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1088 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1089 ResultTruncRegTypes.push_back(CoerceTy);
1090
1091 // Coerce the integer by bitcasting the return slot pointer.
1092 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
1093 CoerceTy->getPointerTo()));
1094 ResultRegDests.push_back(ReturnSlot);
1095
1096 rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1097}
1098
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001099/// shouldReturnTypeInRegister - Determine if the given type should be
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001100/// returned in a register (for the Darwin and MCU ABI).
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001101bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1102 ASTContext &Context) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001103 uint64_t Size = Context.getTypeSize(Ty);
1104
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001105 // For i386, type must be register sized.
1106 // For the MCU ABI, it only needs to be <= 8-byte
1107 if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1108 return false;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001109
1110 if (Ty->isVectorType()) {
1111 // 64- and 128- bit vectors inside structures are not returned in
1112 // registers.
1113 if (Size == 64 || Size == 128)
1114 return false;
1115
1116 return true;
1117 }
1118
Daniel Dunbar77115232010-05-15 00:00:30 +00001119 // If this is a builtin, pointer, enum, complex type, member pointer, or
1120 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +00001121 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +00001122 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +00001123 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001124 return true;
1125
1126 // Arrays are treated like records.
1127 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001128 return shouldReturnTypeInRegister(AT->getElementType(), Context);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001129
1130 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +00001131 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001132 if (!RT) return false;
1133
Anders Carlssona8874232010-01-27 03:25:19 +00001134 // FIXME: Traverse bases here too.
1135
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001136 // Structure types are passed in register if all fields would be
1137 // passed in a register.
Stephen Hines651f13c2014-04-23 16:59:28 -07001138 for (const auto *FD : RT->getDecl()->fields()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001139 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +00001140 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001141 continue;
1142
1143 // Check fields recursively.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001144 if (!shouldReturnTypeInRegister(FD->getType(), Context))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001145 return false;
1146 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001147 return true;
1148}
1149
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001150static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
1151 // Treat complex types as the element type.
1152 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
1153 Ty = CTy->getElementType();
1154
1155 // Check for a type which we know has a simple scalar argument-passing
1156 // convention without any padding. (We're specifically looking for 32
1157 // and 64-bit integer and integer-equivalents, float, and double.)
1158 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
1159 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
1160 return false;
1161
1162 uint64_t Size = Context.getTypeSize(Ty);
1163 return Size == 32 || Size == 64;
1164}
1165
1166/// Test whether an argument type which is to be passed indirectly (on the
1167/// stack) would have the equivalent layout if it was expanded into separate
1168/// arguments. If so, we prefer to do the latter to avoid inhibiting
1169/// optimizations.
1170bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
1171 // We can only expand structure types.
1172 const RecordType *RT = Ty->getAs<RecordType>();
1173 if (!RT)
1174 return false;
1175 const RecordDecl *RD = RT->getDecl();
1176 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1177 if (!IsWin32StructABI ) {
1178 // On non-Windows, we have to conservatively match our old bitcode
1179 // prototypes in order to be ABI-compatible at the bitcode level.
1180 if (!CXXRD->isCLike())
1181 return false;
1182 } else {
1183 // Don't do this for dynamic classes.
1184 if (CXXRD->isDynamicClass())
1185 return false;
1186 // Don't do this if there are any non-empty bases.
1187 for (const CXXBaseSpecifier &Base : CXXRD->bases()) {
1188 if (!isEmptyRecord(getContext(), Base.getType(), /*AllowArrays=*/true))
1189 return false;
1190 }
1191 }
1192 }
1193
1194 uint64_t Size = 0;
1195
1196 for (const auto *FD : RD->fields()) {
1197 // Scalar arguments on the stack get 4 byte alignment on x86. If the
1198 // argument is smaller than 32-bits, expanding the struct will create
1199 // alignment padding.
1200 if (!is32Or64BitBasicType(FD->getType(), getContext()))
1201 return false;
1202
1203 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
1204 // how to expand them yet, and the predicate for telling if a bitfield still
1205 // counts as "basic" is more complicated than what we were doing previously.
1206 if (FD->isBitField())
1207 return false;
1208
1209 Size += getContext().getTypeSize(FD->getType());
1210 }
1211
1212 // We can do this if there was no alignment padding.
1213 return Size == getContext().getTypeSize(Ty);
1214}
1215
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001216ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001217 // If the return value is indirect, then the hidden argument is consuming one
1218 // integer register.
1219 if (State.FreeRegs) {
1220 --State.FreeRegs;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001221 if (!IsMCUABI)
1222 return getNaturalAlignIndirectInReg(RetTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07001223 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001224 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001225}
1226
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001227ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1228 CCState &State) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00001229 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001230 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001231
Stephen Hines176edba2014-12-01 14:53:08 -08001232 const Type *Base = nullptr;
1233 uint64_t NumElts = 0;
1234 if (State.CC == llvm::CallingConv::X86_VectorCall &&
1235 isHomogeneousAggregate(RetTy, Base, NumElts)) {
1236 // The LLVM struct type for such an aggregate should lower properly.
1237 return ABIArgInfo::getDirect();
1238 }
1239
Chris Lattnera3c109b2010-07-29 02:16:43 +00001240 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001241 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +00001242 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00001243 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001244
1245 // 128-bit vectors are a special case; they are returned in
1246 // registers and we need to make sure to pick a type the LLVM
1247 // backend will like.
1248 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +00001249 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +00001250 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001251
1252 // Always return in register if it fits in a general purpose
1253 // register, or if it is 64 bits and has a single element.
1254 if ((Size == 8 || Size == 16 || Size == 32) ||
1255 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +00001256 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00001257 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001258
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001259 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001260 }
1261
1262 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +00001263 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001264
John McCalld608cdb2010-08-22 10:59:02 +00001265 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +00001266 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +00001267 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001268 if (RT->getDecl()->hasFlexibleArrayMember())
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001269 return getIndirectReturnResult(RetTy, State);
Anders Carlsson40092972009-10-20 22:07:59 +00001270 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001271
David Chisnall1e4249c2009-08-17 23:08:21 +00001272 // If specified, structs and unions are always indirect.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001273 if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
1274 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001275
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001276 // Ignore empty structs/unions.
1277 if (isEmptyRecord(getContext(), RetTy, true))
1278 return ABIArgInfo::getIgnore();
1279
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001280 // Small structures which are register sized are generally returned
1281 // in a register.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001282 if (shouldReturnTypeInRegister(RetTy, getContext())) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00001283 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +00001284
1285 // As a special-case, if the struct is a "single-element" struct, and
1286 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +00001287 // floating-point register. (MSVC does not apply this special case.)
1288 // We apply a similar transformation for pointer types to improve the
1289 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +00001290 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001291 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedman55fc7e22012-01-25 22:46:34 +00001292 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +00001293 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1294
1295 // FIXME: We should be able to narrow this integer in cases with dead
1296 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +00001297 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001298 }
1299
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001300 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001301 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001302
Chris Lattnera3c109b2010-07-29 02:16:43 +00001303 // Treat an enum type as its underlying type.
1304 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1305 RetTy = EnumTy->getDecl()->getIntegerType();
1306
1307 return (RetTy->isPromotableIntegerType() ?
1308 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001309}
1310
Eli Friedmanf4bd4d82012-06-05 19:40:46 +00001311static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
1312 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1313}
1314
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001315static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
1316 const RecordType *RT = Ty->getAs<RecordType>();
1317 if (!RT)
1318 return 0;
1319 const RecordDecl *RD = RT->getDecl();
1320
1321 // If this is a C++ record, check the bases first.
1322 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Stephen Hines651f13c2014-04-23 16:59:28 -07001323 for (const auto &I : CXXRD->bases())
1324 if (!isRecordWithSSEVectorType(Context, I.getType()))
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001325 return false;
1326
Stephen Hines651f13c2014-04-23 16:59:28 -07001327 for (const auto *i : RD->fields()) {
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001328 QualType FT = i->getType();
1329
Eli Friedmanf4bd4d82012-06-05 19:40:46 +00001330 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001331 return true;
1332
1333 if (isRecordWithSSEVectorType(Context, FT))
1334 return true;
1335 }
1336
1337 return false;
1338}
1339
Daniel Dunbare59d8582010-09-16 20:42:06 +00001340unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1341 unsigned Align) const {
1342 // Otherwise, if the alignment is less than or equal to the minimum ABI
1343 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +00001344 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +00001345 return 0; // Use default alignment.
1346
1347 // On non-Darwin, the stack type alignment is always 4.
1348 if (!IsDarwinVectorABI) {
1349 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +00001350 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +00001351 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +00001352
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001353 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +00001354 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
1355 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +00001356 return 16;
1357
1358 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +00001359}
1360
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001361ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
Stephen Hines651f13c2014-04-23 16:59:28 -07001362 CCState &State) const {
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001363 if (!ByVal) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001364 if (State.FreeRegs) {
1365 --State.FreeRegs; // Non-byval indirects just use one pointer.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001366 if (!IsMCUABI)
1367 return getNaturalAlignIndirectInReg(Ty);
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001368 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001369 return getNaturalAlignIndirect(Ty, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001370 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001371
Daniel Dunbare59d8582010-09-16 20:42:06 +00001372 // Compute the byval alignment.
1373 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1374 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1375 if (StackAlign == 0)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001376 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
Daniel Dunbare59d8582010-09-16 20:42:06 +00001377
1378 // If the stack alignment is less than the type alignment, realign the
1379 // argument.
Stephen Hines651f13c2014-04-23 16:59:28 -07001380 bool Realign = TypeAlign > StackAlign;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001381 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1382 /*ByVal=*/true, Realign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +00001383}
1384
Rafael Espindolab48280b2012-07-31 02:44:24 +00001385X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1386 const Type *T = isSingleElementStruct(Ty, getContext());
1387 if (!T)
1388 T = Ty.getTypePtr();
1389
1390 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1391 BuiltinType::Kind K = BT->getKind();
1392 if (K == BuiltinType::Float || K == BuiltinType::Double)
1393 return Float;
1394 }
1395 return Integer;
1396}
1397
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001398bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001399 if (!IsSoftFloatABI) {
1400 Class C = classify(Ty);
1401 if (C == Float)
1402 return false;
1403 }
Rafael Espindolab48280b2012-07-31 02:44:24 +00001404
Rafael Espindolab6932692012-10-24 01:58:58 +00001405 unsigned Size = getContext().getTypeSize(Ty);
1406 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +00001407
1408 if (SizeInRegs == 0)
1409 return false;
1410
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001411 if (!IsMCUABI) {
1412 if (SizeInRegs > State.FreeRegs) {
1413 State.FreeRegs = 0;
1414 return false;
1415 }
1416 } else {
1417 // The MCU psABI allows passing parameters in-reg even if there are
1418 // earlier parameters that are passed on the stack. Also,
1419 // it does not allow passing >8-byte structs in-register,
1420 // even if there are 3 free registers available.
1421 if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1422 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +00001423 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001424
Stephen Hines651f13c2014-04-23 16:59:28 -07001425 State.FreeRegs -= SizeInRegs;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001426 return true;
1427}
1428
1429bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1430 bool &InReg,
1431 bool &NeedsPadding) const {
1432 // On Windows, aggregates other than HFAs are never passed in registers, and
1433 // they do not consume register slots. Homogenous floating-point aggregates
1434 // (HFAs) have already been dealt with at this point.
1435 if (IsWin32StructABI && isAggregateTypeForABI(Ty))
1436 return false;
1437
1438 NeedsPadding = false;
1439 InReg = !IsMCUABI;
1440
1441 if (!updateFreeRegs(Ty, State))
1442 return false;
1443
1444 if (IsMCUABI)
1445 return true;
Rafael Espindolab6932692012-10-24 01:58:58 +00001446
Stephen Hines176edba2014-12-01 14:53:08 -08001447 if (State.CC == llvm::CallingConv::X86_FastCall ||
1448 State.CC == llvm::CallingConv::X86_VectorCall) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001449 if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +00001450 NeedsPadding = true;
1451
Rafael Espindolab6932692012-10-24 01:58:58 +00001452 return false;
1453 }
1454
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001455 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +00001456}
1457
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001458bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1459 if (!updateFreeRegs(Ty, State))
1460 return false;
1461
1462 if (IsMCUABI)
1463 return false;
1464
1465 if (State.CC == llvm::CallingConv::X86_FastCall ||
1466 State.CC == llvm::CallingConv::X86_VectorCall) {
1467 if (getContext().getTypeSize(Ty) > 32)
1468 return false;
1469
1470 return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1471 Ty->isReferenceType());
1472 }
1473
1474 return true;
1475}
1476
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001477ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Stephen Hines651f13c2014-04-23 16:59:28 -07001478 CCState &State) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001479 // FIXME: Set alignment on indirect arguments.
Daniel Dunbardc6d5742010-04-21 19:10:51 +00001480
Stephen Hines176edba2014-12-01 14:53:08 -08001481 Ty = useFirstFieldIfTransparentUnion(Ty);
1482
1483 // Check with the C++ ABI first.
1484 const RecordType *RT = Ty->getAs<RecordType>();
1485 if (RT) {
1486 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1487 if (RAA == CGCXXABI::RAA_Indirect) {
1488 return getIndirectResult(Ty, false, State);
1489 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1490 // The field index doesn't matter, we'll fix it up later.
1491 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1492 }
1493 }
1494
1495 // vectorcall adds the concept of a homogenous vector aggregate, similar
1496 // to other targets.
1497 const Type *Base = nullptr;
1498 uint64_t NumElts = 0;
1499 if (State.CC == llvm::CallingConv::X86_VectorCall &&
1500 isHomogeneousAggregate(Ty, Base, NumElts)) {
1501 if (State.FreeSSERegs >= NumElts) {
1502 State.FreeSSERegs -= NumElts;
1503 if (Ty->isBuiltinType() || Ty->isVectorType())
1504 return ABIArgInfo::getDirect();
1505 return ABIArgInfo::getExpand();
1506 }
1507 return getIndirectResult(Ty, /*ByVal=*/false, State);
1508 }
1509
1510 if (isAggregateTypeForABI(Ty)) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001511 // Structures with flexible arrays are always indirect.
1512 // FIXME: This should not be byval!
1513 if (RT && RT->getDecl()->hasFlexibleArrayMember())
1514 return getIndirectResult(Ty, true, State);
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00001515
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001516 // Ignore empty structs/unions on non-Windows.
1517 if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001518 return ABIArgInfo::getIgnore();
1519
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +00001520 llvm::LLVMContext &LLVMContext = getVMContext();
1521 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001522 bool NeedsPadding = false;
1523 bool InReg;
1524 if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001525 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Craig Topperb9bad792013-07-08 04:47:18 +00001526 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001527 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001528 if (InReg)
1529 return ABIArgInfo::getDirectInReg(Result);
1530 else
1531 return ABIArgInfo::getDirect(Result);
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001532 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001533 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001534
Daniel Dunbar53012f42009-11-09 01:33:53 +00001535 // Expand small (<= 128-bit) record types when we know that the stack layout
1536 // of those arguments will match the struct. This is important because the
1537 // LLVM backend isn't smart enough to remove byval, which inhibits many
1538 // optimizations.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001539 // Don't do this for the MCU if there are still free integer registers
1540 // (see X86_64 ABI for full explanation).
1541 if (getContext().getTypeSize(Ty) <= 4 * 32 &&
1542 (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty))
Stephen Hines651f13c2014-04-23 16:59:28 -07001543 return ABIArgInfo::getExpandWithPadding(
Stephen Hines176edba2014-12-01 14:53:08 -08001544 State.CC == llvm::CallingConv::X86_FastCall ||
1545 State.CC == llvm::CallingConv::X86_VectorCall,
1546 PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001547
Stephen Hines651f13c2014-04-23 16:59:28 -07001548 return getIndirectResult(Ty, true, State);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001549 }
1550
Chris Lattnerbbae8b42010-08-26 20:05:13 +00001551 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +00001552 // On Darwin, some vectors are passed in memory, we handle this by passing
1553 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +00001554 if (IsDarwinVectorABI) {
1555 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +00001556 if ((Size == 8 || Size == 16 || Size == 32) ||
1557 (Size == 64 && VT->getNumElements() == 1))
1558 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1559 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +00001560 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001561
Chad Rosier1f1df1f2013-03-25 21:00:27 +00001562 if (IsX86_MMXType(CGT.ConvertType(Ty)))
1563 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001564
Chris Lattnerbbae8b42010-08-26 20:05:13 +00001565 return ABIArgInfo::getDirect();
1566 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001567
1568
Chris Lattnera3c109b2010-07-29 02:16:43 +00001569 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1570 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001571
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001572 bool InReg = shouldPrimitiveUseInReg(Ty, State);
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001573
1574 if (Ty->isPromotableIntegerType()) {
1575 if (InReg)
1576 return ABIArgInfo::getExtendInReg();
1577 return ABIArgInfo::getExtend();
1578 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001579
Rafael Espindola0b4cc952012-10-19 05:04:37 +00001580 if (InReg)
1581 return ABIArgInfo::getDirectInReg();
1582 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001583}
1584
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +00001585void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001586 CCState State(FI.getCallingConvention());
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001587 if (IsMCUABI)
1588 State.FreeRegs = 3;
1589 else if (State.CC == llvm::CallingConv::X86_FastCall)
Stephen Hines651f13c2014-04-23 16:59:28 -07001590 State.FreeRegs = 2;
Stephen Hines176edba2014-12-01 14:53:08 -08001591 else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1592 State.FreeRegs = 2;
1593 State.FreeSSERegs = 6;
1594 } else if (FI.getHasRegParm())
Stephen Hines651f13c2014-04-23 16:59:28 -07001595 State.FreeRegs = FI.getRegParm();
Rafael Espindolab6932692012-10-24 01:58:58 +00001596 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001597 State.FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +00001598
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001599 if (!getCXXABI().classifyReturnType(FI)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001600 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001601 } else if (FI.getReturnInfo().isIndirect()) {
1602 // The C++ ABI is not aware of register usage, so we have to check if the
1603 // return value was sret and put it in a register ourselves if appropriate.
1604 if (State.FreeRegs) {
1605 --State.FreeRegs; // The sret parameter consumes a register.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001606 if (!IsMCUABI)
1607 FI.getReturnInfo().setInReg(true);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001608 }
1609 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001610
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001611 // The chain argument effectively gives us another free register.
1612 if (FI.isChainCall())
1613 ++State.FreeRegs;
1614
Stephen Hines651f13c2014-04-23 16:59:28 -07001615 bool UsedInAlloca = false;
1616 for (auto &I : FI.arguments()) {
1617 I.info = classifyArgumentType(I.type, State);
1618 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
Rafael Espindolab48280b2012-07-31 02:44:24 +00001619 }
1620
Stephen Hines651f13c2014-04-23 16:59:28 -07001621 // If we needed to use inalloca for any argument, do a second pass and rewrite
1622 // all the memory arguments to use inalloca.
1623 if (UsedInAlloca)
1624 rewriteWithInAlloca(FI);
1625}
1626
1627void
1628X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001629 CharUnits &StackOffset, ABIArgInfo &Info,
1630 QualType Type) const {
1631 // Arguments are always 4-byte-aligned.
1632 CharUnits FieldAlign = CharUnits::fromQuantity(4);
1633
1634 assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001635 Info = ABIArgInfo::getInAlloca(FrameFields.size());
1636 FrameFields.push_back(CGT.ConvertTypeForMem(Type));
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001637 StackOffset += getContext().getTypeSizeInChars(Type);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001638
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001639 // Insert padding bytes to respect alignment.
1640 CharUnits FieldEnd = StackOffset;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001641 StackOffset = FieldEnd.alignTo(FieldAlign);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001642 if (StackOffset != FieldEnd) {
1643 CharUnits NumBytes = StackOffset - FieldEnd;
Stephen Hines651f13c2014-04-23 16:59:28 -07001644 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001645 Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
Stephen Hines651f13c2014-04-23 16:59:28 -07001646 FrameFields.push_back(Ty);
1647 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001648}
1649
Stephen Hines176edba2014-12-01 14:53:08 -08001650static bool isArgInAlloca(const ABIArgInfo &Info) {
1651 // Leave ignored and inreg arguments alone.
1652 switch (Info.getKind()) {
1653 case ABIArgInfo::InAlloca:
1654 return true;
1655 case ABIArgInfo::Indirect:
1656 assert(Info.getIndirectByVal());
1657 return true;
1658 case ABIArgInfo::Ignore:
1659 return false;
1660 case ABIArgInfo::Direct:
1661 case ABIArgInfo::Extend:
Stephen Hines176edba2014-12-01 14:53:08 -08001662 if (Info.getInReg())
1663 return false;
1664 return true;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001665 case ABIArgInfo::Expand:
1666 case ABIArgInfo::CoerceAndExpand:
1667 // These are aggregate types which are never passed in registers when
1668 // inalloca is involved.
1669 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001670 }
1671 llvm_unreachable("invalid enum");
1672}
1673
Stephen Hines651f13c2014-04-23 16:59:28 -07001674void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1675 assert(IsWin32StructABI && "inalloca only supported on win32");
1676
1677 // Build a packed struct type for all of the arguments in memory.
1678 SmallVector<llvm::Type *, 6> FrameFields;
1679
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001680 // The stack alignment is always 4.
1681 CharUnits StackAlign = CharUnits::fromQuantity(4);
1682
1683 CharUnits StackOffset;
Stephen Hines176edba2014-12-01 14:53:08 -08001684 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1685
1686 // Put 'this' into the struct before 'sret', if necessary.
1687 bool IsThisCall =
1688 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
1689 ABIArgInfo &Ret = FI.getReturnInfo();
1690 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
1691 isArgInAlloca(I->info)) {
1692 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1693 ++I;
1694 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001695
1696 // Put the sret parameter into the inalloca struct if it's in memory.
Stephen Hines651f13c2014-04-23 16:59:28 -07001697 if (Ret.isIndirect() && !Ret.getInReg()) {
1698 CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1699 addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
1700 // On Windows, the hidden sret parameter is always returned in eax.
1701 Ret.setInAllocaSRet(IsWin32StructABI);
1702 }
1703
1704 // Skip the 'this' parameter in ecx.
Stephen Hines176edba2014-12-01 14:53:08 -08001705 if (IsThisCall)
Stephen Hines651f13c2014-04-23 16:59:28 -07001706 ++I;
1707
1708 // Put arguments passed in memory into the struct.
1709 for (; I != E; ++I) {
Stephen Hines176edba2014-12-01 14:53:08 -08001710 if (isArgInAlloca(I->info))
1711 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
Stephen Hines651f13c2014-04-23 16:59:28 -07001712 }
1713
1714 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001715 /*isPacked=*/true),
1716 StackAlign);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +00001717}
1718
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001719Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
1720 Address VAListAddr, QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001721
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001722 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
Eli Friedman7b1fb812011-11-18 02:12:09 +00001723
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001724 // x86-32 changes the alignment of certain arguments on the stack.
1725 //
1726 // Just messing with TypeInfo like this works because we never pass
1727 // anything indirectly.
1728 TypeInfo.second = CharUnits::fromQuantity(
1729 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
Eli Friedman7b1fb812011-11-18 02:12:09 +00001730
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001731 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
1732 TypeInfo, CharUnits::fromQuantity(4),
1733 /*AllowHigherAlign*/ true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001734}
1735
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001736bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
1737 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
1738 assert(Triple.getArch() == llvm::Triple::x86);
1739
1740 switch (Opts.getStructReturnConvention()) {
1741 case CodeGenOptions::SRCK_Default:
1742 break;
1743 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
1744 return false;
1745 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
1746 return true;
1747 }
1748
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001749 if (Triple.isOSDarwin() || Triple.isOSIAMCU())
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001750 return true;
1751
1752 switch (Triple.getOS()) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001753 case llvm::Triple::DragonFly:
1754 case llvm::Triple::FreeBSD:
1755 case llvm::Triple::OpenBSD:
1756 case llvm::Triple::Bitrig:
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001757 case llvm::Triple::Win32:
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001758 return true;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001759 default:
1760 return false;
1761 }
1762}
1763
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07001764void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Charles Davis74f72932010-02-13 15:54:06 +00001765 llvm::GlobalValue *GV,
1766 CodeGen::CodeGenModule &CGM) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001767 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Charles Davis74f72932010-02-13 15:54:06 +00001768 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1769 // Get the LLVM function.
1770 llvm::Function *Fn = cast<llvm::Function>(GV);
1771
1772 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001773 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001774 B.addStackAlignmentAttr(16);
Bill Wendling909b6de2013-01-23 00:21:06 +00001775 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1776 llvm::AttributeSet::get(CGM.getLLVMContext(),
1777 llvm::AttributeSet::FunctionIndex,
1778 B));
Charles Davis74f72932010-02-13 15:54:06 +00001779 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001780 if (FD->hasAttr<AnyX86InterruptAttr>()) {
1781 llvm::Function *Fn = cast<llvm::Function>(GV);
1782 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
1783 }
Charles Davis74f72932010-02-13 15:54:06 +00001784 }
1785}
1786
John McCall6374c332010-03-06 00:35:14 +00001787bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1788 CodeGen::CodeGenFunction &CGF,
1789 llvm::Value *Address) const {
1790 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001791
Chris Lattner8b418682012-02-07 00:39:47 +00001792 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001793
John McCall6374c332010-03-06 00:35:14 +00001794 // 0-7 are the eight integer registers; the order is different
1795 // on Darwin (for EH), but the range is the same.
1796 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001797 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001798
John McCall64aa4b32013-04-16 22:48:15 +00001799 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCall6374c332010-03-06 00:35:14 +00001800 // 12-16 are st(0..4). Not sure why we stop at 4.
1801 // These have size 16, which is sizeof(long double) on
1802 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001803 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001804 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001805
John McCall6374c332010-03-06 00:35:14 +00001806 } else {
1807 // 9 is %eflags, which doesn't get a size on Darwin for some
1808 // reason.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001809 Builder.CreateAlignedStore(
1810 Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
1811 CharUnits::One());
John McCall6374c332010-03-06 00:35:14 +00001812
1813 // 11-16 are st(0..5). Not sure why we stop at 5.
1814 // These have size 12, which is sizeof(long double) on
1815 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001816 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001817 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1818 }
John McCall6374c332010-03-06 00:35:14 +00001819
1820 return false;
1821}
1822
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001823//===----------------------------------------------------------------------===//
1824// X86-64 ABI Implementation
1825//===----------------------------------------------------------------------===//
1826
1827
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001828namespace {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001829/// The AVX ABI level for X86 targets.
1830enum class X86AVXABILevel {
1831 None,
1832 AVX,
1833 AVX512
1834};
1835
1836/// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
1837static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
1838 switch (AVXLevel) {
1839 case X86AVXABILevel::AVX512:
1840 return 512;
1841 case X86AVXABILevel::AVX:
1842 return 256;
1843 case X86AVXABILevel::None:
1844 return 128;
1845 }
1846 llvm_unreachable("Unknown AVXLevel");
1847}
1848
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001849/// X86_64ABIInfo - The X86_64 ABI information.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001850class X86_64ABIInfo : public SwiftABIInfo {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001851 enum Class {
1852 Integer = 0,
1853 SSE,
1854 SSEUp,
1855 X87,
1856 X87Up,
1857 ComplexX87,
1858 NoClass,
1859 Memory
1860 };
1861
1862 /// merge - Implement the X86_64 ABI merging algorithm.
1863 ///
1864 /// Merge an accumulating classification \arg Accum with a field
1865 /// classification \arg Field.
1866 ///
1867 /// \param Accum - The accumulating classification. This should
1868 /// always be either NoClass or the result of a previous merge
1869 /// call. In addition, this should never be Memory (the caller
1870 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001871 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001872
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001873 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1874 ///
1875 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1876 /// final MEMORY or SSE classes when necessary.
1877 ///
1878 /// \param AggregateSize - The size of the current aggregate in
1879 /// the classification process.
1880 ///
1881 /// \param Lo - The classification for the parts of the type
1882 /// residing in the low word of the containing object.
1883 ///
1884 /// \param Hi - The classification for the parts of the type
1885 /// residing in the higher words of the containing object.
1886 ///
1887 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1888
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001889 /// classify - Determine the x86_64 register classes in which the
1890 /// given type T should be passed.
1891 ///
1892 /// \param Lo - The classification for the parts of the type
1893 /// residing in the low word of the containing object.
1894 ///
1895 /// \param Hi - The classification for the parts of the type
1896 /// residing in the high word of the containing object.
1897 ///
1898 /// \param OffsetBase - The bit offset of this type in the
1899 /// containing object. Some parameters are classified different
1900 /// depending on whether they straddle an eightbyte boundary.
1901 ///
Eli Friedman7a1b5862013-06-12 00:13:45 +00001902 /// \param isNamedArg - Whether the argument in question is a "named"
1903 /// argument, as used in AMD64-ABI 3.5.7.
1904 ///
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001905 /// If a word is unused its result will be NoClass; if a type should
1906 /// be passed in Memory then at least the classification of \arg Lo
1907 /// will be Memory.
1908 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001909 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001910 ///
1911 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1912 /// also be ComplexX87.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001913 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1914 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001915
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001916 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001917 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1918 unsigned IROffset, QualType SourceTy,
1919 unsigned SourceOffset) const;
1920 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1921 unsigned IROffset, QualType SourceTy,
1922 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001923
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001924 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001925 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001926 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001927
1928 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001929 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001930 ///
1931 /// \param freeIntRegs - The number of free integer registers remaining
1932 /// available.
1933 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001934
Chris Lattnera3c109b2010-07-29 02:16:43 +00001935 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001936
Bill Wendlingbb465d72010-10-18 03:41:31 +00001937 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001938 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001939 unsigned &neededInt,
Eli Friedman7a1b5862013-06-12 00:13:45 +00001940 unsigned &neededSSE,
1941 bool isNamedArg) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001942
Eli Friedmanee1ad992011-12-02 00:11:43 +00001943 bool IsIllegalVectorType(QualType Ty) const;
1944
John McCall67a57732011-04-21 01:20:55 +00001945 /// The 0.98 ABI revision clarified a lot of ambiguities,
1946 /// unfortunately in ways that were not always consistent with
1947 /// certain previous compilers. In particular, platforms which
1948 /// required strict binary compatibility with older versions of GCC
1949 /// may need to exempt themselves.
1950 bool honorsRevision0_98() const {
John McCall64aa4b32013-04-16 22:48:15 +00001951 return !getTarget().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001952 }
1953
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001954 /// GCC classifies <1 x long long> as SSE but compatibility with older clang
1955 // compilers require us to classify it as INTEGER.
1956 bool classifyIntegerMMXAsSSE() const {
1957 const llvm::Triple &Triple = getTarget().getTriple();
1958 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
1959 return false;
1960 if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
1961 return false;
1962 return true;
1963 }
1964
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001965 X86AVXABILevel AVXLevel;
Derek Schuffbabaf312012-10-11 15:52:22 +00001966 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1967 // 64-bit hardware.
1968 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001969
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001970public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001971 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001972 SwiftABIInfo(CGT), AVXLevel(AVXLevel),
Derek Schuff90da80c2012-10-11 18:21:13 +00001973 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001974 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001975
John McCallde5d3c72012-02-17 03:33:10 +00001976 bool isPassedUsingAVXType(QualType type) const {
1977 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001978 // The freeIntRegs argument doesn't matter here.
Eli Friedman7a1b5862013-06-12 00:13:45 +00001979 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1980 /*isNamedArg*/true);
John McCallde5d3c72012-02-17 03:33:10 +00001981 if (info.isDirect()) {
1982 llvm::Type *ty = info.getCoerceToType();
1983 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1984 return (vectorTy->getBitWidth() > 128);
1985 }
1986 return false;
1987 }
1988
Stephen Hines651f13c2014-04-23 16:59:28 -07001989 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001990
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08001991 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1992 QualType Ty) const override;
1993 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
1994 QualType Ty) const override;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001995
1996 bool has64BitPointers() const {
1997 return Has64BitPointers;
1998 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07001999
2000 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
2001 ArrayRef<llvm::Type*> scalars,
2002 bool asReturnValue) const override {
2003 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2004 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002005};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002006
Chris Lattnerf13721d2010-08-31 16:44:54 +00002007/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002008class WinX86_64ABIInfo : public ABIInfo {
Chris Lattnerf13721d2010-08-31 16:44:54 +00002009public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002010 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT)
2011 : ABIInfo(CGT),
2012 IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002013
Stephen Hines651f13c2014-04-23 16:59:28 -07002014 void computeInfo(CGFunctionInfo &FI) const override;
Chris Lattnerf13721d2010-08-31 16:44:54 +00002015
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002016 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2017 QualType Ty) const override;
Stephen Hines176edba2014-12-01 14:53:08 -08002018
2019 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
2020 // FIXME: Assumes vectorcall is in use.
2021 return isX86VectorTypeForVectorCall(getContext(), Ty);
2022 }
2023
2024 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
2025 uint64_t NumMembers) const override {
2026 // FIXME: Assumes vectorcall is in use.
2027 return isX86VectorCallAggregateSmallEnough(NumMembers);
2028 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002029
2030private:
2031 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs,
2032 bool IsReturnType) const;
2033
2034 bool IsMingw64;
Chris Lattnerf13721d2010-08-31 16:44:54 +00002035};
2036
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002037class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2038public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002039 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2040 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
John McCall6374c332010-03-06 00:35:14 +00002041
John McCallde5d3c72012-02-17 03:33:10 +00002042 const X86_64ABIInfo &getABIInfo() const {
2043 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
2044 }
2045
Stephen Hines651f13c2014-04-23 16:59:28 -07002046 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCall6374c332010-03-06 00:35:14 +00002047 return 7;
2048 }
2049
2050 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07002051 llvm::Value *Address) const override {
Chris Lattner8b418682012-02-07 00:39:47 +00002052 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002053
John McCallaeeb7012010-05-27 06:19:26 +00002054 // 0-15 are the 16 integer registers.
2055 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00002056 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00002057 return false;
2058 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00002059
Jay Foadef6de3d2011-07-11 09:56:20 +00002060 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002061 StringRef Constraint,
Stephen Hines651f13c2014-04-23 16:59:28 -07002062 llvm::Type* Ty) const override {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00002063 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
2064 }
2065
John McCallde5d3c72012-02-17 03:33:10 +00002066 bool isNoProtoCallVariadic(const CallArgList &args,
Stephen Hines651f13c2014-04-23 16:59:28 -07002067 const FunctionNoProtoType *fnType) const override {
John McCall01f151e2011-09-21 08:08:30 +00002068 // The default CC on x86-64 sets %al to the number of SSA
2069 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00002070 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00002071 // that when AVX types are involved: the ABI explicitly states it is
2072 // undefined, and it doesn't work in practice because of how the ABI
2073 // defines varargs anyway.
Reid Kleckneref072032013-08-27 23:08:25 +00002074 if (fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00002075 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00002076 for (CallArgList::const_iterator
2077 it = args.begin(), ie = args.end(); it != ie; ++it) {
2078 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
2079 HasAVXType = true;
2080 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00002081 }
2082 }
John McCallde5d3c72012-02-17 03:33:10 +00002083
Eli Friedman3ed79032011-12-01 04:53:19 +00002084 if (!HasAVXType)
2085 return true;
2086 }
John McCall01f151e2011-09-21 08:08:30 +00002087
John McCallde5d3c72012-02-17 03:33:10 +00002088 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00002089 }
2090
Stephen Hines651f13c2014-04-23 16:59:28 -07002091 llvm::Constant *
2092 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002093 unsigned Sig;
2094 if (getABIInfo().has64BitPointers())
2095 Sig = (0xeb << 0) | // jmp rel8
2096 (0x0a << 8) | // .+0x0c
2097 ('F' << 16) |
2098 ('T' << 24);
2099 else
2100 Sig = (0xeb << 0) | // jmp rel8
2101 (0x06 << 8) | // .+0x08
2102 ('F' << 16) |
2103 ('T' << 24);
Peter Collingbourneb914e872013-10-20 21:29:19 +00002104 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2105 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002106
2107 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2108 CodeGen::CodeGenModule &CGM) const override {
2109 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2110 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2111 llvm::Function *Fn = cast<llvm::Function>(GV);
2112 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2113 }
2114 }
2115 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002116};
2117
2118class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo {
2119public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002120 PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2121 : X86_64TargetCodeGenInfo(CGT, AVXLevel) {}
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002122
2123 void getDependentLibraryOption(llvm::StringRef Lib,
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07002124 llvm::SmallString<24> &Opt) const override {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002125 Opt = "\01";
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002126 // If the argument contains a space, enclose it in quotes.
2127 if (Lib.find(" ") != StringRef::npos)
2128 Opt += "\"" + Lib.str() + "\"";
2129 else
2130 Opt += Lib;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002131 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002132};
2133
Aaron Ballman89735b92013-05-24 15:06:56 +00002134static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002135 // If the argument does not end in .lib, automatically add the suffix.
2136 // If the argument contains a space, enclose it in quotes.
2137 // This matches the behavior of MSVC.
2138 bool Quote = (Lib.find(" ") != StringRef::npos);
2139 std::string ArgStr = Quote ? "\"" : "";
2140 ArgStr += Lib;
Rui Ueyama723cead2013-10-31 19:12:53 +00002141 if (!Lib.endswith_lower(".lib"))
Aaron Ballman89735b92013-05-24 15:06:56 +00002142 ArgStr += ".lib";
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002143 ArgStr += Quote ? "\"" : "";
Aaron Ballman89735b92013-05-24 15:06:56 +00002144 return ArgStr;
2145}
2146
Reid Kleckner3190ca92013-05-08 13:44:39 +00002147class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2148public:
John McCallb8b52972013-06-18 02:46:29 +00002149 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002150 bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2151 unsigned NumRegisterParameters)
2152 : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
2153 Win32StructABI, NumRegisterParameters, false) {}
Reid Kleckner3190ca92013-05-08 13:44:39 +00002154
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002155 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002156 CodeGen::CodeGenModule &CGM) const override;
2157
Reid Kleckner3190ca92013-05-08 13:44:39 +00002158 void getDependentLibraryOption(llvm::StringRef Lib,
Stephen Hines651f13c2014-04-23 16:59:28 -07002159 llvm::SmallString<24> &Opt) const override {
Reid Kleckner3190ca92013-05-08 13:44:39 +00002160 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00002161 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00002162 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00002163
2164 void getDetectMismatchOption(llvm::StringRef Name,
2165 llvm::StringRef Value,
Stephen Hines651f13c2014-04-23 16:59:28 -07002166 llvm::SmallString<32> &Opt) const override {
Eli Friedman572ac322013-06-07 22:42:22 +00002167 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00002168 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00002169};
2170
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002171static void addStackProbeSizeTargetAttribute(const Decl *D,
2172 llvm::GlobalValue *GV,
2173 CodeGen::CodeGenModule &CGM) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002174 if (D && isa<FunctionDecl>(D)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002175 if (CGM.getCodeGenOpts().StackProbeSize != 4096) {
2176 llvm::Function *Fn = cast<llvm::Function>(GV);
2177
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002178 Fn->addFnAttr("stack-probe-size",
2179 llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002180 }
2181 }
2182}
2183
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002184void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002185 llvm::GlobalValue *GV,
2186 CodeGen::CodeGenModule &CGM) const {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002187 X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002188
2189 addStackProbeSizeTargetAttribute(D, GV, CGM);
2190}
2191
Chris Lattnerf13721d2010-08-31 16:44:54 +00002192class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002193public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002194 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2195 X86AVXABILevel AVXLevel)
2196 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002197
2198 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002199 CodeGen::CodeGenModule &CGM) const override;
2200
Stephen Hines651f13c2014-04-23 16:59:28 -07002201 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
Chris Lattnerf13721d2010-08-31 16:44:54 +00002202 return 7;
2203 }
2204
2205 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07002206 llvm::Value *Address) const override {
Chris Lattner8b418682012-02-07 00:39:47 +00002207 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002208
Chris Lattnerf13721d2010-08-31 16:44:54 +00002209 // 0-15 are the 16 integer registers.
2210 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00002211 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00002212 return false;
2213 }
Reid Kleckner3190ca92013-05-08 13:44:39 +00002214
2215 void getDependentLibraryOption(llvm::StringRef Lib,
Stephen Hines651f13c2014-04-23 16:59:28 -07002216 llvm::SmallString<24> &Opt) const override {
Reid Kleckner3190ca92013-05-08 13:44:39 +00002217 Opt = "/DEFAULTLIB:";
Aaron Ballman89735b92013-05-24 15:06:56 +00002218 Opt += qualifyWindowsLibrary(Lib);
Reid Kleckner3190ca92013-05-08 13:44:39 +00002219 }
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00002220
2221 void getDetectMismatchOption(llvm::StringRef Name,
2222 llvm::StringRef Value,
Stephen Hines651f13c2014-04-23 16:59:28 -07002223 llvm::SmallString<32> &Opt) const override {
Eli Friedman572ac322013-06-07 22:42:22 +00002224 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00002225 }
Chris Lattnerf13721d2010-08-31 16:44:54 +00002226};
2227
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002228void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002229 llvm::GlobalValue *GV,
2230 CodeGen::CodeGenModule &CGM) const {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002231 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002232
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002233 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2234 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2235 llvm::Function *Fn = cast<llvm::Function>(GV);
2236 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2237 }
2238 }
2239
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002240 addStackProbeSizeTargetAttribute(D, GV, CGM);
2241}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002242}
2243
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002244void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2245 Class &Hi) const {
2246 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2247 //
2248 // (a) If one of the classes is Memory, the whole argument is passed in
2249 // memory.
2250 //
2251 // (b) If X87UP is not preceded by X87, the whole argument is passed in
2252 // memory.
2253 //
2254 // (c) If the size of the aggregate exceeds two eightbytes and the first
2255 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2256 // argument is passed in memory. NOTE: This is necessary to keep the
2257 // ABI working for processors that don't support the __m256 type.
2258 //
2259 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2260 //
2261 // Some of these are enforced by the merging logic. Others can arise
2262 // only with unions; for example:
2263 // union { _Complex double; unsigned; }
2264 //
2265 // Note that clauses (b) and (c) were added in 0.98.
2266 //
2267 if (Hi == Memory)
2268 Lo = Memory;
2269 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2270 Lo = Memory;
2271 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2272 Lo = Memory;
2273 if (Hi == SSEUp && Lo != SSE)
2274 Hi = SSE;
2275}
2276
Chris Lattner1090a9b2010-06-28 21:43:59 +00002277X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002278 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2279 // classified recursively so that always two fields are
2280 // considered. The resulting class is calculated according to
2281 // the classes of the fields in the eightbyte:
2282 //
2283 // (a) If both classes are equal, this is the resulting class.
2284 //
2285 // (b) If one of the classes is NO_CLASS, the resulting class is
2286 // the other class.
2287 //
2288 // (c) If one of the classes is MEMORY, the result is the MEMORY
2289 // class.
2290 //
2291 // (d) If one of the classes is INTEGER, the result is the
2292 // INTEGER.
2293 //
2294 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2295 // MEMORY is used as class.
2296 //
2297 // (f) Otherwise class SSE is used.
2298
2299 // Accum should never be memory (we should have returned) or
2300 // ComplexX87 (because this cannot be passed in a structure).
2301 assert((Accum != Memory && Accum != ComplexX87) &&
2302 "Invalid accumulated classification during merge.");
2303 if (Accum == Field || Field == NoClass)
2304 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002305 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002306 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002307 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002308 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002309 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002310 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002311 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2312 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002313 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002314 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002315}
2316
Chris Lattnerbcaedae2010-06-30 19:14:05 +00002317void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman7a1b5862013-06-12 00:13:45 +00002318 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002319 // FIXME: This code can be simplified by introducing a simple value class for
2320 // Class pairs with appropriate constructor methods for the various
2321 // situations.
2322
2323 // FIXME: Some of the split computations are wrong; unaligned vectors
2324 // shouldn't be passed in registers for example, so there is no chance they
2325 // can straddle an eightbyte. Verify & simplify.
2326
2327 Lo = Hi = NoClass;
2328
2329 Class &Current = OffsetBase < 64 ? Lo : Hi;
2330 Current = Memory;
2331
John McCall183700f2009-09-21 23:43:11 +00002332 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002333 BuiltinType::Kind k = BT->getKind();
2334
2335 if (k == BuiltinType::Void) {
2336 Current = NoClass;
2337 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2338 Lo = Integer;
2339 Hi = Integer;
2340 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2341 Current = Integer;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002342 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002343 Current = SSE;
2344 } else if (k == BuiltinType::LongDouble) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002345 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2346 if (LDF == &llvm::APFloat::IEEEquad) {
2347 Lo = SSE;
2348 Hi = SSEUp;
2349 } else if (LDF == &llvm::APFloat::x87DoubleExtended) {
2350 Lo = X87;
2351 Hi = X87Up;
2352 } else if (LDF == &llvm::APFloat::IEEEdouble) {
2353 Current = SSE;
2354 } else
2355 llvm_unreachable("unexpected long double representation!");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002356 }
2357 // FIXME: _Decimal32 and _Decimal64 are SSE.
2358 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00002359 return;
2360 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002361
Chris Lattner1090a9b2010-06-28 21:43:59 +00002362 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002363 // Classify the underlying integer type.
Eli Friedman7a1b5862013-06-12 00:13:45 +00002364 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002365 return;
2366 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002367
Chris Lattner1090a9b2010-06-28 21:43:59 +00002368 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002369 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00002370 return;
2371 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002372
Chris Lattner1090a9b2010-06-28 21:43:59 +00002373 if (Ty->isMemberPointerType()) {
Stephen Hines176edba2014-12-01 14:53:08 -08002374 if (Ty->isMemberFunctionPointerType()) {
2375 if (Has64BitPointers) {
2376 // If Has64BitPointers, this is an {i64, i64}, so classify both
2377 // Lo and Hi now.
2378 Lo = Hi = Integer;
2379 } else {
2380 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2381 // straddles an eightbyte boundary, Hi should be classified as well.
2382 uint64_t EB_FuncPtr = (OffsetBase) / 64;
2383 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2384 if (EB_FuncPtr != EB_ThisAdj) {
2385 Lo = Hi = Integer;
2386 } else {
2387 Current = Integer;
2388 }
2389 }
2390 } else {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00002391 Current = Integer;
Stephen Hines176edba2014-12-01 14:53:08 -08002392 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00002393 return;
2394 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002395
Chris Lattner1090a9b2010-06-28 21:43:59 +00002396 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00002397 uint64_t Size = getContext().getTypeSize(VT);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002398 if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2399 // gcc passes the following as integer:
2400 // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2401 // 2 bytes - <2 x char>, <1 x short>
2402 // 1 byte - <1 x char>
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002403 Current = Integer;
2404
2405 // If this type crosses an eightbyte boundary, it should be
2406 // split.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002407 uint64_t EB_Lo = (OffsetBase) / 64;
2408 uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2409 if (EB_Lo != EB_Hi)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002410 Hi = Lo;
2411 } else if (Size == 64) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002412 QualType ElementType = VT->getElementType();
2413
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002414 // gcc passes <1 x double> in memory. :(
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002415 if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002416 return;
2417
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002418 // gcc passes <1 x long long> as SSE but clang used to unconditionally
2419 // pass them as integer. For platforms where clang is the de facto
2420 // platform compiler, we must continue to use integer.
2421 if (!classifyIntegerMMXAsSSE() &&
2422 (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2423 ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2424 ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2425 ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002426 Current = Integer;
2427 else
2428 Current = SSE;
2429
2430 // If this type crosses an eightbyte boundary, it should be
2431 // split.
2432 if (OffsetBase && OffsetBase != 64)
2433 Hi = Lo;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002434 } else if (Size == 128 ||
2435 (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002436 // Arguments of 256-bits are split into four eightbyte chunks. The
2437 // least significant one belongs to class SSE and all the others to class
2438 // SSEUP. The original Lo and Hi design considers that types can't be
2439 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2440 // This design isn't correct for 256-bits, but since there're no cases
2441 // where the upper parts would need to be inspected, avoid adding
2442 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman7a1b5862013-06-12 00:13:45 +00002443 //
2444 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2445 // registers if they are "named", i.e. not part of the "..." of a
2446 // variadic function.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002447 //
2448 // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2449 // split into eight eightbyte chunks, one SSE and seven SSEUP.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002450 Lo = SSE;
2451 Hi = SSEUp;
2452 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00002453 return;
2454 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002455
Chris Lattner1090a9b2010-06-28 21:43:59 +00002456 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00002457 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002458
Chris Lattnerea044322010-07-29 02:01:43 +00002459 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002460 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002461 if (Size <= 64)
2462 Current = Integer;
2463 else if (Size <= 128)
2464 Lo = Hi = Integer;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002465 } else if (ET == getContext().FloatTy) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002466 Current = SSE;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002467 } else if (ET == getContext().DoubleTy) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002468 Lo = Hi = SSE;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002469 } else if (ET == getContext().LongDoubleTy) {
2470 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2471 if (LDF == &llvm::APFloat::IEEEquad)
2472 Current = Memory;
2473 else if (LDF == &llvm::APFloat::x87DoubleExtended)
2474 Current = ComplexX87;
2475 else if (LDF == &llvm::APFloat::IEEEdouble)
2476 Lo = Hi = SSE;
2477 else
2478 llvm_unreachable("unexpected long double representation!");
2479 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002480
2481 // If this complex type crosses an eightbyte boundary then it
2482 // should be split.
2483 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00002484 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002485 if (Hi == NoClass && EB_Real != EB_Imag)
2486 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002487
Chris Lattner1090a9b2010-06-28 21:43:59 +00002488 return;
2489 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002490
Chris Lattnerea044322010-07-29 02:01:43 +00002491 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002492 // Arrays are treated like structures.
2493
Chris Lattnerea044322010-07-29 02:01:43 +00002494 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002495
2496 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002497 // than four eightbytes, ..., it has class MEMORY.
2498 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002499 return;
2500
2501 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2502 // fields, it has class MEMORY.
2503 //
2504 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00002505 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002506 return;
2507
2508 // Otherwise implement simplified merge. We could be smarter about
2509 // this, but it isn't worth it and would be harder to verify.
2510 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00002511 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002512 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00002513
2514 // The only case a 256-bit wide vector could be used is when the array
2515 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2516 // to work for sizes wider than 128, early check and fallback to memory.
2517 if (Size > 128 && EltSize != 256)
2518 return;
2519
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002520 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
2521 Class FieldLo, FieldHi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00002522 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002523 Lo = merge(Lo, FieldLo);
2524 Hi = merge(Hi, FieldHi);
2525 if (Lo == Memory || Hi == Memory)
2526 break;
2527 }
2528
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002529 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002530 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002531 return;
2532 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002533
Chris Lattner1090a9b2010-06-28 21:43:59 +00002534 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00002535 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002536
2537 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002538 // than four eightbytes, ..., it has class MEMORY.
2539 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002540 return;
2541
Anders Carlsson0a8f8472009-09-16 15:53:40 +00002542 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
2543 // copy constructor or a non-trivial destructor, it is passed by invisible
2544 // reference.
Mark Lacey23630722013-10-06 01:33:34 +00002545 if (getRecordArgABI(RT, getCXXABI()))
Anders Carlsson0a8f8472009-09-16 15:53:40 +00002546 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002547
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002548 const RecordDecl *RD = RT->getDecl();
2549
2550 // Assume variable sized types are passed in memory.
2551 if (RD->hasFlexibleArrayMember())
2552 return;
2553
Chris Lattnerea044322010-07-29 02:01:43 +00002554 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002555
2556 // Reset Lo class, this will be recomputed.
2557 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002558
2559 // If this is a C++ record, classify the bases first.
2560 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002561 for (const auto &I : CXXRD->bases()) {
2562 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002563 "Unexpected base class!");
2564 const CXXRecordDecl *Base =
Stephen Hines651f13c2014-04-23 16:59:28 -07002565 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002566
2567 // Classify this field.
2568 //
2569 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
2570 // single eightbyte, each is classified separately. Each eightbyte gets
2571 // initialized to class NO_CLASS.
2572 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00002573 uint64_t Offset =
2574 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Stephen Hines651f13c2014-04-23 16:59:28 -07002575 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002576 Lo = merge(Lo, FieldLo);
2577 Hi = merge(Hi, FieldHi);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002578 if (Lo == Memory || Hi == Memory) {
2579 postMerge(Size, Lo, Hi);
2580 return;
2581 }
Daniel Dunbarce9f4232009-11-22 23:01:23 +00002582 }
2583 }
2584
2585 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002586 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00002587 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002588 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002589 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2590 bool BitField = i->isBitField();
2591
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00002592 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
2593 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002594 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00002595 // The only case a 256-bit wide vector could be used is when the struct
2596 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2597 // to work for sizes wider than 128, early check and fallback to memory.
2598 //
2599 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
2600 Lo = Memory;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002601 postMerge(Size, Lo, Hi);
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00002602 return;
2603 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002604 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00002605 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002606 Lo = Memory;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002607 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002608 return;
2609 }
2610
2611 // Classify this field.
2612 //
2613 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
2614 // exceeds a single eightbyte, each is classified
2615 // separately. Each eightbyte gets initialized to class
2616 // NO_CLASS.
2617 Class FieldLo, FieldHi;
2618
2619 // Bit-fields require special handling, they do not force the
2620 // structure to be passed in memory even if unaligned, and
2621 // therefore they can straddle an eightbyte.
2622 if (BitField) {
2623 // Ignore padding bit-fields.
2624 if (i->isUnnamedBitfield())
2625 continue;
2626
2627 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002628 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002629
2630 uint64_t EB_Lo = Offset / 64;
2631 uint64_t EB_Hi = (Offset + Size - 1) / 64;
Sylvestre Ledru9a6002a2013-10-06 09:54:18 +00002632
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002633 if (EB_Lo) {
2634 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
2635 FieldLo = NoClass;
2636 FieldHi = Integer;
2637 } else {
2638 FieldLo = Integer;
2639 FieldHi = EB_Hi ? Integer : NoClass;
2640 }
2641 } else
Eli Friedman7a1b5862013-06-12 00:13:45 +00002642 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002643 Lo = merge(Lo, FieldLo);
2644 Hi = merge(Hi, FieldHi);
2645 if (Lo == Memory || Hi == Memory)
2646 break;
2647 }
2648
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002649 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002650 }
2651}
2652
Chris Lattner9c254f02010-06-29 06:01:59 +00002653ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00002654 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2655 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00002656 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00002657 // Treat an enum type as its underlying type.
2658 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2659 Ty = EnumTy->getDecl()->getIntegerType();
2660
2661 return (Ty->isPromotableIntegerType() ?
2662 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2663 }
2664
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002665 return getNaturalAlignIndirect(Ty);
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00002666}
2667
Eli Friedmanee1ad992011-12-02 00:11:43 +00002668bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
2669 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
2670 uint64_t Size = getContext().getTypeSize(VecTy);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002671 unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
Eli Friedmanee1ad992011-12-02 00:11:43 +00002672 if (Size <= 64 || Size > LargestVector)
2673 return true;
2674 }
2675
2676 return false;
2677}
2678
Daniel Dunbaredfac032012-03-10 01:03:58 +00002679ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
2680 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002681 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2682 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00002683 //
2684 // This assumption is optimistic, as there could be free registers available
2685 // when we need to pass this argument in memory, and LLVM could try to pass
2686 // the argument in the free register. This does not seem to happen currently,
2687 // but this code would be much safer if we could mark the argument with
2688 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00002689 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002690 // Treat an enum type as its underlying type.
2691 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2692 Ty = EnumTy->getDecl()->getIntegerType();
2693
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002694 return (Ty->isPromotableIntegerType() ?
2695 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002696 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002697
Mark Lacey23630722013-10-06 01:33:34 +00002698 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002699 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00002700
Chris Lattner855d2272011-05-22 23:21:23 +00002701 // Compute the byval alignment. We specify the alignment of the byval in all
2702 // cases so that the mid-level optimizer knows the alignment of the byval.
2703 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002704
2705 // Attempt to avoid passing indirect results using byval when possible. This
2706 // is important for good codegen.
2707 //
2708 // We do this by coercing the value into a scalar type which the backend can
2709 // handle naturally (i.e., without using byval).
2710 //
2711 // For simplicity, we currently only do this when we have exhausted all of the
2712 // free integer registers. Doing this when there are free integer registers
2713 // would require more care, as we would have to ensure that the coerced value
2714 // did not claim the unused register. That would require either reording the
2715 // arguments to the function (so that any subsequent inreg values came first),
2716 // or only doing this optimization when there were no following arguments that
2717 // might be inreg.
2718 //
2719 // We currently expect it to be rare (particularly in well written code) for
2720 // arguments to be passed on the stack when there are still free integer
2721 // registers available (this would typically imply large structs being passed
2722 // by value), so this seems like a fair tradeoff for now.
2723 //
2724 // We can revisit this if the backend grows support for 'onstack' parameter
2725 // attributes. See PR12193.
2726 if (freeIntRegs == 0) {
2727 uint64_t Size = getContext().getTypeSize(Ty);
2728
2729 // If this type fits in an eightbyte, coerce it into the matching integral
2730 // type, which will end up on the stack (with alignment 8).
2731 if (Align == 8 && Size <= 64)
2732 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2733 Size));
2734 }
2735
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002736 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002737}
2738
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002739/// The ABI specifies that a value should be passed in a full vector XMM/YMM
2740/// register. Pick an LLVM IR type that will be passed as a vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002741llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002742 // Wrapper structs/arrays that only contain vectors are passed just like
2743 // vectors; strip them off if present.
2744 if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
2745 Ty = QualType(InnerTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002746
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002747 llvm::Type *IRType = CGT.ConvertType(Ty);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002748 if (isa<llvm::VectorType>(IRType) ||
2749 IRType->getTypeID() == llvm::Type::FP128TyID)
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07002750 return IRType;
2751
2752 // We couldn't find the preferred IR vector type for 'Ty'.
2753 uint64_t Size = getContext().getTypeSize(Ty);
2754 assert((Size == 128 || Size == 256) && "Invalid type found!");
2755
2756 // Return a LLVM IR vector type based on the size of 'Ty'.
2757 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
2758 Size / 64);
Chris Lattner0f408f52010-07-29 04:56:46 +00002759}
2760
Chris Lattnere2962be2010-07-29 07:30:00 +00002761/// BitsContainNoUserData - Return true if the specified [start,end) bit range
2762/// is known to either be off the end of the specified type or being in
2763/// alignment padding. The user type specified is known to be at most 128 bits
2764/// in size, and have passed through X86_64ABIInfo::classify with a successful
2765/// classification that put one of the two halves in the INTEGER class.
2766///
2767/// It is conservatively correct to return false.
2768static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
2769 unsigned EndBit, ASTContext &Context) {
2770 // If the bytes being queried are off the end of the type, there is no user
2771 // data hiding here. This handles analysis of builtins, vectors and other
2772 // types that don't contain interesting padding.
2773 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
2774 if (TySize <= StartBit)
2775 return true;
2776
Chris Lattner021c3a32010-07-29 07:43:55 +00002777 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2778 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
2779 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
2780
2781 // Check each element to see if the element overlaps with the queried range.
2782 for (unsigned i = 0; i != NumElts; ++i) {
2783 // If the element is after the span we care about, then we're done..
2784 unsigned EltOffset = i*EltSize;
2785 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002786
Chris Lattner021c3a32010-07-29 07:43:55 +00002787 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
2788 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
2789 EndBit-EltOffset, Context))
2790 return false;
2791 }
2792 // If it overlaps no elements, then it is safe to process as padding.
2793 return true;
2794 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002795
Chris Lattnere2962be2010-07-29 07:30:00 +00002796 if (const RecordType *RT = Ty->getAs<RecordType>()) {
2797 const RecordDecl *RD = RT->getDecl();
2798 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002799
Chris Lattnere2962be2010-07-29 07:30:00 +00002800 // If this is a C++ record, check the bases first.
2801 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002802 for (const auto &I : CXXRD->bases()) {
2803 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Chris Lattnere2962be2010-07-29 07:30:00 +00002804 "Unexpected base class!");
2805 const CXXRecordDecl *Base =
Stephen Hines651f13c2014-04-23 16:59:28 -07002806 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002807
Chris Lattnere2962be2010-07-29 07:30:00 +00002808 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00002809 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00002810 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002811
Chris Lattnere2962be2010-07-29 07:30:00 +00002812 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
Stephen Hines651f13c2014-04-23 16:59:28 -07002813 if (!BitsContainNoUserData(I.getType(), BaseStart,
Chris Lattnere2962be2010-07-29 07:30:00 +00002814 EndBit-BaseOffset, Context))
2815 return false;
2816 }
2817 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002818
Chris Lattnere2962be2010-07-29 07:30:00 +00002819 // Verify that no field has data that overlaps the region of interest. Yes
2820 // this could be sped up a lot by being smarter about queried fields,
2821 // however we're only looking at structs up to 16 bytes, so we don't care
2822 // much.
2823 unsigned idx = 0;
2824 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2825 i != e; ++i, ++idx) {
2826 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002827
Chris Lattnere2962be2010-07-29 07:30:00 +00002828 // If we found a field after the region we care about, then we're done.
2829 if (FieldOffset >= EndBit) break;
2830
2831 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
2832 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
2833 Context))
2834 return false;
2835 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002836
Chris Lattnere2962be2010-07-29 07:30:00 +00002837 // If nothing in this record overlapped the area of interest, then we're
2838 // clean.
2839 return true;
2840 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002841
Chris Lattnere2962be2010-07-29 07:30:00 +00002842 return false;
2843}
2844
Chris Lattner0b362002010-07-29 18:39:32 +00002845/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
2846/// float member at the specified offset. For example, {int,{float}} has a
2847/// float at offset 4. It is conservatively correct for this routine to return
2848/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002849static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00002850 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00002851 // Base case if we find a float.
2852 if (IROffset == 0 && IRType->isFloatTy())
2853 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002854
Chris Lattner0b362002010-07-29 18:39:32 +00002855 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002856 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00002857 const llvm::StructLayout *SL = TD.getStructLayout(STy);
2858 unsigned Elt = SL->getElementContainingOffset(IROffset);
2859 IROffset -= SL->getElementOffset(Elt);
2860 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
2861 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002862
Chris Lattner0b362002010-07-29 18:39:32 +00002863 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002864 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
2865 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00002866 unsigned EltSize = TD.getTypeAllocSize(EltTy);
2867 IROffset -= IROffset/EltSize*EltSize;
2868 return ContainsFloatAtOffset(EltTy, IROffset, TD);
2869 }
2870
2871 return false;
2872}
2873
Chris Lattnerf47c9442010-07-29 18:13:09 +00002874
2875/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
2876/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002877llvm::Type *X86_64ABIInfo::
2878GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00002879 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00002880 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00002881 // pass as float if the last 4 bytes is just padding. This happens for
2882 // structs that contain 3 floats.
2883 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
2884 SourceOffset*8+64, getContext()))
2885 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002886
Chris Lattner0b362002010-07-29 18:39:32 +00002887 // We want to pass as <2 x float> if the LLVM IR type contains a float at
2888 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
2889 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00002890 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
2891 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00002892 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002893
Chris Lattnerf47c9442010-07-29 18:13:09 +00002894 return llvm::Type::getDoubleTy(getVMContext());
2895}
2896
2897
Chris Lattner0d2656d2010-07-29 17:40:35 +00002898/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
2899/// an 8-byte GPR. This means that we either have a scalar or we are talking
2900/// about the high or low part of an up-to-16-byte struct. This routine picks
2901/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00002902/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
2903/// etc).
2904///
2905/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
2906/// the source type. IROffset is an offset in bytes into the LLVM IR type that
2907/// the 8-byte value references. PrefType may be null.
2908///
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002909/// SourceTy is the source-level type for the entire argument. SourceOffset is
Chris Lattner49382de2010-07-28 22:44:07 +00002910/// an offset into this that we're processing (which is always either 0 or 8).
2911///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002912llvm::Type *X86_64ABIInfo::
2913GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00002914 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00002915 // If we're dealing with an un-offset LLVM IR type, then it means that we're
2916 // returning an 8-byte unit starting with it. See if we can safely use it.
2917 if (IROffset == 0) {
2918 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00002919 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
2920 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00002921 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00002922
Chris Lattnere2962be2010-07-29 07:30:00 +00002923 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
2924 // goodness in the source type is just tail padding. This is allowed to
2925 // kick in for struct {double,int} on the int, but not on
2926 // struct{double,int,int} because we wouldn't return the second int. We
2927 // have to do this analysis on the source type because we can't depend on
2928 // unions being lowered a specific way etc.
2929 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00002930 IRType->isIntegerTy(32) ||
2931 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
2932 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2933 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002934
Chris Lattnere2962be2010-07-29 07:30:00 +00002935 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2936 SourceOffset*8+64, getContext()))
2937 return IRType;
2938 }
2939 }
Chris Lattner49382de2010-07-28 22:44:07 +00002940
Chris Lattner2acc6e32011-07-18 04:24:23 +00002941 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00002942 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00002943 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00002944 if (IROffset < SL->getSizeInBytes()) {
2945 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2946 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002947
Chris Lattner0d2656d2010-07-29 17:40:35 +00002948 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2949 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002950 }
Chris Lattner49382de2010-07-28 22:44:07 +00002951 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002952
Chris Lattner2acc6e32011-07-18 04:24:23 +00002953 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002954 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00002955 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00002956 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00002957 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2958 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00002959 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002960
Chris Lattner49382de2010-07-28 22:44:07 +00002961 // Okay, we don't have any better idea of what to pass, so we pass this in an
2962 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002963 unsigned TySizeInBytes =
2964 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00002965
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002966 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002967
Chris Lattner49382de2010-07-28 22:44:07 +00002968 // It is always safe to classify this as an integer type up to i64 that
2969 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00002970 return llvm::IntegerType::get(getVMContext(),
2971 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00002972}
2973
Chris Lattner66e7b682010-09-01 00:50:20 +00002974
2975/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2976/// be used as elements of a two register pair to pass or return, return a
2977/// first class aggregate to represent them. For example, if the low part of
2978/// a by-value argument should be passed as i32* and the high part as float,
2979/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002980static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00002981GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00002982 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00002983 // In order to correctly satisfy the ABI, we need to the high part to start
2984 // at offset 8. If the high and low parts we inferred are both 4-byte types
2985 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2986 // the second element at offset 8. Check for this:
2987 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2988 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07002989 unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00002990 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002991
Chris Lattner66e7b682010-09-01 00:50:20 +00002992 // To handle this, we have to increase the size of the low part so that the
2993 // second element will start at an 8 byte offset. We can't increase the size
2994 // of the second element because it might make us access off the end of the
2995 // struct.
2996 if (HiStart != 8) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08002997 // There are usually two sorts of types the ABI generation code can produce
2998 // for the low part of a pair that aren't 8 bytes in size: float or
2999 // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and
3000 // NaCl).
Chris Lattner66e7b682010-09-01 00:50:20 +00003001 // Promote these to a larger type.
3002 if (Lo->isFloatTy())
3003 Lo = llvm::Type::getDoubleTy(Lo->getContext());
3004 else {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003005 assert((Lo->isIntegerTy() || Lo->isPointerTy())
3006 && "Invalid/unknown lo type");
Chris Lattner66e7b682010-09-01 00:50:20 +00003007 Lo = llvm::Type::getInt64Ty(Lo->getContext());
3008 }
3009 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00003010
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003011 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00003012
3013
Chris Lattner66e7b682010-09-01 00:50:20 +00003014 // Verify that the second element is at an 8-byte offset.
3015 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3016 "Invalid x86-64 argument pair!");
3017 return Result;
3018}
3019
Chris Lattner519f68c2010-07-28 23:06:14 +00003020ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00003021classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00003022 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3023 // classification algorithm.
3024 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00003025 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner519f68c2010-07-28 23:06:14 +00003026
3027 // Check some invariants.
3028 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00003029 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3030
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003031 llvm::Type *ResType = nullptr;
Chris Lattner519f68c2010-07-28 23:06:14 +00003032 switch (Lo) {
3033 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00003034 if (Hi == NoClass)
3035 return ABIArgInfo::getIgnore();
3036 // If the low part is just padding, it takes no register, leave ResType
3037 // null.
3038 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3039 "Unknown missing lo part");
3040 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00003041
3042 case SSEUp:
3043 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00003044 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00003045
3046 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3047 // hidden argument.
3048 case Memory:
3049 return getIndirectReturnResult(RetTy);
3050
3051 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3052 // available register of the sequence %rax, %rdx is used.
3053 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003054 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003055
Chris Lattnereb518b42010-07-29 21:42:50 +00003056 // If we have a sign or zero extended integer, make sure to return Extend
3057 // so that the parameter gets the right LLVM IR attributes.
3058 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3059 // Treat an enum type as its underlying type.
3060 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3061 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003062
Chris Lattnereb518b42010-07-29 21:42:50 +00003063 if (RetTy->isIntegralOrEnumerationType() &&
3064 RetTy->isPromotableIntegerType())
3065 return ABIArgInfo::getExtend();
3066 }
Chris Lattner519f68c2010-07-28 23:06:14 +00003067 break;
3068
3069 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3070 // available SSE register of the sequence %xmm0, %xmm1 is used.
3071 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003072 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00003073 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00003074
3075 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3076 // returned on the X87 stack in %st0 as 80-bit x87 number.
3077 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00003078 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00003079 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00003080
3081 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3082 // part of the value is returned in %st0 and the imaginary part in
3083 // %st1.
3084 case ComplexX87:
3085 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00003086 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00003087 llvm::Type::getX86_FP80Ty(getVMContext()),
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003088 nullptr);
Chris Lattner519f68c2010-07-28 23:06:14 +00003089 break;
3090 }
3091
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003092 llvm::Type *HighPart = nullptr;
Chris Lattner519f68c2010-07-28 23:06:14 +00003093 switch (Hi) {
3094 // Memory was handled previously and X87 should
3095 // never occur as a hi class.
3096 case Memory:
3097 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00003098 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00003099
3100 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00003101 case NoClass:
3102 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00003103
Chris Lattner3db4dde2010-09-01 00:20:33 +00003104 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003105 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00003106 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3107 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00003108 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00003109 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003110 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00003111 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3112 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00003113 break;
3114
3115 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00003116 // is passed in the next available eightbyte chunk if the last used
3117 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00003118 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003119 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00003120 case SSEUp:
3121 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00003122 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00003123 break;
3124
3125 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3126 // returned together with the previous X87 value in %st0.
3127 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003128 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00003129 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003130 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00003131 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00003132 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003133 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00003134 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3135 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00003136 }
Chris Lattner519f68c2010-07-28 23:06:14 +00003137 break;
3138 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00003139
Chris Lattner3db4dde2010-09-01 00:20:33 +00003140 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00003141 // known to pass in the high eightbyte of the result. We do this by forming a
3142 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00003143 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00003144 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00003145
Chris Lattnereb518b42010-07-29 21:42:50 +00003146 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00003147}
3148
Daniel Dunbaredfac032012-03-10 01:03:58 +00003149ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman7a1b5862013-06-12 00:13:45 +00003150 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
3151 bool isNamedArg)
Daniel Dunbaredfac032012-03-10 01:03:58 +00003152 const
3153{
Stephen Hines176edba2014-12-01 14:53:08 -08003154 Ty = useFirstFieldIfTransparentUnion(Ty);
3155
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003156 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman7a1b5862013-06-12 00:13:45 +00003157 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003158
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003159 // Check some invariants.
3160 // FIXME: Enforce these by construction.
3161 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003162 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3163
3164 neededInt = 0;
3165 neededSSE = 0;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003166 llvm::Type *ResType = nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003167 switch (Lo) {
3168 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00003169 if (Hi == NoClass)
3170 return ABIArgInfo::getIgnore();
3171 // If the low part is just padding, it takes no register, leave ResType
3172 // null.
3173 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3174 "Unknown missing lo part");
3175 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003176
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003177 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3178 // on the stack.
3179 case Memory:
3180
3181 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3182 // COMPLEX_X87, it is passed in memory.
3183 case X87:
3184 case ComplexX87:
Mark Lacey23630722013-10-06 01:33:34 +00003185 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
Eli Friedmanded137f2011-06-29 07:04:55 +00003186 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00003187 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003188
3189 case SSEUp:
3190 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00003191 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003192
3193 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3194 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3195 // and %r9 is used.
3196 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00003197 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003198
Chris Lattner49382de2010-07-28 22:44:07 +00003199 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003200 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00003201
3202 // If we have a sign or zero extended integer, make sure to return Extend
3203 // so that the parameter gets the right LLVM IR attributes.
3204 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3205 // Treat an enum type as its underlying type.
3206 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3207 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003208
Chris Lattnereb518b42010-07-29 21:42:50 +00003209 if (Ty->isIntegralOrEnumerationType() &&
3210 Ty->isPromotableIntegerType())
3211 return ABIArgInfo::getExtend();
3212 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003213
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003214 break;
3215
3216 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3217 // available SSE register is used, the registers are taken in the
3218 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00003219 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003220 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00003221 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00003222 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003223 break;
3224 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00003225 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003226
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003227 llvm::Type *HighPart = nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003228 switch (Hi) {
3229 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003230 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003231 // which is passed in memory.
3232 case Memory:
3233 case X87:
3234 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00003235 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003236
3237 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003238
Chris Lattner645406a2010-09-01 00:24:35 +00003239 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003240 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00003241 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003242 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003243
Chris Lattner645406a2010-09-01 00:24:35 +00003244 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3245 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003246 break;
3247
3248 // X87Up generally doesn't occur here (long double is passed in
3249 // memory), except in situations involving unions.
3250 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00003251 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003252 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003253
Chris Lattner645406a2010-09-01 00:24:35 +00003254 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3255 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00003256
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003257 ++neededSSE;
3258 break;
3259
3260 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3261 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003262 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003263 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00003264 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00003265 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003266 break;
3267 }
3268
Chris Lattner645406a2010-09-01 00:24:35 +00003269 // If a high part was specified, merge it together with the low part. It is
3270 // known to pass in the high eightbyte of the result. We do this by forming a
3271 // first class struct aggregate with the high and low part: {low, high}
3272 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00003273 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00003274
Chris Lattnereb518b42010-07-29 21:42:50 +00003275 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003276}
3277
Chris Lattneree5dcd02010-07-29 02:31:05 +00003278void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003279
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003280 if (!getCXXABI().classifyReturnType(FI))
3281 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003282
3283 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00003284 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003285
3286 // If the return value is indirect, then the hidden argument is consuming one
3287 // integer register.
3288 if (FI.getReturnInfo().isIndirect())
3289 --freeIntRegs;
3290
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003291 // The chain argument effectively gives us another free register.
3292 if (FI.isChainCall())
3293 ++freeIntRegs;
3294
Stephen Hines176edba2014-12-01 14:53:08 -08003295 unsigned NumRequiredArgs = FI.getNumRequiredArgs();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003296 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3297 // get assigned (in left-to-right order) for passing as follows...
Stephen Hines176edba2014-12-01 14:53:08 -08003298 unsigned ArgNo = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003299 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Stephen Hines176edba2014-12-01 14:53:08 -08003300 it != ie; ++it, ++ArgNo) {
3301 bool IsNamedArg = ArgNo < NumRequiredArgs;
Eli Friedman7a1b5862013-06-12 00:13:45 +00003302
Bill Wendling99aaae82010-10-18 23:51:38 +00003303 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00003304 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
Stephen Hines176edba2014-12-01 14:53:08 -08003305 neededSSE, IsNamedArg);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003306
3307 // AMD64-ABI 3.2.3p3: If there are no registers available for any
3308 // eightbyte of an argument, the whole argument is passed on the
3309 // stack. If registers have already been assigned for some
3310 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00003311 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003312 freeIntRegs -= neededInt;
3313 freeSSERegs -= neededSSE;
3314 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00003315 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003316 }
3317 }
3318}
3319
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003320static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3321 Address VAListAddr, QualType Ty) {
3322 Address overflow_arg_area_p = CGF.Builder.CreateStructGEP(
3323 VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003324 llvm::Value *overflow_arg_area =
3325 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3326
3327 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3328 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00003329 // It isn't stated explicitly in the standard, but in practice we use
3330 // alignment greater than 16 where necessary.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003331 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3332 if (Align > CharUnits::fromQuantity(8)) {
3333 overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
3334 Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003335 }
3336
3337 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003338 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003339 llvm::Value *Res =
3340 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003341 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003342
3343 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
3344 // l->overflow_arg_area + sizeof(type).
3345 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
3346 // an 8 byte boundary.
3347
3348 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00003349 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00003350 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003351 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
3352 "overflow_arg_area.next");
3353 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
3354
3355 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003356 return Address(Res, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003357}
3358
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003359Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3360 QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003361 // Assume that va_list type is correct; should be pointer to LLVM type:
3362 // struct {
3363 // i32 gp_offset;
3364 // i32 fp_offset;
3365 // i8* overflow_arg_area;
3366 // i8* reg_save_area;
3367 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00003368 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003369
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003370 Ty = getContext().getCanonicalType(Ty);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003371 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
Eli Friedman7a1b5862013-06-12 00:13:45 +00003372 /*isNamedArg*/false);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003373
3374 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
3375 // in the registers. If not go to step 7.
3376 if (!neededInt && !neededSSE)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003377 return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003378
3379 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
3380 // general purpose registers needed to pass type and num_fp to hold
3381 // the number of floating point registers needed.
3382
3383 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
3384 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
3385 // l->fp_offset > 304 - num_fp * 16 go to step 7.
3386 //
3387 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
3388 // register save space).
3389
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003390 llvm::Value *InRegs = nullptr;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003391 Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
3392 llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003393 if (neededInt) {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003394 gp_offset_p =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003395 CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(),
3396 "gp_offset_p");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003397 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00003398 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
3399 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003400 }
3401
3402 if (neededSSE) {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07003403 fp_offset_p =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003404 CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4),
3405 "fp_offset_p");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003406 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
3407 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00003408 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
3409 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003410 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
3411 }
3412
3413 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3414 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
3415 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3416 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
3417
3418 // Emit code to load the value if it was passed in registers.
3419
3420 CGF.EmitBlock(InRegBlock);
3421
3422 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
3423 // an offset of l->gp_offset and/or l->fp_offset. This may require
3424 // copying to a temporary location in case the parameter is passed
3425 // in different register classes or requires an alignment greater
3426 // than 8 for general purpose registers and 16 for XMM registers.
3427 //
3428 // FIXME: This really results in shameful code when we end up needing to
3429 // collect arguments from different places; often what should result in a
3430 // simple assembling of a structure from scattered addresses has many more
3431 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00003432 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003433 llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
3434 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)),
3435 "reg_save_area");
3436
3437 Address RegAddr = Address::invalid();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003438 if (neededInt && neededSSE) {
3439 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00003440 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003441 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003442 Address Tmp = CGF.CreateMemTemp(Ty);
3443 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003444 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003445 llvm::Type *TyLo = ST->getElementType(0);
3446 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00003447 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003448 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003449 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
3450 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003451 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
3452 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003453 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
3454 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003455
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003456 // Copy the first element.
3457 llvm::Value *V =
3458 CGF.Builder.CreateDefaultAlignedLoad(
3459 CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
3460 CGF.Builder.CreateStore(V,
3461 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3462
3463 // Copy the second element.
3464 V = CGF.Builder.CreateDefaultAlignedLoad(
3465 CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
3466 CharUnits Offset = CharUnits::fromQuantity(
3467 getDataLayout().getStructLayout(ST)->getElementOffset(1));
3468 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset));
3469
3470 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003471 } else if (neededInt) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003472 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
3473 CharUnits::fromQuantity(8));
3474 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Eli Friedmaneeb00622013-06-07 23:20:55 +00003475
3476 // Copy to a temporary if necessary to ensure the appropriate alignment.
3477 std::pair<CharUnits, CharUnits> SizeAlign =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003478 getContext().getTypeInfoInChars(Ty);
Eli Friedmaneeb00622013-06-07 23:20:55 +00003479 uint64_t TySize = SizeAlign.first.getQuantity();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003480 CharUnits TyAlign = SizeAlign.second;
3481
3482 // Copy into a temporary if the type is more aligned than the
3483 // register save area.
3484 if (TyAlign.getQuantity() > 8) {
3485 Address Tmp = CGF.CreateMemTemp(Ty);
3486 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
Eli Friedmaneeb00622013-06-07 23:20:55 +00003487 RegAddr = Tmp;
3488 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003489
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003490 } else if (neededSSE == 1) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003491 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3492 CharUnits::fromQuantity(16));
3493 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003494 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003495 assert(neededSSE == 2 && "Invalid number of needed registers!");
3496 // SSE registers are spaced 16 bytes apart in the register save
3497 // area, we need to collect the two eightbytes together.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003498 // The ABI isn't explicit about this, but it seems reasonable
3499 // to assume that the slots are 16-byte aligned, since the stack is
3500 // naturally 16-byte aligned and the prologue is expected to store
3501 // all the SSE registers to the RSA.
3502 Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3503 CharUnits::fromQuantity(16));
3504 Address RegAddrHi =
3505 CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
3506 CharUnits::fromQuantity(16));
Chris Lattner8b418682012-02-07 00:39:47 +00003507 llvm::Type *DoubleTy = CGF.DoubleTy;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003508 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003509 llvm::Value *V;
3510 Address Tmp = CGF.CreateMemTemp(Ty);
3511 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
3512 V = CGF.Builder.CreateLoad(
3513 CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy));
3514 CGF.Builder.CreateStore(V,
3515 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3516 V = CGF.Builder.CreateLoad(
3517 CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy));
3518 CGF.Builder.CreateStore(V,
3519 CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8)));
3520
3521 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003522 }
3523
3524 // AMD64-ABI 3.5.7p5: Step 5. Set:
3525 // l->gp_offset = l->gp_offset + num_gp * 8
3526 // l->fp_offset = l->fp_offset + num_fp * 16.
3527 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00003528 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003529 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
3530 gp_offset_p);
3531 }
3532 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00003533 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003534 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
3535 fp_offset_p);
3536 }
3537 CGF.EmitBranch(ContBlock);
3538
3539 // Emit code to load the value if it was passed in memory.
3540
3541 CGF.EmitBlock(InMemBlock);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003542 Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003543
3544 // Return the appropriate result.
3545
3546 CGF.EmitBlock(ContBlock);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003547 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
3548 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003549 return ResAddr;
3550}
3551
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003552Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
3553 QualType Ty) const {
3554 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3555 CGF.getContext().getTypeInfoInChars(Ty),
3556 CharUnits::fromQuantity(8),
3557 /*allowHigherAlign*/ false);
3558}
3559
Stephen Hines176edba2014-12-01 14:53:08 -08003560ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
3561 bool IsReturnType) const {
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003562
3563 if (Ty->isVoidType())
3564 return ABIArgInfo::getIgnore();
3565
3566 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3567 Ty = EnumTy->getDecl()->getIntegerType();
3568
Stephen Hines176edba2014-12-01 14:53:08 -08003569 TypeInfo Info = getContext().getTypeInfo(Ty);
3570 uint64_t Width = Info.Width;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003571 CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003572
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003573 const RecordType *RT = Ty->getAs<RecordType>();
3574 if (RT) {
3575 if (!IsReturnType) {
Mark Lacey23630722013-10-06 01:33:34 +00003576 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003577 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Timur Iskhodzhanoved23bdf2013-04-17 12:54:10 +00003578 }
3579
3580 if (RT->getDecl()->hasFlexibleArrayMember())
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003581 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003582
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003583 }
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00003584
Stephen Hines176edba2014-12-01 14:53:08 -08003585 // vectorcall adds the concept of a homogenous vector aggregate, similar to
3586 // other targets.
3587 const Type *Base = nullptr;
3588 uint64_t NumElts = 0;
3589 if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) {
3590 if (FreeSSERegs >= NumElts) {
3591 FreeSSERegs -= NumElts;
3592 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
3593 return ABIArgInfo::getDirect();
3594 return ABIArgInfo::getExpand();
3595 }
3596 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3597 }
3598
3599
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003600 if (Ty->isMemberPointerType()) {
3601 // If the member pointer is represented by an LLVM int or ptr, pass it
3602 // directly.
3603 llvm::Type *LLTy = CGT.ConvertType(Ty);
3604 if (LLTy->isPointerTy() || LLTy->isIntegerTy())
3605 return ABIArgInfo::getDirect();
3606 }
3607
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003608 if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00003609 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
3610 // not 1, 2, 4, or 8 bytes, must be passed by reference."
Stephen Hines176edba2014-12-01 14:53:08 -08003611 if (Width > 64 || !llvm::isPowerOf2_64(Width))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003612 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003613
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003614 // Otherwise, coerce it to a small integer.
Stephen Hines176edba2014-12-01 14:53:08 -08003615 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003616 }
3617
Stephen Hines176edba2014-12-01 14:53:08 -08003618 // Bool type is always extended to the ABI, other builtin types are not
3619 // extended.
3620 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3621 if (BT && BT->getKind() == BuiltinType::Bool)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003622 return ABIArgInfo::getExtend();
3623
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003624 // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It
3625 // passes them indirectly through memory.
3626 if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) {
3627 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
3628 if (LDF == &llvm::APFloat::x87DoubleExtended)
3629 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3630 }
3631
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003632 return ABIArgInfo::getDirect();
3633}
3634
3635void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines176edba2014-12-01 14:53:08 -08003636 bool IsVectorCall =
3637 FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall;
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003638
Stephen Hines176edba2014-12-01 14:53:08 -08003639 // We can use up to 4 SSE return registers with vectorcall.
3640 unsigned FreeSSERegs = IsVectorCall ? 4 : 0;
3641 if (!getCXXABI().classifyReturnType(FI))
3642 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true);
3643
3644 // We can use up to 6 SSE register parameters with vectorcall.
3645 FreeSSERegs = IsVectorCall ? 6 : 0;
Stephen Hines651f13c2014-04-23 16:59:28 -07003646 for (auto &I : FI.arguments())
Stephen Hines176edba2014-12-01 14:53:08 -08003647 I.info = classify(I.type, FreeSSERegs, false);
NAKAMURA Takumia7573222011-01-17 22:56:31 +00003648}
3649
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003650Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3651 QualType Ty) const {
3652 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3653 CGF.getContext().getTypeInfoInChars(Ty),
3654 CharUnits::fromQuantity(8),
3655 /*allowHigherAlign*/ false);
Chris Lattnerf13721d2010-08-31 16:44:54 +00003656}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003657
John McCallec853ba2010-03-11 00:10:12 +00003658// PowerPC-32
John McCallec853ba2010-03-11 00:10:12 +00003659namespace {
Stephen Hines176edba2014-12-01 14:53:08 -08003660/// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
3661class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003662bool IsSoftFloatABI;
John McCallec853ba2010-03-11 00:10:12 +00003663public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003664 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI)
3665 : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {}
Stephen Hines176edba2014-12-01 14:53:08 -08003666
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003667 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3668 QualType Ty) const override;
Stephen Hines176edba2014-12-01 14:53:08 -08003669};
3670
3671class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
3672public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003673 PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI)
3674 : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003675
Stephen Hines651f13c2014-04-23 16:59:28 -07003676 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallec853ba2010-03-11 00:10:12 +00003677 // This is recovered from gcc output.
3678 return 1; // r1 is the dedicated stack pointer
3679 }
3680
3681 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07003682 llvm::Value *Address) const override;
John McCallec853ba2010-03-11 00:10:12 +00003683};
3684
3685}
3686
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003687// TODO: this implementation is now likely redundant with
3688// DefaultABIInfo::EmitVAArg.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003689Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
3690 QualType Ty) const {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003691 const unsigned OverflowLimit = 8;
Stephen Hines176edba2014-12-01 14:53:08 -08003692 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
3693 // TODO: Implement this. For now ignore.
3694 (void)CTy;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003695 return Address::invalid(); // FIXME?
Stephen Hines176edba2014-12-01 14:53:08 -08003696 }
3697
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003698 // struct __va_list_tag {
3699 // unsigned char gpr;
3700 // unsigned char fpr;
3701 // unsigned short reserved;
3702 // void *overflow_arg_area;
3703 // void *reg_save_area;
3704 // };
3705
Stephen Hines176edba2014-12-01 14:53:08 -08003706 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003707 bool isInt =
3708 Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003709 bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
3710
3711 // All aggregates are passed indirectly? That doesn't seem consistent
3712 // with the argument-lowering code.
3713 bool isIndirect = Ty->isAggregateType();
Stephen Hines176edba2014-12-01 14:53:08 -08003714
3715 CGBuilderTy &Builder = CGF.Builder;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003716
3717 // The calling convention either uses 1-2 GPRs or 1 FPR.
3718 Address NumRegsAddr = Address::invalid();
3719 if (isInt || IsSoftFloatABI) {
3720 NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr");
3721 } else {
3722 NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr");
Stephen Hines176edba2014-12-01 14:53:08 -08003723 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003724
3725 llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
3726
3727 // "Align" the register count when TY is i64.
3728 if (isI64 || (isF64 && IsSoftFloatABI)) {
3729 NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
3730 NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
3731 }
Stephen Hines176edba2014-12-01 14:53:08 -08003732
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07003733 llvm::Value *CC =
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003734 Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
Stephen Hines176edba2014-12-01 14:53:08 -08003735
3736 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
3737 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
3738 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
3739
3740 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
3741
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003742 llvm::Type *DirectTy = CGF.ConvertType(Ty);
3743 if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
Stephen Hines176edba2014-12-01 14:53:08 -08003744
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003745 // Case 1: consume registers.
3746 Address RegAddr = Address::invalid();
3747 {
3748 CGF.EmitBlock(UsingRegs);
3749
3750 Address RegSaveAreaPtr =
3751 Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8));
3752 RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
3753 CharUnits::fromQuantity(8));
3754 assert(RegAddr.getElementType() == CGF.Int8Ty);
3755
3756 // Floating-point registers start after the general-purpose registers.
3757 if (!(isInt || IsSoftFloatABI)) {
3758 RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
3759 CharUnits::fromQuantity(32));
3760 }
3761
3762 // Get the address of the saved value by scaling the number of
3763 // registers we've used by the number of
3764 CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
3765 llvm::Value *RegOffset =
3766 Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
3767 RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
3768 RegAddr.getPointer(), RegOffset),
3769 RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
3770 RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
3771
3772 // Increase the used-register count.
3773 NumRegs =
3774 Builder.CreateAdd(NumRegs,
3775 Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
3776 Builder.CreateStore(NumRegs, NumRegsAddr);
3777
3778 CGF.EmitBranch(Cont);
Stephen Hines176edba2014-12-01 14:53:08 -08003779 }
Stephen Hines176edba2014-12-01 14:53:08 -08003780
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003781 // Case 2: consume space in the overflow area.
3782 Address MemAddr = Address::invalid();
3783 {
3784 CGF.EmitBlock(UsingOverflow);
Stephen Hines176edba2014-12-01 14:53:08 -08003785
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003786 Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
3787
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003788 // Everything in the overflow area is rounded up to a size of at least 4.
3789 CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
3790
3791 CharUnits Size;
3792 if (!isIndirect) {
3793 auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003794 Size = TypeInfo.first.alignTo(OverflowAreaAlign);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003795 } else {
3796 Size = CGF.getPointerSize();
3797 }
3798
3799 Address OverflowAreaAddr =
3800 Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4));
3801 Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
3802 OverflowAreaAlign);
3803 // Round up address of argument to alignment
3804 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3805 if (Align > OverflowAreaAlign) {
3806 llvm::Value *Ptr = OverflowArea.getPointer();
3807 OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
3808 Align);
3809 }
3810
3811 MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
3812
3813 // Increase the overflow area.
3814 OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
3815 Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
3816 CGF.EmitBranch(Cont);
3817 }
Stephen Hines176edba2014-12-01 14:53:08 -08003818
3819 CGF.EmitBlock(Cont);
3820
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003821 // Merge the cases with a phi.
3822 Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
3823 "vaarg.addr");
Stephen Hines176edba2014-12-01 14:53:08 -08003824
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003825 // Load the pointer if the argument was passed indirectly.
3826 if (isIndirect) {
3827 Result = Address(Builder.CreateLoad(Result, "aggr"),
3828 getContext().getTypeAlignInChars(Ty));
Stephen Hines176edba2014-12-01 14:53:08 -08003829 }
3830
3831 return Result;
3832}
3833
John McCallec853ba2010-03-11 00:10:12 +00003834bool
3835PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3836 llvm::Value *Address) const {
3837 // This is calculated from the LLVM and GCC tables and verified
3838 // against gcc output. AFAIK all ABIs use the same encoding.
3839
3840 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00003841
Chris Lattner8b418682012-02-07 00:39:47 +00003842 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00003843 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3844 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
3845 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
3846
3847 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00003848 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00003849
3850 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00003851 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00003852
3853 // 64-76 are various 4-byte special-purpose registers:
3854 // 64: mq
3855 // 65: lr
3856 // 66: ctr
3857 // 67: ap
3858 // 68-75 cr0-7
3859 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00003860 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00003861
3862 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00003863 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00003864
3865 // 109: vrsave
3866 // 110: vscr
3867 // 111: spe_acc
3868 // 112: spefscr
3869 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00003870 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00003871
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003872 return false;
John McCallec853ba2010-03-11 00:10:12 +00003873}
3874
Roman Divacky0fbc4b92012-05-09 18:22:46 +00003875// PowerPC-64
3876
3877namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003878/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003879class PPC64_SVR4_ABIInfo : public ABIInfo {
Stephen Hines176edba2014-12-01 14:53:08 -08003880public:
3881 enum ABIKind {
3882 ELFv1 = 0,
3883 ELFv2
3884 };
3885
3886private:
3887 static const unsigned GPRBits = 64;
3888 ABIKind Kind;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003889 bool HasQPX;
3890
3891 // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
3892 // will be passed in a QPX register.
3893 bool IsQPXVectorTy(const Type *Ty) const {
3894 if (!HasQPX)
3895 return false;
3896
3897 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3898 unsigned NumElements = VT->getNumElements();
3899 if (NumElements == 1)
3900 return false;
3901
3902 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
3903 if (getContext().getTypeSize(Ty) <= 256)
3904 return true;
3905 } else if (VT->getElementType()->
3906 isSpecificBuiltinType(BuiltinType::Float)) {
3907 if (getContext().getTypeSize(Ty) <= 128)
3908 return true;
3909 }
3910 }
3911
3912 return false;
3913 }
3914
3915 bool IsQPXVectorTy(QualType Ty) const {
3916 return IsQPXVectorTy(Ty.getTypePtr());
3917 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003918
3919public:
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003920 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07003921 : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {}
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003922
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00003923 bool isPromotableTypeForABI(QualType Ty) const;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003924 CharUnits getParamTypeAlignment(QualType Ty) const;
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00003925
3926 ABIArgInfo classifyReturnType(QualType RetTy) const;
3927 ABIArgInfo classifyArgumentType(QualType Ty) const;
3928
Stephen Hines176edba2014-12-01 14:53:08 -08003929 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
3930 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
3931 uint64_t Members) const override;
3932
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003933 // TODO: We can add more logic to computeInfo to improve performance.
3934 // Example: For aggregate arguments that fit in a register, we could
3935 // use getDirectInReg (as is done below for structs containing a single
3936 // floating-point value) to avoid pushing them to memory on function
3937 // entry. This would require changing the logic in PPCISelLowering
3938 // when lowering the parameters in the caller and args in the callee.
Stephen Hines651f13c2014-04-23 16:59:28 -07003939 void computeInfo(CGFunctionInfo &FI) const override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003940 if (!getCXXABI().classifyReturnType(FI))
3941 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines651f13c2014-04-23 16:59:28 -07003942 for (auto &I : FI.arguments()) {
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003943 // We rely on the default argument classification for the most part.
3944 // One exception: An aggregate containing a single floating-point
Bill Schmidtb1993102013-07-23 22:15:57 +00003945 // or vector item must be passed in a register if one is available.
Stephen Hines651f13c2014-04-23 16:59:28 -07003946 const Type *T = isSingleElementStruct(I.type, getContext());
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003947 if (T) {
3948 const BuiltinType *BT = T->getAs<BuiltinType>();
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003949 if (IsQPXVectorTy(T) ||
3950 (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003951 (BT && BT->isFloatingPoint())) {
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003952 QualType QT(T, 0);
Stephen Hines651f13c2014-04-23 16:59:28 -07003953 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003954 continue;
3955 }
3956 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003957 I.info = classifyArgumentType(I.type);
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00003958 }
3959 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003960
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003961 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3962 QualType Ty) const override;
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003963};
3964
3965class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003966
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003967public:
Stephen Hines176edba2014-12-01 14:53:08 -08003968 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003969 PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08003970 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {}
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003971
Stephen Hines651f13c2014-04-23 16:59:28 -07003972 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003973 // This is recovered from gcc output.
3974 return 1; // r1 is the dedicated stack pointer
3975 }
3976
3977 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07003978 llvm::Value *Address) const override;
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003979};
3980
Roman Divacky0fbc4b92012-05-09 18:22:46 +00003981class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3982public:
3983 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
3984
Stephen Hines651f13c2014-04-23 16:59:28 -07003985 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00003986 // This is recovered from gcc output.
3987 return 1; // r1 is the dedicated stack pointer
3988 }
3989
3990 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07003991 llvm::Value *Address) const override;
Roman Divacky0fbc4b92012-05-09 18:22:46 +00003992};
3993
3994}
3995
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00003996// Return true if the ABI requires Ty to be passed sign- or zero-
3997// extended to 64 bits.
3998bool
3999PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
4000 // Treat an enum type as its underlying type.
4001 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4002 Ty = EnumTy->getDecl()->getIntegerType();
4003
4004 // Promotable integer types are required to be promoted by the ABI.
4005 if (Ty->isPromotableIntegerType())
4006 return true;
4007
4008 // In addition to the usual promotable integer types, we also need to
4009 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
4010 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4011 switch (BT->getKind()) {
4012 case BuiltinType::Int:
4013 case BuiltinType::UInt:
4014 return true;
4015 default:
4016 break;
4017 }
4018
4019 return false;
4020}
4021
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004022/// isAlignedParamType - Determine whether a type requires 16-byte or
4023/// higher alignment in the parameter area. Always returns at least 8.
4024CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004025 // Complex types are passed just like their elements.
4026 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4027 Ty = CTy->getElementType();
4028
4029 // Only vector types of size 16 bytes need alignment (larger types are
4030 // passed via reference, smaller types are not aligned).
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004031 if (IsQPXVectorTy(Ty)) {
4032 if (getContext().getTypeSize(Ty) > 128)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004033 return CharUnits::fromQuantity(32);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004034
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004035 return CharUnits::fromQuantity(16);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004036 } else if (Ty->isVectorType()) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004037 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004038 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004039
4040 // For single-element float/vector structs, we consider the whole type
4041 // to have the same alignment requirements as its single element.
4042 const Type *AlignAsType = nullptr;
4043 const Type *EltType = isSingleElementStruct(Ty, getContext());
4044 if (EltType) {
4045 const BuiltinType *BT = EltType->getAs<BuiltinType>();
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004046 if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004047 getContext().getTypeSize(EltType) == 128) ||
4048 (BT && BT->isFloatingPoint()))
4049 AlignAsType = EltType;
4050 }
4051
Stephen Hines176edba2014-12-01 14:53:08 -08004052 // Likewise for ELFv2 homogeneous aggregates.
4053 const Type *Base = nullptr;
4054 uint64_t Members = 0;
4055 if (!AlignAsType && Kind == ELFv2 &&
4056 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
4057 AlignAsType = Base;
4058
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004059 // With special case aggregates, only vector base types need alignment.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004060 if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
4061 if (getContext().getTypeSize(AlignAsType) > 128)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004062 return CharUnits::fromQuantity(32);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004063
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004064 return CharUnits::fromQuantity(16);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004065 } else if (AlignAsType) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004066 return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004067 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004068
4069 // Otherwise, we only need alignment for any aggregate type that
4070 // has an alignment requirement of >= 16 bytes.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004071 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
4072 if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004073 return CharUnits::fromQuantity(32);
4074 return CharUnits::fromQuantity(16);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004075 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004076
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004077 return CharUnits::fromQuantity(8);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004078}
4079
Stephen Hines176edba2014-12-01 14:53:08 -08004080/// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
4081/// aggregate. Base is set to the base element type, and Members is set
4082/// to the number of base elements.
4083bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
4084 uint64_t &Members) const {
4085 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
4086 uint64_t NElements = AT->getSize().getZExtValue();
4087 if (NElements == 0)
4088 return false;
4089 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
4090 return false;
4091 Members *= NElements;
4092 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
4093 const RecordDecl *RD = RT->getDecl();
4094 if (RD->hasFlexibleArrayMember())
4095 return false;
4096
4097 Members = 0;
4098
4099 // If this is a C++ record, check the bases first.
4100 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
4101 for (const auto &I : CXXRD->bases()) {
4102 // Ignore empty records.
4103 if (isEmptyRecord(getContext(), I.getType(), true))
4104 continue;
4105
4106 uint64_t FldMembers;
4107 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
4108 return false;
4109
4110 Members += FldMembers;
4111 }
4112 }
4113
4114 for (const auto *FD : RD->fields()) {
4115 // Ignore (non-zero arrays of) empty records.
4116 QualType FT = FD->getType();
4117 while (const ConstantArrayType *AT =
4118 getContext().getAsConstantArrayType(FT)) {
4119 if (AT->getSize().getZExtValue() == 0)
4120 return false;
4121 FT = AT->getElementType();
4122 }
4123 if (isEmptyRecord(getContext(), FT, true))
4124 continue;
4125
4126 // For compatibility with GCC, ignore empty bitfields in C++ mode.
4127 if (getContext().getLangOpts().CPlusPlus &&
4128 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4129 continue;
4130
4131 uint64_t FldMembers;
4132 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
4133 return false;
4134
4135 Members = (RD->isUnion() ?
4136 std::max(Members, FldMembers) : Members + FldMembers);
4137 }
4138
4139 if (!Base)
4140 return false;
4141
4142 // Ensure there is no padding.
4143 if (getContext().getTypeSize(Base) * Members !=
4144 getContext().getTypeSize(Ty))
4145 return false;
4146 } else {
4147 Members = 1;
4148 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
4149 Members = 2;
4150 Ty = CT->getElementType();
4151 }
4152
4153 // Most ABIs only support float, double, and some vector type widths.
4154 if (!isHomogeneousAggregateBaseType(Ty))
4155 return false;
4156
4157 // The base type must be the same for all members. Types that
4158 // agree in both total size and mode (float vs. vector) are
4159 // treated as being equivalent here.
4160 const Type *TyPtr = Ty.getTypePtr();
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004161 if (!Base) {
Stephen Hines176edba2014-12-01 14:53:08 -08004162 Base = TyPtr;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004163 // If it's a non-power-of-2 vector, its size is already a power-of-2,
4164 // so make sure to widen it explicitly.
4165 if (const VectorType *VT = Base->getAs<VectorType>()) {
4166 QualType EltTy = VT->getElementType();
4167 unsigned NumElements =
4168 getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
4169 Base = getContext()
4170 .getVectorType(EltTy, NumElements, VT->getVectorKind())
4171 .getTypePtr();
4172 }
4173 }
Stephen Hines176edba2014-12-01 14:53:08 -08004174
4175 if (Base->isVectorType() != TyPtr->isVectorType() ||
4176 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
4177 return false;
4178 }
4179 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
4180}
4181
4182bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4183 // Homogeneous aggregates for ELFv2 must have base types of float,
4184 // double, long double, or 128-bit vectors.
4185 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4186 if (BT->getKind() == BuiltinType::Float ||
4187 BT->getKind() == BuiltinType::Double ||
4188 BT->getKind() == BuiltinType::LongDouble)
4189 return true;
4190 }
4191 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004192 if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
Stephen Hines176edba2014-12-01 14:53:08 -08004193 return true;
4194 }
4195 return false;
4196}
4197
4198bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
4199 const Type *Base, uint64_t Members) const {
4200 // Vector types require one register, floating point types require one
4201 // or two registers depending on their size.
4202 uint32_t NumRegs =
4203 Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64;
4204
4205 // Homogeneous Aggregates may occupy at most 8 registers.
4206 return Members * NumRegs <= 8;
4207}
4208
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00004209ABIArgInfo
4210PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Stephen Hines176edba2014-12-01 14:53:08 -08004211 Ty = useFirstFieldIfTransparentUnion(Ty);
4212
Bill Schmidtc9715fc2012-11-27 02:46:43 +00004213 if (Ty->isAnyComplexType())
4214 return ABIArgInfo::getDirect();
4215
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004216 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
4217 // or via reference (larger than 16 bytes).
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004218 if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004219 uint64_t Size = getContext().getTypeSize(Ty);
4220 if (Size > 128)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004221 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004222 else if (Size < 128) {
4223 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4224 return ABIArgInfo::getDirect(CoerceTy);
4225 }
4226 }
4227
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00004228 if (isAggregateTypeForABI(Ty)) {
Mark Lacey23630722013-10-06 01:33:34 +00004229 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004230 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00004231
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004232 uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
4233 uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
Stephen Hines176edba2014-12-01 14:53:08 -08004234
4235 // ELFv2 homogeneous aggregates are passed as array types.
4236 const Type *Base = nullptr;
4237 uint64_t Members = 0;
4238 if (Kind == ELFv2 &&
4239 isHomogeneousAggregate(Ty, Base, Members)) {
4240 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4241 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4242 return ABIArgInfo::getDirect(CoerceTy);
4243 }
4244
4245 // If an aggregate may end up fully in registers, we do not
4246 // use the ByVal method, but pass the aggregate as array.
4247 // This is usually beneficial since we avoid forcing the
4248 // back-end to store the argument to memory.
4249 uint64_t Bits = getContext().getTypeSize(Ty);
4250 if (Bits > 0 && Bits <= 8 * GPRBits) {
4251 llvm::Type *CoerceTy;
4252
4253 // Types up to 8 bytes are passed as integer type (which will be
4254 // properly aligned in the argument save area doubleword).
4255 if (Bits <= GPRBits)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004256 CoerceTy =
4257 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Stephen Hines176edba2014-12-01 14:53:08 -08004258 // Larger types are passed as arrays, with the base type selected
4259 // according to the required alignment in the save area.
4260 else {
4261 uint64_t RegBits = ABIAlign * 8;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004262 uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
Stephen Hines176edba2014-12-01 14:53:08 -08004263 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
4264 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
4265 }
4266
4267 return ABIArgInfo::getDirect(CoerceTy);
4268 }
4269
4270 // All other aggregates are passed ByVal.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004271 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
4272 /*ByVal=*/true,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004273 /*Realign=*/TyAlign > ABIAlign);
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00004274 }
4275
4276 return (isPromotableTypeForABI(Ty) ?
4277 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4278}
4279
4280ABIArgInfo
4281PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4282 if (RetTy->isVoidType())
4283 return ABIArgInfo::getIgnore();
4284
Bill Schmidt9e6111a2012-12-17 04:20:17 +00004285 if (RetTy->isAnyComplexType())
4286 return ABIArgInfo::getDirect();
4287
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004288 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
4289 // or via reference (larger than 16 bytes).
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07004290 if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004291 uint64_t Size = getContext().getTypeSize(RetTy);
4292 if (Size > 128)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004293 return getNaturalAlignIndirect(RetTy);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004294 else if (Size < 128) {
4295 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4296 return ABIArgInfo::getDirect(CoerceTy);
4297 }
4298 }
4299
Stephen Hines176edba2014-12-01 14:53:08 -08004300 if (isAggregateTypeForABI(RetTy)) {
4301 // ELFv2 homogeneous aggregates are returned as array types.
4302 const Type *Base = nullptr;
4303 uint64_t Members = 0;
4304 if (Kind == ELFv2 &&
4305 isHomogeneousAggregate(RetTy, Base, Members)) {
4306 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4307 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4308 return ABIArgInfo::getDirect(CoerceTy);
4309 }
4310
4311 // ELFv2 small aggregates are returned in up to two registers.
4312 uint64_t Bits = getContext().getTypeSize(RetTy);
4313 if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
4314 if (Bits == 0)
4315 return ABIArgInfo::getIgnore();
4316
4317 llvm::Type *CoerceTy;
4318 if (Bits > GPRBits) {
4319 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004320 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr);
Stephen Hines176edba2014-12-01 14:53:08 -08004321 } else
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004322 CoerceTy =
4323 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Stephen Hines176edba2014-12-01 14:53:08 -08004324 return ABIArgInfo::getDirect(CoerceTy);
4325 }
4326
4327 // All other aggregates are returned indirectly.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004328 return getNaturalAlignIndirect(RetTy);
Stephen Hines176edba2014-12-01 14:53:08 -08004329 }
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00004330
4331 return (isPromotableTypeForABI(RetTy) ?
4332 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4333}
4334
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004335// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004336Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4337 QualType Ty) const {
4338 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4339 TypeInfo.second = getParamTypeAlignment(Ty);
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004340
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004341 CharUnits SlotSize = CharUnits::fromQuantity(8);
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004342
Bill Schmidt19f8e852013-01-14 17:45:36 +00004343 // If we have a complex type and the base type is smaller than 8 bytes,
4344 // the ABI calls for the real and imaginary parts to be right-adjusted
4345 // in separate doublewords. However, Clang expects us to produce a
4346 // pointer to a structure with the two parts packed tightly. So generate
4347 // loads of the real and imaginary parts relative to the va_list pointer,
4348 // and store them to a temporary structure.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004349 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4350 CharUnits EltSize = TypeInfo.first / 2;
4351 if (EltSize < SlotSize) {
4352 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
4353 SlotSize * 2, SlotSize,
4354 SlotSize, /*AllowHigher*/ true);
4355
4356 Address RealAddr = Addr;
4357 Address ImagAddr = RealAddr;
4358 if (CGF.CGM.getDataLayout().isBigEndian()) {
4359 RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
4360 SlotSize - EltSize);
4361 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
4362 2 * SlotSize - EltSize);
4363 } else {
4364 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
4365 }
4366
4367 llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
4368 RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
4369 ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
4370 llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
4371 llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
4372
4373 Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
4374 CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
4375 /*init*/ true);
4376 return Temp;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004377 }
Bill Schmidt19f8e852013-01-14 17:45:36 +00004378 }
4379
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004380 // Otherwise, just use the general rule.
4381 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
4382 TypeInfo, SlotSize, /*AllowHigher*/ true);
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004383}
4384
4385static bool
4386PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4387 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004388 // This is calculated from the LLVM and GCC tables and verified
4389 // against gcc output. AFAIK all ABIs use the same encoding.
4390
4391 CodeGen::CGBuilderTy &Builder = CGF.Builder;
4392
4393 llvm::IntegerType *i8 = CGF.Int8Ty;
4394 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4395 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4396 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4397
4398 // 0-31: r0-31, the 8-byte general-purpose registers
4399 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
4400
4401 // 32-63: fp0-31, the 8-byte floating-point registers
4402 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4403
4404 // 64-76 are various 4-byte special-purpose registers:
4405 // 64: mq
4406 // 65: lr
4407 // 66: ctr
4408 // 67: ap
4409 // 68-75 cr0-7
4410 // 76: xer
4411 AssignToArrayRange(Builder, Address, Four8, 64, 76);
4412
4413 // 77-108: v0-31, the 16-byte vector registers
4414 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4415
4416 // 109: vrsave
4417 // 110: vscr
4418 // 111: spe_acc
4419 // 112: spefscr
4420 // 113: sfp
4421 AssignToArrayRange(Builder, Address, Four8, 109, 113);
4422
4423 return false;
4424}
John McCallec853ba2010-03-11 00:10:12 +00004425
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004426bool
4427PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
4428 CodeGen::CodeGenFunction &CGF,
4429 llvm::Value *Address) const {
4430
4431 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4432}
4433
4434bool
4435PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4436 llvm::Value *Address) const {
4437
4438 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4439}
4440
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004441//===----------------------------------------------------------------------===//
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004442// AArch64 ABI Implementation
Stephen Hines651f13c2014-04-23 16:59:28 -07004443//===----------------------------------------------------------------------===//
4444
4445namespace {
4446
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004447class AArch64ABIInfo : public SwiftABIInfo {
Stephen Hines651f13c2014-04-23 16:59:28 -07004448public:
4449 enum ABIKind {
4450 AAPCS = 0,
4451 DarwinPCS
4452 };
4453
4454private:
4455 ABIKind Kind;
4456
4457public:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004458 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
4459 : SwiftABIInfo(CGT), Kind(Kind) {}
Stephen Hines651f13c2014-04-23 16:59:28 -07004460
4461private:
4462 ABIKind getABIKind() const { return Kind; }
4463 bool isDarwinPCS() const { return Kind == DarwinPCS; }
4464
4465 ABIArgInfo classifyReturnType(QualType RetTy) const;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004466 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Stephen Hines176edba2014-12-01 14:53:08 -08004467 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4468 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4469 uint64_t Members) const override;
4470
Stephen Hines651f13c2014-04-23 16:59:28 -07004471 bool isIllegalVectorType(QualType Ty) const;
4472
Stephen Hines176edba2014-12-01 14:53:08 -08004473 void computeInfo(CGFunctionInfo &FI) const override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004474 if (!getCXXABI().classifyReturnType(FI))
4475 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004476
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004477 for (auto &it : FI.arguments())
4478 it.info = classifyArgumentType(it.type);
Stephen Hines651f13c2014-04-23 16:59:28 -07004479 }
4480
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004481 Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4482 CodeGenFunction &CGF) const;
Stephen Hines651f13c2014-04-23 16:59:28 -07004483
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004484 Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
4485 CodeGenFunction &CGF) const;
Stephen Hines651f13c2014-04-23 16:59:28 -07004486
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004487 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4488 QualType Ty) const override {
Stephen Hines651f13c2014-04-23 16:59:28 -07004489 return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
4490 : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
4491 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004492
4493 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
4494 ArrayRef<llvm::Type*> scalars,
4495 bool asReturnValue) const override {
4496 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
4497 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004498};
4499
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004500class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
Stephen Hines651f13c2014-04-23 16:59:28 -07004501public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004502 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
4503 : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
Stephen Hines651f13c2014-04-23 16:59:28 -07004504
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07004505 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
Stephen Hines651f13c2014-04-23 16:59:28 -07004506 return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue";
4507 }
4508
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07004509 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4510 return 31;
4511 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004512
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07004513 bool doesReturnSlotInterfereWithArgs() const override { return false; }
Stephen Hines651f13c2014-04-23 16:59:28 -07004514};
4515}
4516
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004517ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
Stephen Hines176edba2014-12-01 14:53:08 -08004518 Ty = useFirstFieldIfTransparentUnion(Ty);
4519
Stephen Hines651f13c2014-04-23 16:59:28 -07004520 // Handle illegal vector types here.
4521 if (isIllegalVectorType(Ty)) {
4522 uint64_t Size = getContext().getTypeSize(Ty);
Tim Murray9212d4f2014-08-15 16:00:15 -07004523 // Android promotes <2 x i8> to i16, not i32
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004524 if (isAndroid() && (Size <= 16)) {
Tim Murray9212d4f2014-08-15 16:00:15 -07004525 llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
Tim Murray9212d4f2014-08-15 16:00:15 -07004526 return ABIArgInfo::getDirect(ResType);
4527 }
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004528 if (Size <= 32) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004529 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
Stephen Hines651f13c2014-04-23 16:59:28 -07004530 return ABIArgInfo::getDirect(ResType);
4531 }
4532 if (Size == 64) {
4533 llvm::Type *ResType =
4534 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
Stephen Hines651f13c2014-04-23 16:59:28 -07004535 return ABIArgInfo::getDirect(ResType);
4536 }
4537 if (Size == 128) {
4538 llvm::Type *ResType =
4539 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
Stephen Hines651f13c2014-04-23 16:59:28 -07004540 return ABIArgInfo::getDirect(ResType);
4541 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004542 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07004543 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004544
4545 if (!isAggregateTypeForABI(Ty)) {
4546 // Treat an enum type as its underlying type.
4547 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4548 Ty = EnumTy->getDecl()->getIntegerType();
4549
Stephen Hines651f13c2014-04-23 16:59:28 -07004550 return (Ty->isPromotableIntegerType() && isDarwinPCS()
4551 ? ABIArgInfo::getExtend()
4552 : ABIArgInfo::getDirect());
4553 }
4554
4555 // Structures with either a non-trivial destructor or a non-trivial
4556 // copy constructor are always indirect.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004557 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004558 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
4559 CGCXXABI::RAA_DirectInMemory);
Stephen Hines651f13c2014-04-23 16:59:28 -07004560 }
4561
4562 // Empty records are always ignored on Darwin, but actually passed in C++ mode
4563 // elsewhere for GNU compatibility.
4564 if (isEmptyRecord(getContext(), Ty, true)) {
4565 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
4566 return ABIArgInfo::getIgnore();
4567
Stephen Hines651f13c2014-04-23 16:59:28 -07004568 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4569 }
4570
4571 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004572 const Type *Base = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004573 uint64_t Members = 0;
Stephen Hines176edba2014-12-01 14:53:08 -08004574 if (isHomogeneousAggregate(Ty, Base, Members)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004575 return ABIArgInfo::getDirect(
4576 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
Stephen Hines651f13c2014-04-23 16:59:28 -07004577 }
4578
4579 // Aggregates <= 16 bytes are passed directly in registers or on the stack.
4580 uint64_t Size = getContext().getTypeSize(Ty);
4581 if (Size <= 128) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004582 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
4583 // same size and alignment.
4584 if (getTarget().isRenderScriptTarget()) {
Matt Wala1d151512015-08-10 15:58:40 -07004585 return coerceToIntArray(Ty, getContext(), getVMContext());
4586 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004587 unsigned Alignment = getContext().getTypeAlign(Ty);
Stephen Hines651f13c2014-04-23 16:59:28 -07004588 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004589
Stephen Hines651f13c2014-04-23 16:59:28 -07004590 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4591 // For aggregates with 16-byte alignment, we use i128.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004592 if (Alignment < 128 && Size == 128) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004593 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4594 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4595 }
4596 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4597 }
4598
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004599 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07004600}
4601
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004602ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07004603 if (RetTy->isVoidType())
4604 return ABIArgInfo::getIgnore();
4605
4606 // Large vector types should be returned via memory.
4607 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004608 return getNaturalAlignIndirect(RetTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004609
4610 if (!isAggregateTypeForABI(RetTy)) {
4611 // Treat an enum type as its underlying type.
4612 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4613 RetTy = EnumTy->getDecl()->getIntegerType();
4614
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004615 return (RetTy->isPromotableIntegerType() && isDarwinPCS()
4616 ? ABIArgInfo::getExtend()
4617 : ABIArgInfo::getDirect());
Stephen Hines651f13c2014-04-23 16:59:28 -07004618 }
4619
Stephen Hines651f13c2014-04-23 16:59:28 -07004620 if (isEmptyRecord(getContext(), RetTy, true))
4621 return ABIArgInfo::getIgnore();
4622
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004623 const Type *Base = nullptr;
Stephen Hines176edba2014-12-01 14:53:08 -08004624 uint64_t Members = 0;
4625 if (isHomogeneousAggregate(RetTy, Base, Members))
Stephen Hines651f13c2014-04-23 16:59:28 -07004626 // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
4627 return ABIArgInfo::getDirect();
4628
4629 // Aggregates <= 16 bytes are returned directly in registers or on the stack.
4630 uint64_t Size = getContext().getTypeSize(RetTy);
4631 if (Size <= 128) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004632 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
4633 // same size and alignment.
4634 if (getTarget().isRenderScriptTarget()) {
Matt Wala1d151512015-08-10 15:58:40 -07004635 return coerceToIntArray(RetTy, getContext(), getVMContext());
4636 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07004637 unsigned Alignment = getContext().getTypeAlign(RetTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004638 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07004639
4640 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4641 // For aggregates with 16-byte alignment, we use i128.
4642 if (Alignment < 128 && Size == 128) {
4643 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4644 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4645 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004646 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4647 }
4648
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004649 return getNaturalAlignIndirect(RetTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004650}
4651
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004652/// isIllegalVectorType - check whether the vector type is legal for AArch64.
4653bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07004654 if (const VectorType *VT = Ty->getAs<VectorType>()) {
4655 // Check whether VT is legal.
4656 unsigned NumElements = VT->getNumElements();
4657 uint64_t Size = getContext().getTypeSize(VT);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004658 // NumElements should be power of 2.
4659 if (!llvm::isPowerOf2_32(NumElements))
Stephen Hines651f13c2014-04-23 16:59:28 -07004660 return true;
4661 return Size != 64 && (Size != 128 || NumElements == 1);
4662 }
4663 return false;
4664}
4665
Stephen Hines176edba2014-12-01 14:53:08 -08004666bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4667 // Homogeneous aggregates for AAPCS64 must have base types of a floating
4668 // point type or a short-vector type. This is the same as the 32-bit ABI,
4669 // but with the difference that any floating-point type is allowed,
4670 // including __fp16.
4671 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4672 if (BT->isFloatingPoint())
4673 return true;
4674 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
4675 unsigned VecSize = getContext().getTypeSize(VT);
4676 if (VecSize == 64 || VecSize == 128)
4677 return true;
4678 }
4679 return false;
4680}
4681
4682bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
4683 uint64_t Members) const {
4684 return Members <= 4;
4685}
4686
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004687Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004688 QualType Ty,
4689 CodeGenFunction &CGF) const {
4690 ABIArgInfo AI = classifyArgumentType(Ty);
Stephen Hines176edba2014-12-01 14:53:08 -08004691 bool IsIndirect = AI.isIndirect();
4692
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004693 llvm::Type *BaseTy = CGF.ConvertType(Ty);
4694 if (IsIndirect)
4695 BaseTy = llvm::PointerType::getUnqual(BaseTy);
4696 else if (AI.getCoerceToType())
4697 BaseTy = AI.getCoerceToType();
4698
4699 unsigned NumRegs = 1;
4700 if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
4701 BaseTy = ArrTy->getElementType();
4702 NumRegs = ArrTy->getNumElements();
4703 }
4704 bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
4705
Stephen Hines651f13c2014-04-23 16:59:28 -07004706 // The AArch64 va_list type and handling is specified in the Procedure Call
4707 // Standard, section B.4:
4708 //
4709 // struct {
4710 // void *__stack;
4711 // void *__gr_top;
4712 // void *__vr_top;
4713 // int __gr_offs;
4714 // int __vr_offs;
4715 // };
4716
4717 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
4718 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4719 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
4720 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
Stephen Hines651f13c2014-04-23 16:59:28 -07004721
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004722 auto TyInfo = getContext().getTypeInfoInChars(Ty);
4723 CharUnits TyAlign = TyInfo.second;
4724
4725 Address reg_offs_p = Address::invalid();
4726 llvm::Value *reg_offs = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004727 int reg_top_index;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004728 CharUnits reg_top_offset;
4729 int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004730 if (!IsFPR) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004731 // 3 is the field number of __gr_offs
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07004732 reg_offs_p =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004733 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
4734 "gr_offs_p");
Stephen Hines651f13c2014-04-23 16:59:28 -07004735 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
4736 reg_top_index = 1; // field number for __gr_top
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004737 reg_top_offset = CharUnits::fromQuantity(8);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004738 RegSize = llvm::alignTo(RegSize, 8);
Stephen Hines651f13c2014-04-23 16:59:28 -07004739 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -07004740 // 4 is the field number of __vr_offs.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07004741 reg_offs_p =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004742 CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28),
4743 "vr_offs_p");
Stephen Hines651f13c2014-04-23 16:59:28 -07004744 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
4745 reg_top_index = 2; // field number for __vr_top
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004746 reg_top_offset = CharUnits::fromQuantity(16);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004747 RegSize = 16 * NumRegs;
Stephen Hines651f13c2014-04-23 16:59:28 -07004748 }
4749
4750 //=======================================
4751 // Find out where argument was passed
4752 //=======================================
4753
4754 // If reg_offs >= 0 we're already using the stack for this type of
4755 // argument. We don't want to keep updating reg_offs (in case it overflows,
4756 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
4757 // whatever they get).
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004758 llvm::Value *UsingStack = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004759 UsingStack = CGF.Builder.CreateICmpSGE(
4760 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
4761
4762 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
4763
4764 // Otherwise, at least some kind of argument could go in these registers, the
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004765 // question is whether this particular type is too big.
Stephen Hines651f13c2014-04-23 16:59:28 -07004766 CGF.EmitBlock(MaybeRegBlock);
4767
4768 // Integer arguments may need to correct register alignment (for example a
4769 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
4770 // align __gr_offs to calculate the potential address.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004771 if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
4772 int Align = TyAlign.getQuantity();
Stephen Hines651f13c2014-04-23 16:59:28 -07004773
4774 reg_offs = CGF.Builder.CreateAdd(
4775 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
4776 "align_regoffs");
4777 reg_offs = CGF.Builder.CreateAnd(
4778 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
4779 "aligned_regoffs");
4780 }
4781
4782 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004783 // The fact that this is done unconditionally reflects the fact that
4784 // allocating an argument to the stack also uses up all the remaining
4785 // registers of the appropriate kind.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004786 llvm::Value *NewOffset = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004787 NewOffset = CGF.Builder.CreateAdd(
4788 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
4789 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
4790
4791 // Now we're in a position to decide whether this argument really was in
4792 // registers or not.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004793 llvm::Value *InRegs = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004794 InRegs = CGF.Builder.CreateICmpSLE(
4795 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
4796
4797 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
4798
4799 //=======================================
4800 // Argument was in registers
4801 //=======================================
4802
4803 // Now we emit the code for if the argument was originally passed in
4804 // registers. First start the appropriate block:
4805 CGF.EmitBlock(InRegBlock);
4806
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004807 llvm::Value *reg_top = nullptr;
4808 Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index,
4809 reg_top_offset, "reg_top_p");
Stephen Hines651f13c2014-04-23 16:59:28 -07004810 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004811 Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
4812 CharUnits::fromQuantity(IsFPR ? 16 : 8));
4813 Address RegAddr = Address::invalid();
4814 llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
Stephen Hines651f13c2014-04-23 16:59:28 -07004815
4816 if (IsIndirect) {
4817 // If it's been passed indirectly (actually a struct), whatever we find from
4818 // stored registers or on the stack will actually be a struct **.
4819 MemTy = llvm::PointerType::getUnqual(MemTy);
4820 }
4821
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004822 const Type *Base = nullptr;
Stephen Hines176edba2014-12-01 14:53:08 -08004823 uint64_t NumMembers = 0;
4824 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004825 if (IsHFA && NumMembers > 1) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004826 // Homogeneous aggregates passed in registers will have their elements split
4827 // and stored 16-bytes apart regardless of size (they're notionally in qN,
4828 // qN+1, ...). We reload and store into a temporary local variable
4829 // contiguously.
4830 assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004831 auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
Stephen Hines651f13c2014-04-23 16:59:28 -07004832 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4833 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004834 Address Tmp = CGF.CreateTempAlloca(HFATy,
4835 std::max(TyAlign, BaseTyInfo.second));
Stephen Hines651f13c2014-04-23 16:59:28 -07004836
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004837 // On big-endian platforms, the value will be right-aligned in its slot.
4838 int Offset = 0;
4839 if (CGF.CGM.getDataLayout().isBigEndian() &&
4840 BaseTyInfo.first.getQuantity() < 16)
4841 Offset = 16 - BaseTyInfo.first.getQuantity();
4842
Stephen Hines651f13c2014-04-23 16:59:28 -07004843 for (unsigned i = 0; i < NumMembers; ++i) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004844 CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
4845 Address LoadAddr =
4846 CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
4847 LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
4848
4849 Address StoreAddr =
4850 CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first);
Stephen Hines651f13c2014-04-23 16:59:28 -07004851
4852 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4853 CGF.Builder.CreateStore(Elem, StoreAddr);
4854 }
4855
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004856 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004857 } else {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004858 // Otherwise the object is contiguous in memory.
4859
4860 // It might be right-aligned in its slot.
4861 CharUnits SlotSize = BaseAddr.getAlignment();
4862 if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004863 (IsHFA || !isAggregateTypeForABI(Ty)) &&
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004864 TyInfo.first < SlotSize) {
4865 CharUnits Offset = SlotSize - TyInfo.first;
4866 BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
Stephen Hines651f13c2014-04-23 16:59:28 -07004867 }
4868
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004869 RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004870 }
4871
4872 CGF.EmitBranch(ContBlock);
4873
4874 //=======================================
4875 // Argument was on the stack
4876 //=======================================
4877 CGF.EmitBlock(OnStackBlock);
4878
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004879 Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0,
4880 CharUnits::Zero(), "stack_p");
4881 llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
Stephen Hines651f13c2014-04-23 16:59:28 -07004882
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004883 // Again, stack arguments may need realignment. In this case both integer and
Stephen Hines651f13c2014-04-23 16:59:28 -07004884 // floating-point ones might be affected.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004885 if (!IsIndirect && TyAlign.getQuantity() > 8) {
4886 int Align = TyAlign.getQuantity();
Stephen Hines651f13c2014-04-23 16:59:28 -07004887
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004888 OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
Stephen Hines651f13c2014-04-23 16:59:28 -07004889
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004890 OnStackPtr = CGF.Builder.CreateAdd(
4891 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
Stephen Hines651f13c2014-04-23 16:59:28 -07004892 "align_stack");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004893 OnStackPtr = CGF.Builder.CreateAnd(
4894 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
Stephen Hines651f13c2014-04-23 16:59:28 -07004895 "align_stack");
4896
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004897 OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004898 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004899 Address OnStackAddr(OnStackPtr,
4900 std::max(CharUnits::fromQuantity(8), TyAlign));
Stephen Hines651f13c2014-04-23 16:59:28 -07004901
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004902 // All stack slots are multiples of 8 bytes.
4903 CharUnits StackSlotSize = CharUnits::fromQuantity(8);
4904 CharUnits StackSize;
Stephen Hines651f13c2014-04-23 16:59:28 -07004905 if (IsIndirect)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004906 StackSize = StackSlotSize;
Stephen Hines651f13c2014-04-23 16:59:28 -07004907 else
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004908 StackSize = TyInfo.first.alignTo(StackSlotSize);
Stephen Hines651f13c2014-04-23 16:59:28 -07004909
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004910 llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
Stephen Hines651f13c2014-04-23 16:59:28 -07004911 llvm::Value *NewStack =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004912 CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
Stephen Hines651f13c2014-04-23 16:59:28 -07004913
4914 // Write the new value of __stack for the next call to va_arg
4915 CGF.Builder.CreateStore(NewStack, stack_p);
4916
4917 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004918 TyInfo.first < StackSlotSize) {
4919 CharUnits Offset = StackSlotSize - TyInfo.first;
4920 OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
Stephen Hines651f13c2014-04-23 16:59:28 -07004921 }
4922
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004923 OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
Stephen Hines651f13c2014-04-23 16:59:28 -07004924
4925 CGF.EmitBranch(ContBlock);
4926
4927 //=======================================
4928 // Tidy up
4929 //=======================================
4930 CGF.EmitBlock(ContBlock);
4931
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004932 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
4933 OnStackAddr, OnStackBlock, "vaargs.addr");
Stephen Hines651f13c2014-04-23 16:59:28 -07004934
4935 if (IsIndirect)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004936 return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
4937 TyInfo.second);
Stephen Hines651f13c2014-04-23 16:59:28 -07004938
4939 return ResAddr;
4940}
4941
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004942Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4943 CodeGenFunction &CGF) const {
4944 // The backend's lowering doesn't support va_arg for aggregates or
4945 // illegal vector types. Lower VAArg here for these cases and use
4946 // the LLVM va_arg instruction for everything else.
Stephen Hines651f13c2014-04-23 16:59:28 -07004947 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004948 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Stephen Hines651f13c2014-04-23 16:59:28 -07004949
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004950 CharUnits SlotSize = CharUnits::fromQuantity(8);
Stephen Hines651f13c2014-04-23 16:59:28 -07004951
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004952 // Empty records are ignored for parameter passing purposes.
Stephen Hines651f13c2014-04-23 16:59:28 -07004953 if (isEmptyRecord(getContext(), Ty, true)) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004954 Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
4955 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
4956 return Addr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004957 }
4958
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004959 // The size of the actual thing passed, which might end up just
4960 // being a pointer for indirect types.
4961 auto TyInfo = getContext().getTypeInfoInChars(Ty);
4962
4963 // Arguments bigger than 16 bytes which aren't homogeneous
4964 // aggregates should be passed indirectly.
4965 bool IsIndirect = false;
4966 if (TyInfo.first.getQuantity() > 16) {
4967 const Type *Base = nullptr;
4968 uint64_t Members = 0;
4969 IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
Stephen Hines651f13c2014-04-23 16:59:28 -07004970 }
4971
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004972 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4973 TyInfo, SlotSize, /*AllowHigherAlign*/ true);
Stephen Hines651f13c2014-04-23 16:59:28 -07004974}
4975
4976//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004977// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00004978//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004979
4980namespace {
4981
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004982class ARMABIInfo : public SwiftABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00004983public:
4984 enum ABIKind {
4985 APCS = 0,
4986 AAPCS = 1,
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08004987 AAPCS_VFP = 2,
4988 AAPCS16_VFP = 3,
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00004989 };
4990
4991private:
4992 ABIKind Kind;
4993
4994public:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07004995 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
4996 : SwiftABIInfo(CGT), Kind(_Kind) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004997 setCCs();
John McCallbd7370a2013-02-28 19:01:20 +00004998 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00004999
John McCall49e34be2011-08-30 01:42:09 +00005000 bool isEABI() const {
Stephen Hines651f13c2014-04-23 16:59:28 -07005001 switch (getTarget().getTriple().getEnvironment()) {
5002 case llvm::Triple::Android:
5003 case llvm::Triple::EABI:
5004 case llvm::Triple::EABIHF:
5005 case llvm::Triple::GNUEABI:
5006 case llvm::Triple::GNUEABIHF:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005007 case llvm::Triple::MuslEABI:
5008 case llvm::Triple::MuslEABIHF:
Stephen Hines651f13c2014-04-23 16:59:28 -07005009 return true;
5010 default:
5011 return false;
5012 }
5013 }
5014
5015 bool isEABIHF() const {
5016 switch (getTarget().getTriple().getEnvironment()) {
5017 case llvm::Triple::EABIHF:
5018 case llvm::Triple::GNUEABIHF:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005019 case llvm::Triple::MuslEABIHF:
Stephen Hines651f13c2014-04-23 16:59:28 -07005020 return true;
5021 default:
5022 return false;
5023 }
John McCall49e34be2011-08-30 01:42:09 +00005024 }
5025
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00005026 ABIKind getABIKind() const { return Kind; }
5027
Tim Northover64eac852013-10-01 14:34:25 +00005028private:
Stephen Hines651f13c2014-04-23 16:59:28 -07005029 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005030 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const;
Manman Ren97f81572012-10-16 19:18:39 +00005031 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005032
Stephen Hines176edba2014-12-01 14:53:08 -08005033 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5034 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5035 uint64_t Members) const override;
5036
Stephen Hines651f13c2014-04-23 16:59:28 -07005037 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005038
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005039 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5040 QualType Ty) const override;
John McCallbd7370a2013-02-28 19:01:20 +00005041
5042 llvm::CallingConv::ID getLLVMDefaultCC() const;
5043 llvm::CallingConv::ID getABIDefaultCC() const;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005044 void setCCs();
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005045
5046 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
5047 ArrayRef<llvm::Type*> scalars,
5048 bool asReturnValue) const override {
5049 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5050 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005051};
5052
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005053class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
5054public:
Chris Lattnerea044322010-07-29 02:01:43 +00005055 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5056 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00005057
John McCall49e34be2011-08-30 01:42:09 +00005058 const ARMABIInfo &getABIInfo() const {
5059 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
5060 }
5061
Stephen Hines651f13c2014-04-23 16:59:28 -07005062 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCall6374c332010-03-06 00:35:14 +00005063 return 13;
5064 }
Roman Divacky09345d12011-05-18 19:36:54 +00005065
Stephen Hines651f13c2014-04-23 16:59:28 -07005066 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
John McCallf85e1932011-06-15 23:02:42 +00005067 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
5068 }
5069
Roman Divacky09345d12011-05-18 19:36:54 +00005070 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07005071 llvm::Value *Address) const override {
Chris Lattner8b418682012-02-07 00:39:47 +00005072 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00005073
5074 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00005075 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00005076 return false;
5077 }
John McCall49e34be2011-08-30 01:42:09 +00005078
Stephen Hines651f13c2014-04-23 16:59:28 -07005079 unsigned getSizeOfUnwindException() const override {
John McCall49e34be2011-08-30 01:42:09 +00005080 if (getABIInfo().isEABI()) return 88;
5081 return TargetCodeGenInfo::getSizeOfUnwindException();
5082 }
Tim Northover64eac852013-10-01 14:34:25 +00005083
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005084 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -07005085 CodeGen::CodeGenModule &CGM) const override {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005086 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Tim Northover64eac852013-10-01 14:34:25 +00005087 if (!FD)
5088 return;
5089
5090 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
5091 if (!Attr)
5092 return;
5093
5094 const char *Kind;
5095 switch (Attr->getInterrupt()) {
5096 case ARMInterruptAttr::Generic: Kind = ""; break;
5097 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
5098 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
5099 case ARMInterruptAttr::SWI: Kind = "SWI"; break;
5100 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
5101 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
5102 }
5103
5104 llvm::Function *Fn = cast<llvm::Function>(GV);
5105
5106 Fn->addFnAttr("interrupt", Kind);
5107
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005108 ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
5109 if (ABI == ARMABIInfo::APCS)
Tim Northover64eac852013-10-01 14:34:25 +00005110 return;
5111
5112 // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
5113 // however this is not necessarily true on taking any interrupt. Instruct
5114 // the backend to perform a realignment as part of the function prologue.
5115 llvm::AttrBuilder B;
5116 B.addStackAlignmentAttr(8);
5117 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
5118 llvm::AttributeSet::get(CGM.getLLVMContext(),
5119 llvm::AttributeSet::FunctionIndex,
5120 B));
5121 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00005122};
5123
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005124class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005125public:
5126 WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5127 : ARMTargetCodeGenInfo(CGT, K) {}
5128
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005129 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005130 CodeGen::CodeGenModule &CGM) const override;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005131
5132 void getDependentLibraryOption(llvm::StringRef Lib,
5133 llvm::SmallString<24> &Opt) const override {
5134 Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5135 }
5136
5137 void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5138 llvm::SmallString<32> &Opt) const override {
5139 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5140 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005141};
5142
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005143void WindowsARMTargetCodeGenInfo::setTargetAttributes(
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005144 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005145 ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005146 addStackProbeSizeTargetAttribute(D, GV, CGM);
5147}
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00005148}
5149
Chris Lattneree5dcd02010-07-29 02:31:05 +00005150void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005151 if (!getCXXABI().classifyReturnType(FI))
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005152 FI.getReturnInfo() =
5153 classifyReturnType(FI.getReturnType(), FI.isVariadic());
Stephen Hines651f13c2014-04-23 16:59:28 -07005154
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005155 for (auto &I : FI.arguments())
5156 I.info = classifyArgumentType(I.type, FI.isVariadic());
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00005157
Anton Korobeynikov414d8962011-04-14 20:06:49 +00005158 // Always honor user-specified calling convention.
5159 if (FI.getCallingConvention() != llvm::CallingConv::C)
5160 return;
5161
John McCallbd7370a2013-02-28 19:01:20 +00005162 llvm::CallingConv::ID cc = getRuntimeCC();
5163 if (cc != llvm::CallingConv::C)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005164 FI.setEffectiveCallingConvention(cc);
John McCallbd7370a2013-02-28 19:01:20 +00005165}
Rafael Espindola25117ab2010-06-16 16:13:39 +00005166
John McCallbd7370a2013-02-28 19:01:20 +00005167/// Return the default calling convention that LLVM will use.
5168llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
5169 // The default calling convention that LLVM will infer.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005170 if (isEABIHF() || getTarget().getTriple().isWatchABI())
John McCallbd7370a2013-02-28 19:01:20 +00005171 return llvm::CallingConv::ARM_AAPCS_VFP;
5172 else if (isEABI())
5173 return llvm::CallingConv::ARM_AAPCS;
5174 else
5175 return llvm::CallingConv::ARM_APCS;
5176}
5177
5178/// Return the calling convention that our ABI would like us to use
5179/// as the C calling convention.
5180llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00005181 switch (getABIKind()) {
John McCallbd7370a2013-02-28 19:01:20 +00005182 case APCS: return llvm::CallingConv::ARM_APCS;
5183 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
5184 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005185 case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00005186 }
John McCallbd7370a2013-02-28 19:01:20 +00005187 llvm_unreachable("bad ABI kind");
5188}
5189
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005190void ARMABIInfo::setCCs() {
John McCallbd7370a2013-02-28 19:01:20 +00005191 assert(getRuntimeCC() == llvm::CallingConv::C);
5192
5193 // Don't muddy up the IR with a ton of explicit annotations if
5194 // they'd just match what LLVM will infer from the triple.
5195 llvm::CallingConv::ID abiCC = getABIDefaultCC();
5196 if (abiCC != getLLVMDefaultCC())
5197 RuntimeCC = abiCC;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005198
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005199 // AAPCS apparently requires runtime support functions to be soft-float, but
5200 // that's almost certainly for historic reasons (Thumb1 not supporting VFP
5201 // most likely). It's more convenient for AAPCS16_VFP to be hard-float.
5202 switch (getABIKind()) {
5203 case APCS:
5204 case AAPCS16_VFP:
5205 if (abiCC != getLLVMDefaultCC())
5206 BuiltinCC = abiCC;
5207 break;
5208 case AAPCS:
5209 case AAPCS_VFP:
5210 BuiltinCC = llvm::CallingConv::ARM_AAPCS;
5211 break;
5212 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005213}
5214
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005215ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
5216 bool isVariadic) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00005217 // 6.1.2.1 The following argument types are VFP CPRCs:
5218 // A single-precision floating-point type (including promoted
5219 // half-precision types); A double-precision floating-point type;
5220 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
5221 // with a Base Type of a single- or double-precision floating-point type,
5222 // 64-bit containerized vectors or 128-bit containerized vectors with one
5223 // to four Elements.
Stephen Hines176edba2014-12-01 14:53:08 -08005224 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
5225
5226 Ty = useFirstFieldIfTransparentUnion(Ty);
Manman Renb3fa55f2012-10-30 23:21:41 +00005227
Manman Ren97f81572012-10-16 19:18:39 +00005228 // Handle illegal vector types here.
5229 if (isIllegalVectorType(Ty)) {
5230 uint64_t Size = getContext().getTypeSize(Ty);
5231 if (Size <= 32) {
5232 llvm::Type *ResType =
5233 llvm::Type::getInt32Ty(getVMContext());
5234 return ABIArgInfo::getDirect(ResType);
5235 }
5236 if (Size == 64) {
5237 llvm::Type *ResType = llvm::VectorType::get(
5238 llvm::Type::getInt32Ty(getVMContext()), 2);
5239 return ABIArgInfo::getDirect(ResType);
5240 }
5241 if (Size == 128) {
5242 llvm::Type *ResType = llvm::VectorType::get(
5243 llvm::Type::getInt32Ty(getVMContext()), 4);
5244 return ABIArgInfo::getDirect(ResType);
5245 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005246 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5247 }
5248
5249 // __fp16 gets passed as if it were an int or float, but with the top 16 bits
5250 // unspecified. This is not done for OpenCL as it handles the half type
5251 // natively, and does not need to interwork with AAPCS code.
Pirama Arumuga Nainar1567b302016-03-18 16:58:36 +00005252 if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005253 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5254 llvm::Type::getFloatTy(getVMContext()) :
5255 llvm::Type::getInt32Ty(getVMContext());
5256 return ABIArgInfo::getDirect(ResType);
Manman Ren97f81572012-10-16 19:18:39 +00005257 }
5258
John McCalld608cdb2010-08-22 10:59:02 +00005259 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005260 // Treat an enum type as its underlying type.
Stephen Hines651f13c2014-04-23 16:59:28 -07005261 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005262 Ty = EnumTy->getDecl()->getIntegerType();
Stephen Hines651f13c2014-04-23 16:59:28 -07005263 }
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005264
Stephen Hines176edba2014-12-01 14:53:08 -08005265 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5266 : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005267 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00005268
Stephen Hines651f13c2014-04-23 16:59:28 -07005269 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005270 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Stephen Hines651f13c2014-04-23 16:59:28 -07005271 }
Tim Northoverf5c3a252013-06-21 22:49:34 +00005272
Daniel Dunbar42025572009-09-14 21:54:03 +00005273 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00005274 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00005275 return ABIArgInfo::getIgnore();
5276
Stephen Hines176edba2014-12-01 14:53:08 -08005277 if (IsEffectivelyAAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00005278 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
5279 // into VFP registers.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005280 const Type *Base = nullptr;
Manman Renb3fa55f2012-10-30 23:21:41 +00005281 uint64_t Members = 0;
Stephen Hines176edba2014-12-01 14:53:08 -08005282 if (isHomogeneousAggregate(Ty, Base, Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00005283 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00005284 // Base can be a floating-point or a vector.
Stephen Hines176edba2014-12-01 14:53:08 -08005285 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00005286 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005287 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5288 // WatchOS does have homogeneous aggregates. Note that we intentionally use
5289 // this convention even for a variadic function: the backend will use GPRs
5290 // if needed.
5291 const Type *Base = nullptr;
5292 uint64_t Members = 0;
5293 if (isHomogeneousAggregate(Ty, Base, Members)) {
5294 assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
5295 llvm::Type *Ty =
5296 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
5297 return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
5298 }
5299 }
5300
5301 if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5302 getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
5303 // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
5304 // bigger than 128-bits, they get placed in space allocated by the caller,
5305 // and a pointer is passed.
5306 return ABIArgInfo::getIndirect(
5307 CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
Bob Wilson194f06a2011-08-03 05:58:22 +00005308 }
5309
Manman Ren634b3d22012-08-13 21:23:55 +00005310 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00005311 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
5312 // most 8-byte. We realign the indirect argument if type alignment is bigger
5313 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00005314 uint64_t ABIAlign = 4;
5315 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
5316 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07005317 getABIKind() == ARMABIInfo::AAPCS)
Manman Renfd1ba912012-11-05 22:42:46 +00005318 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07005319
Manman Ren885ad692012-11-06 04:58:01 +00005320 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005321 assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
5322 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5323 /*ByVal=*/true,
5324 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00005325 }
5326
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005327 // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
5328 // same size and alignment.
5329 if (getTarget().isRenderScriptTarget()) {
Matt Wala1d151512015-08-10 15:58:40 -07005330 return coerceToIntArray(Ty, getContext(), getVMContext());
5331 }
5332
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00005333 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00005334 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005335 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00005336 // FIXME: Try to match the types of the arguments more accurately where
5337 // we can.
5338 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00005339 ElemTy = llvm::Type::getInt32Ty(getVMContext());
5340 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00005341 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00005342 ElemTy = llvm::Type::getInt64Ty(getVMContext());
5343 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00005344 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00005345
Stephen Hines176edba2014-12-01 14:53:08 -08005346 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005347}
5348
Chris Lattnera3c109b2010-07-29 02:16:43 +00005349static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00005350 llvm::LLVMContext &VMContext) {
5351 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
5352 // is called integer-like if its size is less than or equal to one word, and
5353 // the offset of each of its addressable sub-fields is zero.
5354
5355 uint64_t Size = Context.getTypeSize(Ty);
5356
5357 // Check that the type fits in a word.
5358 if (Size > 32)
5359 return false;
5360
5361 // FIXME: Handle vector types!
5362 if (Ty->isVectorType())
5363 return false;
5364
Daniel Dunbarb0d58192009-09-14 02:20:34 +00005365 // Float types are never treated as "integer like".
5366 if (Ty->isRealFloatingType())
5367 return false;
5368
Daniel Dunbar98303b92009-09-13 08:03:58 +00005369 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00005370 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00005371 return true;
5372
Daniel Dunbar45815812010-02-01 23:31:26 +00005373 // Small complex integer types are "integer like".
5374 if (const ComplexType *CT = Ty->getAs<ComplexType>())
5375 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00005376
5377 // Single element and zero sized arrays should be allowed, by the definition
5378 // above, but they are not.
5379
5380 // Otherwise, it must be a record type.
5381 const RecordType *RT = Ty->getAs<RecordType>();
5382 if (!RT) return false;
5383
5384 // Ignore records with flexible arrays.
5385 const RecordDecl *RD = RT->getDecl();
5386 if (RD->hasFlexibleArrayMember())
5387 return false;
5388
5389 // Check that all sub-fields are at offset 0, and are themselves "integer
5390 // like".
5391 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
5392
5393 bool HadField = false;
5394 unsigned idx = 0;
5395 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
5396 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00005397 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00005398
Daniel Dunbar679855a2010-01-29 03:22:29 +00005399 // Bit-fields are not addressable, we only need to verify they are "integer
5400 // like". We still have to disallow a subsequent non-bitfield, for example:
5401 // struct { int : 0; int x }
5402 // is non-integer like according to gcc.
5403 if (FD->isBitField()) {
5404 if (!RD->isUnion())
5405 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00005406
Daniel Dunbar679855a2010-01-29 03:22:29 +00005407 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5408 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00005409
Daniel Dunbar679855a2010-01-29 03:22:29 +00005410 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00005411 }
5412
Daniel Dunbar679855a2010-01-29 03:22:29 +00005413 // Check if this field is at offset 0.
5414 if (Layout.getFieldOffset(idx) != 0)
5415 return false;
5416
Daniel Dunbar98303b92009-09-13 08:03:58 +00005417 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5418 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00005419
Daniel Dunbar679855a2010-01-29 03:22:29 +00005420 // Only allow at most one field in a structure. This doesn't match the
5421 // wording above, but follows gcc in situations with a field following an
5422 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00005423 if (!RD->isUnion()) {
5424 if (HadField)
5425 return false;
5426
5427 HadField = true;
5428 }
5429 }
5430
5431 return true;
5432}
5433
Stephen Hines651f13c2014-04-23 16:59:28 -07005434ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
5435 bool isVariadic) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005436 bool IsEffectivelyAAPCS_VFP =
5437 (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic;
Stephen Hines176edba2014-12-01 14:53:08 -08005438
Daniel Dunbar98303b92009-09-13 08:03:58 +00005439 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005440 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00005441
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00005442 // Large vector types should be returned via memory.
Stephen Hines651f13c2014-04-23 16:59:28 -07005443 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005444 return getNaturalAlignIndirect(RetTy);
5445 }
5446
5447 // __fp16 gets returned as if it were an int or float, but with the top 16
5448 // bits unspecified. This is not done for OpenCL as it handles the half type
5449 // natively, and does not need to interwork with AAPCS code.
Pirama Arumuga Nainar1567b302016-03-18 16:58:36 +00005450 if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005451 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5452 llvm::Type::getFloatTy(getVMContext()) :
5453 llvm::Type::getInt32Ty(getVMContext());
5454 return ABIArgInfo::getDirect(ResType);
Stephen Hines651f13c2014-04-23 16:59:28 -07005455 }
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00005456
John McCalld608cdb2010-08-22 10:59:02 +00005457 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005458 // Treat an enum type as its underlying type.
5459 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5460 RetTy = EnumTy->getDecl()->getIntegerType();
5461
Stephen Hines176edba2014-12-01 14:53:08 -08005462 return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5463 : ABIArgInfo::getDirect();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00005464 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00005465
5466 // Are we following APCS?
5467 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00005468 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00005469 return ABIArgInfo::getIgnore();
5470
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00005471 // Complex types are all returned as packed integers.
5472 //
5473 // FIXME: Consider using 2 x vector types if the back end handles them
5474 // correctly.
5475 if (RetTy->isAnyComplexType())
Stephen Hines176edba2014-12-01 14:53:08 -08005476 return ABIArgInfo::getDirect(llvm::IntegerType::get(
5477 getVMContext(), getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00005478
Daniel Dunbar98303b92009-09-13 08:03:58 +00005479 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00005480 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00005481 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00005482 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00005483 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00005484 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00005485 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00005486 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5487 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00005488 }
5489
5490 // Otherwise return in memory.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005491 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005492 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00005493
5494 // Otherwise this is an AAPCS variant.
5495
Chris Lattnera3c109b2010-07-29 02:16:43 +00005496 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00005497 return ABIArgInfo::getIgnore();
5498
Bob Wilson3b694fa2011-11-02 04:51:36 +00005499 // Check for homogeneous aggregates with AAPCS-VFP.
Stephen Hines176edba2014-12-01 14:53:08 -08005500 if (IsEffectivelyAAPCS_VFP) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005501 const Type *Base = nullptr;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005502 uint64_t Members = 0;
Stephen Hines176edba2014-12-01 14:53:08 -08005503 if (isHomogeneousAggregate(RetTy, Base, Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00005504 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00005505 // Homogeneous Aggregates are returned directly.
Stephen Hines176edba2014-12-01 14:53:08 -08005506 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00005507 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00005508 }
5509
Daniel Dunbar98303b92009-09-13 08:03:58 +00005510 // Aggregates <= 4 bytes are returned in r0; other aggregates
5511 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00005512 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00005513 if (Size <= 32) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005514 // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
5515 // same size and alignment.
5516 if (getTarget().isRenderScriptTarget()) {
Matt Wala1d151512015-08-10 15:58:40 -07005517 return coerceToIntArray(RetTy, getContext(), getVMContext());
5518 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07005519 if (getDataLayout().isBigEndian())
5520 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
5521 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5522
Daniel Dunbar16a08082009-09-14 00:56:55 +00005523 // Return in the smallest viable integer type.
5524 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00005525 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00005526 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00005527 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5528 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005529 } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
5530 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
5531 llvm::Type *CoerceTy =
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005532 llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005533 return ABIArgInfo::getDirect(CoerceTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00005534 }
5535
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005536 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005537}
5538
Manman Ren97f81572012-10-16 19:18:39 +00005539/// isIllegalVector - check whether Ty is an illegal vector type.
5540bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005541 if (const VectorType *VT = Ty->getAs<VectorType> ()) {
5542 if (isAndroid()) {
5543 // Android shipped using Clang 3.1, which supported a slightly different
5544 // vector ABI. The primary differences were that 3-element vector types
5545 // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
5546 // accepts that legacy behavior for Android only.
5547 // Check whether VT is legal.
5548 unsigned NumElements = VT->getNumElements();
5549 // NumElements should be power of 2 or equal to 3.
5550 if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
5551 return true;
5552 } else {
5553 // Check whether VT is legal.
5554 unsigned NumElements = VT->getNumElements();
5555 uint64_t Size = getContext().getTypeSize(VT);
5556 // NumElements should be power of 2.
5557 if (!llvm::isPowerOf2_32(NumElements))
5558 return true;
5559 // Size should be greater than 32 bits.
5560 return Size <= 32;
5561 }
Manman Ren97f81572012-10-16 19:18:39 +00005562 }
5563 return false;
5564}
5565
Stephen Hines176edba2014-12-01 14:53:08 -08005566bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5567 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
5568 // double, or 64-bit or 128-bit vectors.
5569 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5570 if (BT->getKind() == BuiltinType::Float ||
5571 BT->getKind() == BuiltinType::Double ||
5572 BT->getKind() == BuiltinType::LongDouble)
5573 return true;
5574 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5575 unsigned VecSize = getContext().getTypeSize(VT);
5576 if (VecSize == 64 || VecSize == 128)
5577 return true;
5578 }
5579 return false;
5580}
5581
5582bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5583 uint64_t Members) const {
5584 return Members <= 4;
5585}
5586
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005587Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5588 QualType Ty) const {
5589 CharUnits SlotSize = CharUnits::fromQuantity(4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005590
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005591 // Empty records are ignored for parameter passing purposes.
Tim Northover373ac0a2013-06-21 23:05:33 +00005592 if (isEmptyRecord(getContext(), Ty, true)) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005593 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
5594 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
5595 return Addr;
Tim Northover373ac0a2013-06-21 23:05:33 +00005596 }
5597
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005598 auto TyInfo = getContext().getTypeInfoInChars(Ty);
5599 CharUnits TyAlignForABI = TyInfo.second;
Manman Rend105e732012-10-16 19:01:37 +00005600
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005601 // Use indirect if size of the illegal vector is bigger than 16 bytes.
5602 bool IsIndirect = false;
5603 const Type *Base = nullptr;
5604 uint64_t Members = 0;
5605 if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
5606 IsIndirect = true;
5607
5608 // ARMv7k passes structs bigger than 16 bytes indirectly, in space
5609 // allocated by the caller.
5610 } else if (TyInfo.first > CharUnits::fromQuantity(16) &&
5611 getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5612 !isHomogeneousAggregate(Ty, Base, Members)) {
5613 IsIndirect = true;
5614
5615 // Otherwise, bound the type's ABI alignment.
Manman Rend105e732012-10-16 19:01:37 +00005616 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
5617 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005618 // Our callers should be prepared to handle an under-aligned address.
5619 } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
5620 getABIKind() == ARMABIInfo::AAPCS) {
5621 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
5622 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
5623 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5624 // ARMv7k allows type alignment up to 16 bytes.
5625 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
5626 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
5627 } else {
5628 TyAlignForABI = CharUnits::fromQuantity(4);
Manman Ren97f81572012-10-16 19:18:39 +00005629 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005630 TyInfo.second = TyAlignForABI;
Manman Rend105e732012-10-16 19:01:37 +00005631
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005632 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
5633 SlotSize, /*AllowHigherAlign*/ true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00005634}
5635
Chris Lattnerdce5ad02010-06-28 20:05:43 +00005636//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00005637// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005638//===----------------------------------------------------------------------===//
5639
5640namespace {
5641
Justin Holewinski2c585b92012-05-24 17:43:12 +00005642class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005643public:
Justin Holewinskidca8f332013-03-30 14:38:24 +00005644 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005645
5646 ABIArgInfo classifyReturnType(QualType RetTy) const;
5647 ABIArgInfo classifyArgumentType(QualType Ty) const;
5648
Stephen Hines651f13c2014-04-23 16:59:28 -07005649 void computeInfo(CGFunctionInfo &FI) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005650 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5651 QualType Ty) const override;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005652};
5653
Justin Holewinski2c585b92012-05-24 17:43:12 +00005654class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005655public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00005656 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
5657 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Stephen Hines651f13c2014-04-23 16:59:28 -07005658
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005659 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -07005660 CodeGen::CodeGenModule &M) const override;
Justin Holewinskidca8f332013-03-30 14:38:24 +00005661private:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005662 // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
5663 // resulting MDNode to the nvvm.annotations MDNode.
5664 static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005665};
5666
Justin Holewinski2c585b92012-05-24 17:43:12 +00005667ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005668 if (RetTy->isVoidType())
5669 return ABIArgInfo::getIgnore();
Bill Wendling846ff9f2013-11-21 23:31:45 +00005670
5671 // note: this is different from default ABI
5672 if (!RetTy->isScalarType())
5673 return ABIArgInfo::getDirect();
5674
5675 // Treat an enum type as its underlying type.
5676 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5677 RetTy = EnumTy->getDecl()->getIntegerType();
5678
5679 return (RetTy->isPromotableIntegerType() ?
5680 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005681}
5682
Justin Holewinski2c585b92012-05-24 17:43:12 +00005683ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Bill Wendling846ff9f2013-11-21 23:31:45 +00005684 // Treat an enum type as its underlying type.
5685 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5686 Ty = EnumTy->getDecl()->getIntegerType();
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005687
Stephen Hines176edba2014-12-01 14:53:08 -08005688 // Return aggregates type as indirect by value
5689 if (isAggregateTypeForABI(Ty))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005690 return getNaturalAlignIndirect(Ty, /* byval */ true);
Stephen Hines176edba2014-12-01 14:53:08 -08005691
Bill Wendling846ff9f2013-11-21 23:31:45 +00005692 return (Ty->isPromotableIntegerType() ?
5693 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005694}
5695
Justin Holewinski2c585b92012-05-24 17:43:12 +00005696void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005697 if (!getCXXABI().classifyReturnType(FI))
5698 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines651f13c2014-04-23 16:59:28 -07005699 for (auto &I : FI.arguments())
5700 I.info = classifyArgumentType(I.type);
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005701
5702 // Always honor user-specified calling convention.
5703 if (FI.getCallingConvention() != llvm::CallingConv::C)
5704 return;
5705
John McCallbd7370a2013-02-28 19:01:20 +00005706 FI.setEffectiveCallingConvention(getRuntimeCC());
5707}
5708
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005709Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5710 QualType Ty) const {
Justin Holewinski2c585b92012-05-24 17:43:12 +00005711 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005712}
5713
Justin Holewinski2c585b92012-05-24 17:43:12 +00005714void NVPTXTargetCodeGenInfo::
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005715setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Justin Holewinski2c585b92012-05-24 17:43:12 +00005716 CodeGen::CodeGenModule &M) const{
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005717 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Justin Holewinski818eafb2011-10-05 17:58:44 +00005718 if (!FD) return;
5719
5720 llvm::Function *F = cast<llvm::Function>(GV);
5721
5722 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00005723 if (M.getLangOpts().OpenCL) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00005724 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00005725 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00005726 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00005727 // OpenCL __kernel functions get kernel metadata
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005728 // Create !{<func-ref>, metadata !"kernel", i32 1} node
5729 addNVVMMetadata(F, "kernel", 1);
Justin Holewinski818eafb2011-10-05 17:58:44 +00005730 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00005731 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00005732 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00005733 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00005734
Peter Collingbourne744d90b2011-10-06 16:49:54 +00005735 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00005736 if (M.getLangOpts().CUDA) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00005737 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne744d90b2011-10-06 16:49:54 +00005738 // __global__ functions cannot be called from the device, we do not
5739 // need to set the noinline attribute.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005740 if (FD->hasAttr<CUDAGlobalAttr>()) {
5741 // Create !{<func-ref>, metadata !"kernel", i32 1} node
5742 addNVVMMetadata(F, "kernel", 1);
5743 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005744 if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005745 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005746 llvm::APSInt MaxThreads(32);
5747 MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
5748 if (MaxThreads > 0)
5749 addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
5750
5751 // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
5752 // not specified in __launch_bounds__ or if the user specified a 0 value,
5753 // we don't have to add a PTX directive.
5754 if (Attr->getMinBlocks()) {
5755 llvm::APSInt MinBlocks(32);
5756 MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
5757 if (MinBlocks > 0)
5758 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
5759 addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005760 }
5761 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00005762 }
5763}
5764
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005765void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
5766 int Operand) {
Justin Holewinskidca8f332013-03-30 14:38:24 +00005767 llvm::Module *M = F->getParent();
5768 llvm::LLVMContext &Ctx = M->getContext();
5769
5770 // Get "nvvm.annotations" metadata node
5771 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
5772
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005773 llvm::Metadata *MDVals[] = {
5774 llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
5775 llvm::ConstantAsMetadata::get(
5776 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
Justin Holewinskidca8f332013-03-30 14:38:24 +00005777 // Append metadata to nvvm.annotations
5778 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
5779}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00005780}
5781
5782//===----------------------------------------------------------------------===//
Ulrich Weigandb8409212013-05-06 16:26:41 +00005783// SystemZ ABI Implementation
5784//===----------------------------------------------------------------------===//
5785
5786namespace {
5787
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005788class SystemZABIInfo : public SwiftABIInfo {
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005789 bool HasVector;
5790
Ulrich Weigandb8409212013-05-06 16:26:41 +00005791public:
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005792 SystemZABIInfo(CodeGenTypes &CGT, bool HV)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005793 : SwiftABIInfo(CGT), HasVector(HV) {}
Ulrich Weigandb8409212013-05-06 16:26:41 +00005794
5795 bool isPromotableIntegerType(QualType Ty) const;
5796 bool isCompoundType(QualType Ty) const;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005797 bool isVectorArgumentType(QualType Ty) const;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005798 bool isFPArgumentType(QualType Ty) const;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005799 QualType GetSingleElementType(QualType Ty) const;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005800
5801 ABIArgInfo classifyReturnType(QualType RetTy) const;
5802 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
5803
Stephen Hines651f13c2014-04-23 16:59:28 -07005804 void computeInfo(CGFunctionInfo &FI) const override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005805 if (!getCXXABI().classifyReturnType(FI))
5806 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines651f13c2014-04-23 16:59:28 -07005807 for (auto &I : FI.arguments())
5808 I.info = classifyArgumentType(I.type);
Ulrich Weigandb8409212013-05-06 16:26:41 +00005809 }
5810
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005811 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5812 QualType Ty) const override;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07005813
5814 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
5815 ArrayRef<llvm::Type*> scalars,
5816 bool asReturnValue) const override {
5817 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5818 }
Ulrich Weigandb8409212013-05-06 16:26:41 +00005819};
5820
5821class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
5822public:
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005823 SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
5824 : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
Ulrich Weigandb8409212013-05-06 16:26:41 +00005825};
5826
5827}
5828
5829bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
5830 // Treat an enum type as its underlying type.
5831 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5832 Ty = EnumTy->getDecl()->getIntegerType();
5833
5834 // Promotable integer types are required to be promoted by the ABI.
5835 if (Ty->isPromotableIntegerType())
5836 return true;
5837
5838 // 32-bit values must also be promoted.
5839 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5840 switch (BT->getKind()) {
5841 case BuiltinType::Int:
5842 case BuiltinType::UInt:
5843 return true;
5844 default:
5845 return false;
5846 }
5847 return false;
5848}
5849
5850bool SystemZABIInfo::isCompoundType(QualType Ty) const {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005851 return (Ty->isAnyComplexType() ||
5852 Ty->isVectorType() ||
5853 isAggregateTypeForABI(Ty));
Ulrich Weigandb8409212013-05-06 16:26:41 +00005854}
5855
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005856bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
5857 return (HasVector &&
5858 Ty->isVectorType() &&
5859 getContext().getTypeSize(Ty) <= 128);
5860}
5861
Ulrich Weigandb8409212013-05-06 16:26:41 +00005862bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
5863 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5864 switch (BT->getKind()) {
5865 case BuiltinType::Float:
5866 case BuiltinType::Double:
5867 return true;
5868 default:
5869 return false;
5870 }
5871
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005872 return false;
5873}
5874
5875QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
Ulrich Weigandb8409212013-05-06 16:26:41 +00005876 if (const RecordType *RT = Ty->getAsStructureType()) {
5877 const RecordDecl *RD = RT->getDecl();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005878 QualType Found;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005879
5880 // If this is a C++ record, check the bases first.
5881 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Stephen Hines651f13c2014-04-23 16:59:28 -07005882 for (const auto &I : CXXRD->bases()) {
5883 QualType Base = I.getType();
Ulrich Weigandb8409212013-05-06 16:26:41 +00005884
5885 // Empty bases don't affect things either way.
5886 if (isEmptyRecord(getContext(), Base, true))
5887 continue;
5888
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005889 if (!Found.isNull())
5890 return Ty;
5891 Found = GetSingleElementType(Base);
Ulrich Weigandb8409212013-05-06 16:26:41 +00005892 }
5893
5894 // Check the fields.
Stephen Hines651f13c2014-04-23 16:59:28 -07005895 for (const auto *FD : RD->fields()) {
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005896 // For compatibility with GCC, ignore empty bitfields in C++ mode.
Ulrich Weigandb8409212013-05-06 16:26:41 +00005897 // Unlike isSingleElementStruct(), empty structure and array fields
5898 // do count. So do anonymous bitfields that aren't zero-sized.
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005899 if (getContext().getLangOpts().CPlusPlus &&
5900 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
5901 continue;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005902
5903 // Unlike isSingleElementStruct(), arrays do not count.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005904 // Nested structures still do though.
5905 if (!Found.isNull())
5906 return Ty;
5907 Found = GetSingleElementType(FD->getType());
Ulrich Weigandb8409212013-05-06 16:26:41 +00005908 }
5909
5910 // Unlike isSingleElementStruct(), trailing padding is allowed.
5911 // An 8-byte aligned struct s { float f; } is passed as a double.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005912 if (!Found.isNull())
5913 return Found;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005914 }
5915
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005916 return Ty;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005917}
5918
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005919Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5920 QualType Ty) const {
Ulrich Weigandb8409212013-05-06 16:26:41 +00005921 // Assume that va_list type is correct; should be pointer to LLVM type:
5922 // struct {
5923 // i64 __gpr;
5924 // i64 __fpr;
5925 // i8 *__overflow_arg_area;
5926 // i8 *__reg_save_area;
5927 // };
5928
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005929 // Every non-vector argument occupies 8 bytes and is passed by preference
5930 // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are
5931 // always passed on the stack.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005932 Ty = getContext().getCanonicalType(Ty);
5933 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005934 llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005935 llvm::Type *DirectTy = ArgTy;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005936 ABIArgInfo AI = classifyArgumentType(Ty);
Ulrich Weigandb8409212013-05-06 16:26:41 +00005937 bool IsIndirect = AI.isIndirect();
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005938 bool InFPRs = false;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005939 bool IsVector = false;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005940 CharUnits UnpaddedSize;
5941 CharUnits DirectAlign;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005942 if (IsIndirect) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005943 DirectTy = llvm::PointerType::getUnqual(DirectTy);
5944 UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005945 } else {
5946 if (AI.getCoerceToType())
5947 ArgTy = AI.getCoerceToType();
5948 InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005949 IsVector = ArgTy->isVectorTy();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005950 UnpaddedSize = TyInfo.first;
5951 DirectAlign = TyInfo.second;
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -07005952 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005953 CharUnits PaddedSize = CharUnits::fromQuantity(8);
5954 if (IsVector && UnpaddedSize > PaddedSize)
5955 PaddedSize = CharUnits::fromQuantity(16);
5956 assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
Ulrich Weigandb8409212013-05-06 16:26:41 +00005957
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005958 CharUnits Padding = (PaddedSize - UnpaddedSize);
Ulrich Weigandb8409212013-05-06 16:26:41 +00005959
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005960 llvm::Type *IndexTy = CGF.Int64Ty;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005961 llvm::Value *PaddedSizeV =
5962 llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005963
5964 if (IsVector) {
5965 // Work out the address of a vector argument on the stack.
5966 // Vector arguments are always passed in the high bits of a
5967 // single (8 byte) or double (16 byte) stack slot.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005968 Address OverflowArgAreaPtr =
5969 CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16),
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005970 "overflow_arg_area_ptr");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005971 Address OverflowArgArea =
5972 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
5973 TyInfo.second);
5974 Address MemAddr =
5975 CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005976
5977 // Update overflow_arg_area_ptr pointer
5978 llvm::Value *NewOverflowArgArea =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005979 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
5980 "overflow_arg_area");
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07005981 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
5982
5983 return MemAddr;
5984 }
5985
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005986 assert(PaddedSize.getQuantity() == 8);
5987
5988 unsigned MaxRegs, RegCountField, RegSaveIndex;
5989 CharUnits RegPadding;
Ulrich Weigandb8409212013-05-06 16:26:41 +00005990 if (InFPRs) {
5991 MaxRegs = 4; // Maximum of 4 FPR arguments
5992 RegCountField = 1; // __fpr
5993 RegSaveIndex = 16; // save offset for f0
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08005994 RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
Ulrich Weigandb8409212013-05-06 16:26:41 +00005995 } else {
5996 MaxRegs = 5; // Maximum of 5 GPR arguments
5997 RegCountField = 0; // __gpr
5998 RegSaveIndex = 2; // save offset for r2
5999 RegPadding = Padding; // values are passed in the low bits of a GPR
6000 }
6001
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006002 Address RegCountPtr = CGF.Builder.CreateStructGEP(
6003 VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8),
6004 "reg_count_ptr");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006005 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006006 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
6007 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
Stephen Hines651f13c2014-04-23 16:59:28 -07006008 "fits_in_regs");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006009
6010 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
6011 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
6012 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
6013 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
6014
6015 // Emit code to load the value if it was passed in registers.
6016 CGF.EmitBlock(InRegBlock);
6017
6018 // Work out the address of an argument register.
Ulrich Weigandb8409212013-05-06 16:26:41 +00006019 llvm::Value *ScaledRegCount =
6020 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
6021 llvm::Value *RegBase =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006022 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
6023 + RegPadding.getQuantity());
Ulrich Weigandb8409212013-05-06 16:26:41 +00006024 llvm::Value *RegOffset =
6025 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006026 Address RegSaveAreaPtr =
6027 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
6028 "reg_save_area_ptr");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006029 llvm::Value *RegSaveArea =
6030 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006031 Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
6032 "raw_reg_addr"),
6033 PaddedSize);
6034 Address RegAddr =
6035 CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006036
6037 // Update the register count
6038 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
6039 llvm::Value *NewRegCount =
6040 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
6041 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
6042 CGF.EmitBranch(ContBlock);
6043
6044 // Emit code to load the value if it was passed in memory.
6045 CGF.EmitBlock(InMemBlock);
6046
6047 // Work out the address of a stack argument.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006048 Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP(
6049 VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr");
6050 Address OverflowArgArea =
6051 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6052 PaddedSize);
6053 Address RawMemAddr =
6054 CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
6055 Address MemAddr =
6056 CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006057
6058 // Update overflow_arg_area_ptr pointer
6059 llvm::Value *NewOverflowArgArea =
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006060 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6061 "overflow_arg_area");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006062 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6063 CGF.EmitBranch(ContBlock);
6064
6065 // Return the appropriate result.
6066 CGF.EmitBlock(ContBlock);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006067 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
6068 MemAddr, InMemBlock, "va_arg.addr");
Ulrich Weigandb8409212013-05-06 16:26:41 +00006069
6070 if (IsIndirect)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006071 ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
6072 TyInfo.second);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006073
6074 return ResAddr;
6075}
6076
Ulrich Weigandb8409212013-05-06 16:26:41 +00006077ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
6078 if (RetTy->isVoidType())
6079 return ABIArgInfo::getIgnore();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006080 if (isVectorArgumentType(RetTy))
6081 return ABIArgInfo::getDirect();
Ulrich Weigandb8409212013-05-06 16:26:41 +00006082 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006083 return getNaturalAlignIndirect(RetTy);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006084 return (isPromotableIntegerType(RetTy) ?
6085 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6086}
6087
6088ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
6089 // Handle the generic C++ ABI.
Mark Lacey23630722013-10-06 01:33:34 +00006090 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006091 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006092
6093 // Integers and enums are extended to full register width.
6094 if (isPromotableIntegerType(Ty))
6095 return ABIArgInfo::getExtend();
6096
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006097 // Handle vector types and vector-like structure types. Note that
6098 // as opposed to float-like structure types, we do not allow any
6099 // padding for vector-like structures, so verify the sizes match.
Ulrich Weigandb8409212013-05-06 16:26:41 +00006100 uint64_t Size = getContext().getTypeSize(Ty);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006101 QualType SingleElementTy = GetSingleElementType(Ty);
6102 if (isVectorArgumentType(SingleElementTy) &&
6103 getContext().getTypeSize(SingleElementTy) == Size)
6104 return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
6105
6106 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
Ulrich Weigandb8409212013-05-06 16:26:41 +00006107 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006108 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006109
6110 // Handle small structures.
6111 if (const RecordType *RT = Ty->getAs<RecordType>()) {
6112 // Structures with flexible arrays have variable length, so really
6113 // fail the size test above.
6114 const RecordDecl *RD = RT->getDecl();
6115 if (RD->hasFlexibleArrayMember())
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006116 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006117
6118 // The structure is passed as an unextended integer, a float, or a double.
6119 llvm::Type *PassTy;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006120 if (isFPArgumentType(SingleElementTy)) {
Ulrich Weigandb8409212013-05-06 16:26:41 +00006121 assert(Size == 32 || Size == 64);
6122 if (Size == 32)
6123 PassTy = llvm::Type::getFloatTy(getVMContext());
6124 else
6125 PassTy = llvm::Type::getDoubleTy(getVMContext());
6126 } else
6127 PassTy = llvm::IntegerType::get(getVMContext(), Size);
6128 return ABIArgInfo::getDirect(PassTy);
6129 }
6130
6131 // Non-structure compounds are passed indirectly.
6132 if (isCompoundType(Ty))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006133 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006134
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006135 return ABIArgInfo::getDirect(nullptr);
Ulrich Weigandb8409212013-05-06 16:26:41 +00006136}
6137
6138//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006139// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00006140//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006141
6142namespace {
6143
6144class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
6145public:
Chris Lattnerea044322010-07-29 02:01:43 +00006146 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
6147 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006148 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -07006149 CodeGen::CodeGenModule &M) const override;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006150};
6151
6152}
6153
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006154void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D,
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006155 llvm::GlobalValue *GV,
6156 CodeGen::CodeGenModule &M) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006157 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006158 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
6159 // Handle 'interrupt' attribute:
6160 llvm::Function *F = cast<llvm::Function>(GV);
6161
6162 // Step 1: Set ISR calling convention.
6163 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
6164
6165 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00006166 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006167
6168 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00006169 unsigned Num = attr->getNumber() / 2;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006170 llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
6171 "__isr_" + Twine(Num), F);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00006172 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00006173 }
6174}
6175
Chris Lattnerdce5ad02010-06-28 20:05:43 +00006176//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00006177// MIPS ABI Implementation. This works for both little-endian and
6178// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00006179//===----------------------------------------------------------------------===//
6180
John McCallaeeb7012010-05-27 06:19:26 +00006181namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00006182class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00006183 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00006184 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
6185 void CoerceToIntArgs(uint64_t TySize,
Craig Topper6b9240e2013-07-05 19:34:19 +00006186 SmallVectorImpl<llvm::Type *> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006187 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006188 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006189 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00006190public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00006191 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00006192 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
6193 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00006194
6195 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00006196 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Stephen Hines651f13c2014-04-23 16:59:28 -07006197 void computeInfo(CGFunctionInfo &FI) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006198 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6199 QualType Ty) const override;
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006200 bool shouldSignExtUnsignedType(QualType Ty) const override;
Akira Hatanaka619e8872011-06-02 00:09:17 +00006201};
6202
John McCallaeeb7012010-05-27 06:19:26 +00006203class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00006204 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00006205public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00006206 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
6207 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
6208 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00006209
Stephen Hines651f13c2014-04-23 16:59:28 -07006210 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallaeeb7012010-05-27 06:19:26 +00006211 return 29;
6212 }
6213
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006214 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -07006215 CodeGen::CodeGenModule &CGM) const override {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006216 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00006217 if (!FD) return;
Rafael Espindolad8e6d6d2013-03-19 14:32:23 +00006218 llvm::Function *Fn = cast<llvm::Function>(GV);
Reed Kotlerad4b8b42013-03-13 20:40:30 +00006219 if (FD->hasAttr<Mips16Attr>()) {
6220 Fn->addFnAttr("mips16");
6221 }
6222 else if (FD->hasAttr<NoMips16Attr>()) {
6223 Fn->addFnAttr("nomips16");
6224 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006225
6226 const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
6227 if (!Attr)
6228 return;
6229
6230 const char *Kind;
6231 switch (Attr->getInterrupt()) {
6232 case MipsInterruptAttr::eic: Kind = "eic"; break;
6233 case MipsInterruptAttr::sw0: Kind = "sw0"; break;
6234 case MipsInterruptAttr::sw1: Kind = "sw1"; break;
6235 case MipsInterruptAttr::hw0: Kind = "hw0"; break;
6236 case MipsInterruptAttr::hw1: Kind = "hw1"; break;
6237 case MipsInterruptAttr::hw2: Kind = "hw2"; break;
6238 case MipsInterruptAttr::hw3: Kind = "hw3"; break;
6239 case MipsInterruptAttr::hw4: Kind = "hw4"; break;
6240 case MipsInterruptAttr::hw5: Kind = "hw5"; break;
6241 }
6242
6243 Fn->addFnAttr("interrupt", Kind);
6244
Reed Kotler7dfd1822013-01-16 17:10:28 +00006245 }
Reed Kotlerad4b8b42013-03-13 20:40:30 +00006246
John McCallaeeb7012010-05-27 06:19:26 +00006247 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Stephen Hines651f13c2014-04-23 16:59:28 -07006248 llvm::Value *Address) const override;
John McCall49e34be2011-08-30 01:42:09 +00006249
Stephen Hines651f13c2014-04-23 16:59:28 -07006250 unsigned getSizeOfUnwindException() const override {
Akira Hatanakae624fa02011-09-20 18:23:28 +00006251 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00006252 }
John McCallaeeb7012010-05-27 06:19:26 +00006253};
6254}
6255
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006256void MipsABIInfo::CoerceToIntArgs(
6257 uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00006258 llvm::IntegerType *IntTy =
6259 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006260
6261 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
6262 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
6263 ArgList.push_back(IntTy);
6264
6265 // If necessary, add one more integer type to ArgList.
6266 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
6267
6268 if (R)
6269 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006270}
6271
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006272// In N32/64, an aligned double precision floating point field is passed in
6273// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006274llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00006275 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
6276
6277 if (IsO32) {
6278 CoerceToIntArgs(TySize, ArgList);
6279 return llvm::StructType::get(getVMContext(), ArgList);
6280 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006281
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00006282 if (Ty->isComplexType())
6283 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00006284
Akira Hatanakaa34e9212012-02-09 19:54:16 +00006285 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006286
Akira Hatanakac359f202012-07-03 19:24:06 +00006287 // Unions/vectors are passed in integer registers.
6288 if (!RT || !RT->isStructureOrClassType()) {
6289 CoerceToIntArgs(TySize, ArgList);
6290 return llvm::StructType::get(getVMContext(), ArgList);
6291 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006292
6293 const RecordDecl *RD = RT->getDecl();
6294 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006295 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006296
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006297 uint64_t LastOffset = 0;
6298 unsigned idx = 0;
6299 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
6300
Akira Hatanakaa34e9212012-02-09 19:54:16 +00006301 // Iterate over fields in the struct/class and check if there are any aligned
6302 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006303 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6304 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00006305 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006306 const BuiltinType *BT = Ty->getAs<BuiltinType>();
6307
6308 if (!BT || BT->getKind() != BuiltinType::Double)
6309 continue;
6310
6311 uint64_t Offset = Layout.getFieldOffset(idx);
6312 if (Offset % 64) // Ignore doubles that are not aligned.
6313 continue;
6314
6315 // Add ((Offset - LastOffset) / 64) args of type i64.
6316 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
6317 ArgList.push_back(I64);
6318
6319 // Add double type.
6320 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
6321 LastOffset = Offset + 64;
6322 }
6323
Akira Hatanakac359f202012-07-03 19:24:06 +00006324 CoerceToIntArgs(TySize - LastOffset, IntArgList);
6325 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00006326
6327 return llvm::StructType::get(getVMContext(), ArgList);
6328}
6329
Akira Hatanaka7ebd9532013-10-29 18:41:15 +00006330llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
6331 uint64_t Offset) const {
6332 if (OrigOffset + MinABIStackAlignInBytes > Offset)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006333 return nullptr;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006334
Akira Hatanaka7ebd9532013-10-29 18:41:15 +00006335 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006336}
Akira Hatanaka9659d592012-01-10 22:44:52 +00006337
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00006338ABIArgInfo
6339MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006340 Ty = useFirstFieldIfTransparentUnion(Ty);
6341
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006342 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006343 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006344 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006345
Akira Hatanakac359f202012-07-03 19:24:06 +00006346 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
6347 (uint64_t)StackAlignInBytes);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07006348 unsigned CurrOffset = llvm::alignTo(Offset, Align);
6349 Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006350
Akira Hatanakac359f202012-07-03 19:24:06 +00006351 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00006352 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00006353 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00006354 return ABIArgInfo::getIgnore();
6355
Mark Lacey23630722013-10-06 01:33:34 +00006356 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006357 Offset = OrigOffset + MinABIStackAlignInBytes;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006358 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00006359 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00006360
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006361 // If we have reached here, aggregates are passed directly by coercing to
6362 // another structure type. Padding is inserted if the offset of the
6363 // aggregate is unaligned.
Stephen Hines176edba2014-12-01 14:53:08 -08006364 ABIArgInfo ArgInfo =
6365 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
6366 getPaddingType(OrigOffset, CurrOffset));
6367 ArgInfo.setInReg(true);
6368 return ArgInfo;
Akira Hatanaka619e8872011-06-02 00:09:17 +00006369 }
6370
6371 // Treat an enum type as its underlying type.
6372 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6373 Ty = EnumTy->getDecl()->getIntegerType();
6374
Stephen Hines176edba2014-12-01 14:53:08 -08006375 // All integral types are promoted to the GPR width.
6376 if (Ty->isIntegralOrEnumerationType())
Akira Hatanakaa33fd392012-01-09 19:31:25 +00006377 return ABIArgInfo::getExtend();
6378
Akira Hatanaka7ebd9532013-10-29 18:41:15 +00006379 return ABIArgInfo::getDirect(
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006380 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00006381}
6382
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006383llvm::Type*
6384MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00006385 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00006386 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006387
Akira Hatanakada54ff32012-02-09 18:49:26 +00006388 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006389 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00006390 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
6391 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006392
Akira Hatanakada54ff32012-02-09 18:49:26 +00006393 // N32/64 returns struct/classes in floating point registers if the
6394 // following conditions are met:
6395 // 1. The size of the struct/class is no larger than 128-bit.
6396 // 2. The struct/class has one or two fields all of which are floating
6397 // point types.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006398 // 3. The offset of the first field is zero (this follows what gcc does).
Akira Hatanakada54ff32012-02-09 18:49:26 +00006399 //
6400 // Any other composite results are returned in integer registers.
6401 //
6402 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
6403 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
6404 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00006405 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006406
Akira Hatanakada54ff32012-02-09 18:49:26 +00006407 if (!BT || !BT->isFloatingPoint())
6408 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006409
David Blaikie262bc182012-04-30 02:36:29 +00006410 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00006411 }
6412
6413 if (b == e)
6414 return llvm::StructType::get(getVMContext(), RTList,
6415 RD->hasAttr<PackedAttr>());
6416
6417 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006418 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006419 }
6420
Akira Hatanakac359f202012-07-03 19:24:06 +00006421 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006422 return llvm::StructType::get(getVMContext(), RTList);
6423}
6424
Akira Hatanaka619e8872011-06-02 00:09:17 +00006425ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00006426 uint64_t Size = getContext().getTypeSize(RetTy);
6427
Stephen Hines176edba2014-12-01 14:53:08 -08006428 if (RetTy->isVoidType())
6429 return ABIArgInfo::getIgnore();
6430
6431 // O32 doesn't treat zero-sized structs differently from other structs.
6432 // However, N32/N64 ignores zero sized return values.
6433 if (!IsO32 && Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00006434 return ABIArgInfo::getIgnore();
6435
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00006436 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006437 if (Size <= 128) {
6438 if (RetTy->isAnyComplexType())
6439 return ABIArgInfo::getDirect();
6440
Stephen Hines176edba2014-12-01 14:53:08 -08006441 // O32 returns integer vectors in registers and N32/N64 returns all small
6442 // aggregates in registers.
6443 if (!IsO32 ||
6444 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
6445 ABIArgInfo ArgInfo =
6446 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
6447 ArgInfo.setInReg(true);
6448 return ArgInfo;
6449 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00006450 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00006451
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006452 return getNaturalAlignIndirect(RetTy);
Akira Hatanaka619e8872011-06-02 00:09:17 +00006453 }
6454
6455 // Treat an enum type as its underlying type.
6456 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6457 RetTy = EnumTy->getDecl()->getIntegerType();
6458
6459 return (RetTy->isPromotableIntegerType() ?
6460 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6461}
6462
6463void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00006464 ABIArgInfo &RetInfo = FI.getReturnInfo();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006465 if (!getCXXABI().classifyReturnType(FI))
6466 RetInfo = classifyReturnType(FI.getReturnType());
Akira Hatanakacc662542012-01-12 01:10:09 +00006467
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006468 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00006469 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00006470
Stephen Hines651f13c2014-04-23 16:59:28 -07006471 for (auto &I : FI.arguments())
6472 I.info = classifyArgumentType(I.type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00006473}
6474
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006475Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6476 QualType OrigTy) const {
6477 QualType Ty = OrigTy;
Stephen Hines176edba2014-12-01 14:53:08 -08006478
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006479 // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
6480 // Pointers are also promoted in the same way but this only matters for N32.
Stephen Hines176edba2014-12-01 14:53:08 -08006481 unsigned SlotSizeInBits = IsO32 ? 32 : 64;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006482 unsigned PtrWidth = getTarget().getPointerWidth(0);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006483 bool DidPromote = false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006484 if ((Ty->isIntegerType() &&
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006485 getContext().getIntWidth(Ty) < SlotSizeInBits) ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006486 (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006487 DidPromote = true;
6488 Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
6489 Ty->isSignedIntegerType());
Stephen Hines176edba2014-12-01 14:53:08 -08006490 }
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006491
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006492 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00006493
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006494 // The alignment of things in the argument area is never larger than
6495 // StackAlignInBytes.
6496 TyInfo.second =
6497 std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
6498
6499 // MinABIStackAlignInBytes is the size of argument slots on the stack.
6500 CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
6501
6502 Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6503 TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
6504
6505
6506 // If there was a promotion, "unpromote" into a temporary.
6507 // TODO: can we just use a pointer into a subset of the original slot?
6508 if (DidPromote) {
6509 Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
6510 llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
6511
6512 // Truncate down to the right width.
6513 llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
6514 : CGF.IntPtrTy);
6515 llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
6516 if (OrigTy->isPointerType())
6517 V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
6518
6519 CGF.Builder.CreateStore(V, Temp);
6520 Addr = Temp;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00006521 }
Akira Hatanakac35e69d2011-08-01 20:48:01 +00006522
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006523 return Addr;
Akira Hatanaka619e8872011-06-02 00:09:17 +00006524}
6525
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006526bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
6527 int TySize = getContext().getTypeSize(Ty);
6528
6529 // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
6530 if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
6531 return true;
6532
6533 return false;
6534}
6535
John McCallaeeb7012010-05-27 06:19:26 +00006536bool
6537MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6538 llvm::Value *Address) const {
6539 // This information comes from gcc's implementation, which seems to
6540 // as canonical as it gets.
6541
John McCallaeeb7012010-05-27 06:19:26 +00006542 // Everything on MIPS is 4 bytes. Double-precision FP registers
6543 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00006544 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00006545
6546 // 0-31 are the general purpose registers, $0 - $31.
6547 // 32-63 are the floating-point registers, $f0 - $f31.
6548 // 64 and 65 are the multiply/divide registers, $hi and $lo.
6549 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00006550 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00006551
6552 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
6553 // They are one bit wide and ignored here.
6554
6555 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
6556 // (coprocessor 1 is the FP unit)
6557 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
6558 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
6559 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00006560 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00006561 return false;
6562}
6563
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006564//===----------------------------------------------------------------------===//
6565// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006566// Currently subclassed only to implement custom OpenCL C function attribute
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006567// handling.
6568//===----------------------------------------------------------------------===//
6569
6570namespace {
6571
6572class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
6573public:
6574 TCETargetCodeGenInfo(CodeGenTypes &CGT)
6575 : DefaultTargetCodeGenInfo(CGT) {}
6576
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006577 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines651f13c2014-04-23 16:59:28 -07006578 CodeGen::CodeGenModule &M) const override;
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006579};
6580
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006581void TCETargetCodeGenInfo::setTargetAttributes(
6582 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006583 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006584 if (!FD) return;
6585
6586 llvm::Function *F = cast<llvm::Function>(GV);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006587
David Blaikie4e4d0842012-03-11 07:00:24 +00006588 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006589 if (FD->hasAttr<OpenCLKernelAttr>()) {
6590 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00006591 F->addFnAttr(llvm::Attribute::NoInline);
Stephen Hines651f13c2014-04-23 16:59:28 -07006592 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
6593 if (Attr) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006594 // Convert the reqd_work_group_size() attributes to metadata.
6595 llvm::LLVMContext &Context = F->getContext();
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006596 llvm::NamedMDNode *OpenCLMetadata =
6597 M.getModule().getOrInsertNamedMetadata(
6598 "opencl.kernel_wg_size_info");
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006599
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006600 SmallVector<llvm::Metadata *, 5> Operands;
6601 Operands.push_back(llvm::ConstantAsMetadata::get(F));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006602
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006603 Operands.push_back(
6604 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6605 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
6606 Operands.push_back(
6607 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6608 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
6609 Operands.push_back(
6610 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6611 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006612
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006613 // Add a boolean constant operand for "required" (true) or "hint"
6614 // (false) for implementing the work_group_size_hint attr later.
6615 // Currently always true as the hint is not yet implemented.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006616 Operands.push_back(
6617 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00006618 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
6619 }
6620 }
6621 }
6622}
6623
6624}
John McCallaeeb7012010-05-27 06:19:26 +00006625
Tony Linthicum96319392011-12-12 21:14:55 +00006626//===----------------------------------------------------------------------===//
6627// Hexagon ABI Implementation
6628//===----------------------------------------------------------------------===//
6629
6630namespace {
6631
6632class HexagonABIInfo : public ABIInfo {
6633
6634
6635public:
6636 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6637
6638private:
6639
6640 ABIArgInfo classifyReturnType(QualType RetTy) const;
6641 ABIArgInfo classifyArgumentType(QualType RetTy) const;
6642
Stephen Hines651f13c2014-04-23 16:59:28 -07006643 void computeInfo(CGFunctionInfo &FI) const override;
Tony Linthicum96319392011-12-12 21:14:55 +00006644
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006645 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6646 QualType Ty) const override;
Tony Linthicum96319392011-12-12 21:14:55 +00006647};
6648
6649class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
6650public:
6651 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
6652 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
6653
Stephen Hines651f13c2014-04-23 16:59:28 -07006654 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Tony Linthicum96319392011-12-12 21:14:55 +00006655 return 29;
6656 }
6657};
6658
6659}
6660
6661void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006662 if (!getCXXABI().classifyReturnType(FI))
6663 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Stephen Hines651f13c2014-04-23 16:59:28 -07006664 for (auto &I : FI.arguments())
6665 I.info = classifyArgumentType(I.type);
Tony Linthicum96319392011-12-12 21:14:55 +00006666}
6667
6668ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
6669 if (!isAggregateTypeForABI(Ty)) {
6670 // Treat an enum type as its underlying type.
6671 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6672 Ty = EnumTy->getDecl()->getIntegerType();
6673
6674 return (Ty->isPromotableIntegerType() ?
6675 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6676 }
6677
6678 // Ignore empty records.
6679 if (isEmptyRecord(getContext(), Ty, true))
6680 return ABIArgInfo::getIgnore();
6681
Mark Lacey23630722013-10-06 01:33:34 +00006682 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006683 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Tony Linthicum96319392011-12-12 21:14:55 +00006684
6685 uint64_t Size = getContext().getTypeSize(Ty);
6686 if (Size > 64)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006687 return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
Tony Linthicum96319392011-12-12 21:14:55 +00006688 // Pass in the smallest viable integer type.
6689 else if (Size > 32)
6690 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6691 else if (Size > 16)
6692 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6693 else if (Size > 8)
6694 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6695 else
6696 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6697}
6698
6699ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
6700 if (RetTy->isVoidType())
6701 return ABIArgInfo::getIgnore();
6702
6703 // Large vector types should be returned via memory.
6704 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006705 return getNaturalAlignIndirect(RetTy);
Tony Linthicum96319392011-12-12 21:14:55 +00006706
6707 if (!isAggregateTypeForABI(RetTy)) {
6708 // Treat an enum type as its underlying type.
6709 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6710 RetTy = EnumTy->getDecl()->getIntegerType();
6711
6712 return (RetTy->isPromotableIntegerType() ?
6713 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6714 }
6715
Tony Linthicum96319392011-12-12 21:14:55 +00006716 if (isEmptyRecord(getContext(), RetTy, true))
6717 return ABIArgInfo::getIgnore();
6718
6719 // Aggregates <= 8 bytes are returned in r0; other aggregates
6720 // are returned indirectly.
6721 uint64_t Size = getContext().getTypeSize(RetTy);
6722 if (Size <= 64) {
6723 // Return in the smallest viable integer type.
6724 if (Size <= 8)
6725 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6726 if (Size <= 16)
6727 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6728 if (Size <= 32)
6729 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6730 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6731 }
6732
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006733 return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
Tony Linthicum96319392011-12-12 21:14:55 +00006734}
6735
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006736Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6737 QualType Ty) const {
6738 // FIXME: Someone needs to audit that this handle alignment correctly.
6739 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6740 getContext().getTypeInfoInChars(Ty),
6741 CharUnits::fromQuantity(4),
6742 /*AllowHigherAlign*/ true);
Tony Linthicum96319392011-12-12 21:14:55 +00006743}
6744
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006745//===----------------------------------------------------------------------===//
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07006746// Lanai ABI Implementation
6747//===----------------------------------------------------------------------===//
6748
6749namespace {
6750class LanaiABIInfo : public DefaultABIInfo {
6751public:
6752 LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
6753
6754 bool shouldUseInReg(QualType Ty, CCState &State) const;
6755
6756 void computeInfo(CGFunctionInfo &FI) const override {
6757 CCState State(FI.getCallingConvention());
6758 // Lanai uses 4 registers to pass arguments unless the function has the
6759 // regparm attribute set.
6760 if (FI.getHasRegParm()) {
6761 State.FreeRegs = FI.getRegParm();
6762 } else {
6763 State.FreeRegs = 4;
6764 }
6765
6766 if (!getCXXABI().classifyReturnType(FI))
6767 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
6768 for (auto &I : FI.arguments())
6769 I.info = classifyArgumentType(I.type, State);
6770 }
6771
6772 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
6773 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
6774};
6775} // end anonymous namespace
6776
6777bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
6778 unsigned Size = getContext().getTypeSize(Ty);
6779 unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
6780
6781 if (SizeInRegs == 0)
6782 return false;
6783
6784 if (SizeInRegs > State.FreeRegs) {
6785 State.FreeRegs = 0;
6786 return false;
6787 }
6788
6789 State.FreeRegs -= SizeInRegs;
6790
6791 return true;
6792}
6793
6794ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
6795 CCState &State) const {
6796 if (!ByVal) {
6797 if (State.FreeRegs) {
6798 --State.FreeRegs; // Non-byval indirects just use one pointer.
6799 return getNaturalAlignIndirectInReg(Ty);
6800 }
6801 return getNaturalAlignIndirect(Ty, false);
6802 }
6803
6804 // Compute the byval alignment.
6805 const unsigned MinABIStackAlignInBytes = 4;
6806 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
6807 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
6808 /*Realign=*/TypeAlign >
6809 MinABIStackAlignInBytes);
6810}
6811
6812ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
6813 CCState &State) const {
6814 // Check with the C++ ABI first.
6815 const RecordType *RT = Ty->getAs<RecordType>();
6816 if (RT) {
6817 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
6818 if (RAA == CGCXXABI::RAA_Indirect) {
6819 return getIndirectResult(Ty, /*ByVal=*/false, State);
6820 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
6821 return getNaturalAlignIndirect(Ty, /*ByRef=*/true);
6822 }
6823 }
6824
6825 if (isAggregateTypeForABI(Ty)) {
6826 // Structures with flexible arrays are always indirect.
6827 if (RT && RT->getDecl()->hasFlexibleArrayMember())
6828 return getIndirectResult(Ty, /*ByVal=*/true, State);
6829
6830 // Ignore empty structs/unions.
6831 if (isEmptyRecord(getContext(), Ty, true))
6832 return ABIArgInfo::getIgnore();
6833
6834 llvm::LLVMContext &LLVMContext = getVMContext();
6835 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
6836 if (SizeInRegs <= State.FreeRegs) {
6837 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
6838 SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
6839 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
6840 State.FreeRegs -= SizeInRegs;
6841 return ABIArgInfo::getDirectInReg(Result);
6842 } else {
6843 State.FreeRegs = 0;
6844 }
6845 return getIndirectResult(Ty, true, State);
6846 }
6847
6848 // Treat an enum type as its underlying type.
6849 if (const auto *EnumTy = Ty->getAs<EnumType>())
6850 Ty = EnumTy->getDecl()->getIntegerType();
6851
6852 bool InReg = shouldUseInReg(Ty, State);
6853 if (Ty->isPromotableIntegerType()) {
6854 if (InReg)
6855 return ABIArgInfo::getDirectInReg();
6856 return ABIArgInfo::getExtend();
6857 }
6858 if (InReg)
6859 return ABIArgInfo::getDirectInReg();
6860 return ABIArgInfo::getDirect();
6861}
6862
6863namespace {
6864class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
6865public:
6866 LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
6867 : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {}
6868};
6869}
6870
6871//===----------------------------------------------------------------------===//
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006872// AMDGPU ABI Implementation
6873//===----------------------------------------------------------------------===//
6874
6875namespace {
6876
6877class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
6878public:
6879 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
6880 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006881 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006882 CodeGen::CodeGenModule &M) const override;
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07006883 unsigned getOpenCLKernelCallingConv() const override;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006884};
6885
6886}
6887
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07006888void AMDGPUTargetCodeGenInfo::setTargetAttributes(
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006889 const Decl *D,
6890 llvm::GlobalValue *GV,
6891 CodeGen::CodeGenModule &M) const {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006892 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07006893 if (!FD)
6894 return;
6895
6896 if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
6897 llvm::Function *F = cast<llvm::Function>(GV);
6898 uint32_t NumVGPR = Attr->getNumVGPR();
6899 if (NumVGPR != 0)
6900 F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR));
6901 }
6902
6903 if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
6904 llvm::Function *F = cast<llvm::Function>(GV);
6905 unsigned NumSGPR = Attr->getNumSGPR();
6906 if (NumSGPR != 0)
6907 F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR));
6908 }
6909}
6910
Tony Linthicum96319392011-12-12 21:14:55 +00006911
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07006912unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
6913 return llvm::CallingConv::AMDGPU_KERNEL;
6914}
6915
6916//===----------------------------------------------------------------------===//
6917// SPARC v8 ABI Implementation.
6918// Based on the SPARC Compliance Definition version 2.4.1.
6919//
6920// Ensures that complex values are passed in registers.
6921//
6922namespace {
6923class SparcV8ABIInfo : public DefaultABIInfo {
6924public:
6925 SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
6926
6927private:
6928 ABIArgInfo classifyReturnType(QualType RetTy) const;
6929 void computeInfo(CGFunctionInfo &FI) const override;
6930};
6931} // end anonymous namespace
6932
6933
6934ABIArgInfo
6935SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
6936 if (Ty->isAnyComplexType()) {
6937 return ABIArgInfo::getDirect();
6938 }
6939 else {
6940 return DefaultABIInfo::classifyReturnType(Ty);
6941 }
6942}
6943
6944void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
6945
6946 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
6947 for (auto &Arg : FI.arguments())
6948 Arg.info = classifyArgumentType(Arg.type);
6949}
6950
6951namespace {
6952class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
6953public:
6954 SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
6955 : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {}
6956};
6957} // end anonymous namespace
6958
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00006959//===----------------------------------------------------------------------===//
6960// SPARC v9 ABI Implementation.
6961// Based on the SPARC Compliance Definition version 2.4.1.
6962//
6963// Function arguments a mapped to a nominal "parameter array" and promoted to
6964// registers depending on their type. Each argument occupies 8 or 16 bytes in
6965// the array, structs larger than 16 bytes are passed indirectly.
6966//
6967// One case requires special care:
6968//
6969// struct mixed {
6970// int i;
6971// float f;
6972// };
6973//
6974// When a struct mixed is passed by value, it only occupies 8 bytes in the
6975// parameter array, but the int is passed in an integer register, and the float
6976// is passed in a floating point register. This is represented as two arguments
6977// with the LLVM IR inreg attribute:
6978//
6979// declare void f(i32 inreg %i, float inreg %f)
6980//
6981// The code generator will only allocate 4 bytes from the parameter array for
6982// the inreg arguments. All other arguments are allocated a multiple of 8
6983// bytes.
6984//
6985namespace {
6986class SparcV9ABIInfo : public ABIInfo {
6987public:
6988 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6989
6990private:
6991 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
Stephen Hines651f13c2014-04-23 16:59:28 -07006992 void computeInfo(CGFunctionInfo &FI) const override;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08006993 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6994 QualType Ty) const override;
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00006995
6996 // Coercion type builder for structs passed in registers. The coercion type
6997 // serves two purposes:
6998 //
6999 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
7000 // in registers.
7001 // 2. Expose aligned floating point elements as first-level elements, so the
7002 // code generator knows to pass them in floating point registers.
7003 //
7004 // We also compute the InReg flag which indicates that the struct contains
7005 // aligned 32-bit floats.
7006 //
7007 struct CoerceBuilder {
7008 llvm::LLVMContext &Context;
7009 const llvm::DataLayout &DL;
7010 SmallVector<llvm::Type*, 8> Elems;
7011 uint64_t Size;
7012 bool InReg;
7013
7014 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
7015 : Context(c), DL(dl), Size(0), InReg(false) {}
7016
7017 // Pad Elems with integers until Size is ToSize.
7018 void pad(uint64_t ToSize) {
7019 assert(ToSize >= Size && "Cannot remove elements");
7020 if (ToSize == Size)
7021 return;
7022
7023 // Finish the current 64-bit word.
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007024 uint64_t Aligned = llvm::alignTo(Size, 64);
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00007025 if (Aligned > Size && Aligned <= ToSize) {
7026 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
7027 Size = Aligned;
7028 }
7029
7030 // Add whole 64-bit words.
7031 while (Size + 64 <= ToSize) {
7032 Elems.push_back(llvm::Type::getInt64Ty(Context));
7033 Size += 64;
7034 }
7035
7036 // Final in-word padding.
7037 if (Size < ToSize) {
7038 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
7039 Size = ToSize;
7040 }
7041 }
7042
7043 // Add a floating point element at Offset.
7044 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
7045 // Unaligned floats are treated as integers.
7046 if (Offset % Bits)
7047 return;
7048 // The InReg flag is only required if there are any floats < 64 bits.
7049 if (Bits < 64)
7050 InReg = true;
7051 pad(Offset);
7052 Elems.push_back(Ty);
7053 Size = Offset + Bits;
7054 }
7055
7056 // Add a struct type to the coercion type, starting at Offset (in bits).
7057 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
7058 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
7059 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
7060 llvm::Type *ElemTy = StrTy->getElementType(i);
7061 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
7062 switch (ElemTy->getTypeID()) {
7063 case llvm::Type::StructTyID:
7064 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
7065 break;
7066 case llvm::Type::FloatTyID:
7067 addFloat(ElemOffset, ElemTy, 32);
7068 break;
7069 case llvm::Type::DoubleTyID:
7070 addFloat(ElemOffset, ElemTy, 64);
7071 break;
7072 case llvm::Type::FP128TyID:
7073 addFloat(ElemOffset, ElemTy, 128);
7074 break;
7075 case llvm::Type::PointerTyID:
7076 if (ElemOffset % 64 == 0) {
7077 pad(ElemOffset);
7078 Elems.push_back(ElemTy);
7079 Size += 64;
7080 }
7081 break;
7082 default:
7083 break;
7084 }
7085 }
7086 }
7087
7088 // Check if Ty is a usable substitute for the coercion type.
7089 bool isUsableType(llvm::StructType *Ty) const {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07007090 return llvm::makeArrayRef(Elems) == Ty->elements();
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00007091 }
7092
7093 // Get the coercion type as a literal struct type.
7094 llvm::Type *getType() const {
7095 if (Elems.size() == 1)
7096 return Elems.front();
7097 else
7098 return llvm::StructType::get(Context, Elems);
7099 }
7100 };
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007101};
7102} // end anonymous namespace
7103
7104ABIArgInfo
7105SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
7106 if (Ty->isVoidType())
7107 return ABIArgInfo::getIgnore();
7108
7109 uint64_t Size = getContext().getTypeSize(Ty);
7110
7111 // Anything too big to fit in registers is passed with an explicit indirect
7112 // pointer / sret pointer.
7113 if (Size > SizeLimit)
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007114 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007115
7116 // Treat an enum type as its underlying type.
7117 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7118 Ty = EnumTy->getDecl()->getIntegerType();
7119
7120 // Integer types smaller than a register are extended.
7121 if (Size < 64 && Ty->isIntegerType())
7122 return ABIArgInfo::getExtend();
7123
7124 // Other non-aggregates go in registers.
7125 if (!isAggregateTypeForABI(Ty))
7126 return ABIArgInfo::getDirect();
7127
Stephen Hines651f13c2014-04-23 16:59:28 -07007128 // If a C++ object has either a non-trivial copy constructor or a non-trivial
7129 // destructor, it is passed with an explicit indirect pointer / sret pointer.
7130 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007131 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Stephen Hines651f13c2014-04-23 16:59:28 -07007132
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007133 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00007134 // Build a coercion type from the LLVM struct type.
7135 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
7136 if (!StrTy)
7137 return ABIArgInfo::getDirect();
7138
7139 CoerceBuilder CB(getVMContext(), getDataLayout());
7140 CB.addStruct(0, StrTy);
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007141 CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
Jakob Stoklund Olesenfc782fb2013-05-28 04:57:37 +00007142
7143 // Try to use the original type for coercion.
7144 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
7145
7146 if (CB.InReg)
7147 return ABIArgInfo::getDirectInReg(CoerceTy);
7148 else
7149 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007150}
7151
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007152Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7153 QualType Ty) const {
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007154 ABIArgInfo AI = classifyType(Ty, 16 * 8);
7155 llvm::Type *ArgTy = CGT.ConvertType(Ty);
7156 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
7157 AI.setCoerceToType(ArgTy);
7158
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007159 CharUnits SlotSize = CharUnits::fromQuantity(8);
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007160
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007161 CGBuilderTy &Builder = CGF.Builder;
7162 Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
7163 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
7164
7165 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
7166
7167 Address ArgAddr = Address::invalid();
7168 CharUnits Stride;
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007169 switch (AI.getKind()) {
7170 case ABIArgInfo::Expand:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007171 case ABIArgInfo::CoerceAndExpand:
Stephen Hines651f13c2014-04-23 16:59:28 -07007172 case ABIArgInfo::InAlloca:
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007173 llvm_unreachable("Unsupported ABI kind for va_arg");
7174
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007175 case ABIArgInfo::Extend: {
7176 Stride = SlotSize;
7177 CharUnits Offset = SlotSize - TypeInfo.first;
7178 ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007179 break;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007180 }
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007181
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007182 case ABIArgInfo::Direct: {
7183 auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007184 Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007185 ArgAddr = Addr;
7186 break;
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007187 }
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007188
7189 case ABIArgInfo::Indirect:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007190 Stride = SlotSize;
7191 ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
7192 ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
7193 TypeInfo.second);
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007194 break;
7195
7196 case ABIArgInfo::Ignore:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007197 return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007198 }
7199
7200 // Update VAList.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007201 llvm::Value *NextPtr =
7202 Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next");
7203 Builder.CreateStore(NextPtr, VAListAddr);
Jakob Stoklund Olesena4b56d32013-06-05 03:00:18 +00007204
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007205 return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007206}
7207
7208void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
7209 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
Stephen Hines651f13c2014-04-23 16:59:28 -07007210 for (auto &I : FI.arguments())
7211 I.info = classifyType(I.type, 16 * 8);
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007212}
7213
7214namespace {
7215class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
7216public:
7217 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
7218 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
Stephen Hines651f13c2014-04-23 16:59:28 -07007219
7220 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
7221 return 14;
7222 }
7223
7224 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7225 llvm::Value *Address) const override;
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007226};
7227} // end anonymous namespace
7228
Stephen Hines651f13c2014-04-23 16:59:28 -07007229bool
7230SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7231 llvm::Value *Address) const {
7232 // This is calculated from the LLVM and GCC tables and verified
7233 // against gcc output. AFAIK all ABIs use the same encoding.
7234
7235 CodeGen::CGBuilderTy &Builder = CGF.Builder;
7236
7237 llvm::IntegerType *i8 = CGF.Int8Ty;
7238 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
7239 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
7240
7241 // 0-31: the 8-byte general-purpose registers
7242 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
7243
7244 // 32-63: f0-31, the 4-byte floating-point registers
7245 AssignToArrayRange(Builder, Address, Four8, 32, 63);
7246
7247 // Y = 64
7248 // PSR = 65
7249 // WIM = 66
7250 // TBR = 67
7251 // PC = 68
7252 // NPC = 69
7253 // FSR = 70
7254 // CSR = 71
7255 AssignToArrayRange(Builder, Address, Eight8, 64, 71);
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07007256
Stephen Hines651f13c2014-04-23 16:59:28 -07007257 // 72-87: d0-15, the 8-byte floating-point registers
7258 AssignToArrayRange(Builder, Address, Eight8, 72, 87);
7259
7260 return false;
7261}
7262
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00007263
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007264//===----------------------------------------------------------------------===//
Stephen Hines651f13c2014-04-23 16:59:28 -07007265// XCore ABI Implementation
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007266//===----------------------------------------------------------------------===//
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007267
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007268namespace {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007269
7270/// A SmallStringEnc instance is used to build up the TypeString by passing
7271/// it by reference between functions that append to it.
7272typedef llvm::SmallString<128> SmallStringEnc;
7273
7274/// TypeStringCache caches the meta encodings of Types.
7275///
7276/// The reason for caching TypeStrings is two fold:
7277/// 1. To cache a type's encoding for later uses;
7278/// 2. As a means to break recursive member type inclusion.
7279///
7280/// A cache Entry can have a Status of:
7281/// NonRecursive: The type encoding is not recursive;
7282/// Recursive: The type encoding is recursive;
7283/// Incomplete: An incomplete TypeString;
7284/// IncompleteUsed: An incomplete TypeString that has been used in a
7285/// Recursive type encoding.
7286///
7287/// A NonRecursive entry will have all of its sub-members expanded as fully
7288/// as possible. Whilst it may contain types which are recursive, the type
7289/// itself is not recursive and thus its encoding may be safely used whenever
7290/// the type is encountered.
7291///
7292/// A Recursive entry will have all of its sub-members expanded as fully as
7293/// possible. The type itself is recursive and it may contain other types which
7294/// are recursive. The Recursive encoding must not be used during the expansion
7295/// of a recursive type's recursive branch. For simplicity the code uses
7296/// IncompleteCount to reject all usage of Recursive encodings for member types.
7297///
7298/// An Incomplete entry is always a RecordType and only encodes its
7299/// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
7300/// are placed into the cache during type expansion as a means to identify and
7301/// handle recursive inclusion of types as sub-members. If there is recursion
7302/// the entry becomes IncompleteUsed.
7303///
7304/// During the expansion of a RecordType's members:
7305///
7306/// If the cache contains a NonRecursive encoding for the member type, the
7307/// cached encoding is used;
7308///
7309/// If the cache contains a Recursive encoding for the member type, the
7310/// cached encoding is 'Swapped' out, as it may be incorrect, and...
7311///
7312/// If the member is a RecordType, an Incomplete encoding is placed into the
7313/// cache to break potential recursive inclusion of itself as a sub-member;
7314///
7315/// Once a member RecordType has been expanded, its temporary incomplete
7316/// entry is removed from the cache. If a Recursive encoding was swapped out
7317/// it is swapped back in;
7318///
7319/// If an incomplete entry is used to expand a sub-member, the incomplete
7320/// entry is marked as IncompleteUsed. The cache keeps count of how many
7321/// IncompleteUsed entries it currently contains in IncompleteUsedCount;
7322///
7323/// If a member's encoding is found to be a NonRecursive or Recursive viz:
7324/// IncompleteUsedCount==0, the member's encoding is added to the cache.
7325/// Else the member is part of a recursive type and thus the recursion has
7326/// been exited too soon for the encoding to be correct for the member.
7327///
7328class TypeStringCache {
7329 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
7330 struct Entry {
7331 std::string Str; // The encoded TypeString for the type.
7332 enum Status State; // Information about the encoding in 'Str'.
7333 std::string Swapped; // A temporary place holder for a Recursive encoding
7334 // during the expansion of RecordType's members.
7335 };
7336 std::map<const IdentifierInfo *, struct Entry> Map;
7337 unsigned IncompleteCount; // Number of Incomplete entries in the Map.
7338 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
7339public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007340 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007341 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
7342 bool removeIncomplete(const IdentifierInfo *ID);
7343 void addIfComplete(const IdentifierInfo *ID, StringRef Str,
7344 bool IsRecursive);
7345 StringRef lookupStr(const IdentifierInfo *ID);
7346};
7347
7348/// TypeString encodings for enum & union fields must be order.
7349/// FieldEncoding is a helper for this ordering process.
7350class FieldEncoding {
7351 bool HasName;
7352 std::string Enc;
7353public:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007354 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
7355 StringRef str() {return Enc.c_str();}
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007356 bool operator<(const FieldEncoding &rhs) const {
7357 if (HasName != rhs.HasName) return HasName;
7358 return Enc < rhs.Enc;
7359 }
7360};
7361
Robert Lytton276c2892013-08-19 09:46:39 +00007362class XCoreABIInfo : public DefaultABIInfo {
7363public:
7364 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007365 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7366 QualType Ty) const override;
Robert Lytton276c2892013-08-19 09:46:39 +00007367};
7368
Stephen Hines651f13c2014-04-23 16:59:28 -07007369class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007370 mutable TypeStringCache TSC;
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007371public:
Stephen Hines651f13c2014-04-23 16:59:28 -07007372 XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
Robert Lytton276c2892013-08-19 09:46:39 +00007373 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007374 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7375 CodeGen::CodeGenModule &M) const override;
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007376};
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007377
Robert Lytton645e6fd2013-10-11 10:29:34 +00007378} // End anonymous namespace.
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007379
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007380// TODO: this implementation is likely now redundant with the default
7381// EmitVAArg.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007382Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7383 QualType Ty) const {
Robert Lytton276c2892013-08-19 09:46:39 +00007384 CGBuilderTy &Builder = CGF.Builder;
Robert Lytton276c2892013-08-19 09:46:39 +00007385
Robert Lytton645e6fd2013-10-11 10:29:34 +00007386 // Get the VAList.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007387 CharUnits SlotSize = CharUnits::fromQuantity(4);
7388 Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
Robert Lytton276c2892013-08-19 09:46:39 +00007389
Robert Lytton645e6fd2013-10-11 10:29:34 +00007390 // Handle the argument.
7391 ABIArgInfo AI = classifyArgumentType(Ty);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007392 CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
Robert Lytton645e6fd2013-10-11 10:29:34 +00007393 llvm::Type *ArgTy = CGT.ConvertType(Ty);
7394 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
7395 AI.setCoerceToType(ArgTy);
Robert Lytton276c2892013-08-19 09:46:39 +00007396 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007397
7398 Address Val = Address::invalid();
7399 CharUnits ArgSize = CharUnits::Zero();
Robert Lytton276c2892013-08-19 09:46:39 +00007400 switch (AI.getKind()) {
Robert Lytton276c2892013-08-19 09:46:39 +00007401 case ABIArgInfo::Expand:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007402 case ABIArgInfo::CoerceAndExpand:
Stephen Hines651f13c2014-04-23 16:59:28 -07007403 case ABIArgInfo::InAlloca:
Robert Lytton276c2892013-08-19 09:46:39 +00007404 llvm_unreachable("Unsupported ABI kind for va_arg");
7405 case ABIArgInfo::Ignore:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007406 Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
7407 ArgSize = CharUnits::Zero();
Robert Lytton645e6fd2013-10-11 10:29:34 +00007408 break;
Robert Lytton276c2892013-08-19 09:46:39 +00007409 case ABIArgInfo::Extend:
7410 case ABIArgInfo::Direct:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007411 Val = Builder.CreateBitCast(AP, ArgPtrTy);
7412 ArgSize = CharUnits::fromQuantity(
7413 getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007414 ArgSize = ArgSize.alignTo(SlotSize);
Robert Lytton645e6fd2013-10-11 10:29:34 +00007415 break;
Robert Lytton276c2892013-08-19 09:46:39 +00007416 case ABIArgInfo::Indirect:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007417 Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
7418 Val = Address(Builder.CreateLoad(Val), TypeAlign);
7419 ArgSize = SlotSize;
Robert Lytton645e6fd2013-10-11 10:29:34 +00007420 break;
Robert Lytton276c2892013-08-19 09:46:39 +00007421 }
Robert Lytton645e6fd2013-10-11 10:29:34 +00007422
7423 // Increment the VAList.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007424 if (!ArgSize.isZero()) {
7425 llvm::Value *APN =
7426 Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize);
7427 Builder.CreateStore(APN, VAListAddr);
Robert Lytton645e6fd2013-10-11 10:29:34 +00007428 }
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007429
Robert Lytton645e6fd2013-10-11 10:29:34 +00007430 return Val;
Robert Lytton276c2892013-08-19 09:46:39 +00007431}
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007432
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007433/// During the expansion of a RecordType, an incomplete TypeString is placed
7434/// into the cache as a means to identify and break recursion.
7435/// If there is a Recursive encoding in the cache, it is swapped out and will
7436/// be reinserted by removeIncomplete().
7437/// All other types of encoding should have been used rather than arriving here.
7438void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
7439 std::string StubEnc) {
7440 if (!ID)
7441 return;
7442 Entry &E = Map[ID];
7443 assert( (E.Str.empty() || E.State == Recursive) &&
7444 "Incorrectly use of addIncomplete");
7445 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
7446 E.Swapped.swap(E.Str); // swap out the Recursive
7447 E.Str.swap(StubEnc);
7448 E.State = Incomplete;
7449 ++IncompleteCount;
7450}
7451
7452/// Once the RecordType has been expanded, the temporary incomplete TypeString
7453/// must be removed from the cache.
7454/// If a Recursive was swapped out by addIncomplete(), it will be replaced.
7455/// Returns true if the RecordType was defined recursively.
7456bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
7457 if (!ID)
7458 return false;
7459 auto I = Map.find(ID);
7460 assert(I != Map.end() && "Entry not present");
7461 Entry &E = I->second;
7462 assert( (E.State == Incomplete ||
7463 E.State == IncompleteUsed) &&
7464 "Entry must be an incomplete type");
7465 bool IsRecursive = false;
7466 if (E.State == IncompleteUsed) {
7467 // We made use of our Incomplete encoding, thus we are recursive.
7468 IsRecursive = true;
7469 --IncompleteUsedCount;
7470 }
7471 if (E.Swapped.empty())
7472 Map.erase(I);
7473 else {
7474 // Swap the Recursive back.
7475 E.Swapped.swap(E.Str);
7476 E.Swapped.clear();
7477 E.State = Recursive;
7478 }
7479 --IncompleteCount;
7480 return IsRecursive;
7481}
7482
7483/// Add the encoded TypeString to the cache only if it is NonRecursive or
7484/// Recursive (viz: all sub-members were expanded as fully as possible).
7485void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
7486 bool IsRecursive) {
7487 if (!ID || IncompleteUsedCount)
7488 return; // No key or it is is an incomplete sub-type so don't add.
7489 Entry &E = Map[ID];
7490 if (IsRecursive && !E.Str.empty()) {
7491 assert(E.State==Recursive && E.Str.size() == Str.size() &&
7492 "This is not the same Recursive entry");
7493 // The parent container was not recursive after all, so we could have used
7494 // this Recursive sub-member entry after all, but we assumed the worse when
7495 // we started viz: IncompleteCount!=0.
7496 return;
7497 }
7498 assert(E.Str.empty() && "Entry already present");
7499 E.Str = Str.str();
7500 E.State = IsRecursive? Recursive : NonRecursive;
7501}
7502
7503/// Return a cached TypeString encoding for the ID. If there isn't one, or we
7504/// are recursively expanding a type (IncompleteCount != 0) and the cached
7505/// encoding is Recursive, return an empty StringRef.
7506StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
7507 if (!ID)
7508 return StringRef(); // We have no key.
7509 auto I = Map.find(ID);
7510 if (I == Map.end())
7511 return StringRef(); // We have no encoding.
7512 Entry &E = I->second;
7513 if (E.State == Recursive && IncompleteCount)
7514 return StringRef(); // We don't use Recursive encodings for member types.
7515
7516 if (E.State == Incomplete) {
7517 // The incomplete type is being used to break out of recursion.
7518 E.State = IncompleteUsed;
7519 ++IncompleteUsedCount;
7520 }
7521 return E.Str.c_str();
7522}
7523
7524/// The XCore ABI includes a type information section that communicates symbol
7525/// type information to the linker. The linker uses this information to verify
7526/// safety/correctness of things such as array bound and pointers et al.
7527/// The ABI only requires C (and XC) language modules to emit TypeStrings.
7528/// This type information (TypeString) is emitted into meta data for all global
7529/// symbols: definitions, declarations, functions & variables.
7530///
7531/// The TypeString carries type, qualifier, name, size & value details.
7532/// Please see 'Tools Development Guide' section 2.16.2 for format details:
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07007533/// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007534/// The output is tested by test/CodeGen/xcore-stringtype.c.
7535///
7536static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
7537 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
7538
7539/// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
7540void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7541 CodeGen::CodeGenModule &CGM) const {
7542 SmallStringEnc Enc;
7543 if (getTypeString(Enc, D, CGM, TSC)) {
7544 llvm::LLVMContext &Ctx = CGM.getModule().getContext();
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007545 llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
7546 llvm::MDString::get(Ctx, Enc.str())};
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007547 llvm::NamedMDNode *MD =
7548 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
7549 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
7550 }
7551}
7552
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007553//===----------------------------------------------------------------------===//
7554// SPIR ABI Implementation
7555//===----------------------------------------------------------------------===//
7556
7557namespace {
7558class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
7559public:
7560 SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
7561 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
7562 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7563 CodeGen::CodeGenModule &M) const override;
7564 unsigned getOpenCLKernelCallingConv() const override;
7565};
7566} // End anonymous namespace.
7567
7568/// Emit SPIR specific metadata: OpenCL and SPIR version.
7569void SPIRTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7570 CodeGen::CodeGenModule &CGM) const {
7571 llvm::LLVMContext &Ctx = CGM.getModule().getContext();
7572 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx);
7573 llvm::Module &M = CGM.getModule();
7574 // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
7575 // opencl.spir.version named metadata.
7576 llvm::Metadata *SPIRVerElts[] = {
7577 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 2)),
7578 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 0))};
7579 llvm::NamedMDNode *SPIRVerMD =
7580 M.getOrInsertNamedMetadata("opencl.spir.version");
7581 SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
7582 // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
7583 // opencl.ocl.version named metadata node.
7584 llvm::Metadata *OCLVerElts[] = {
7585 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
7586 Int32Ty, CGM.getLangOpts().OpenCLVersion / 100)),
7587 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
7588 Int32Ty, (CGM.getLangOpts().OpenCLVersion % 100) / 10))};
7589 llvm::NamedMDNode *OCLVerMD =
7590 M.getOrInsertNamedMetadata("opencl.ocl.version");
7591 OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
7592}
7593
7594unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
7595 return llvm::CallingConv::SPIR_KERNEL;
7596}
7597
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007598static bool appendType(SmallStringEnc &Enc, QualType QType,
7599 const CodeGen::CodeGenModule &CGM,
7600 TypeStringCache &TSC);
7601
7602/// Helper function for appendRecordType().
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07007603/// Builds a SmallVector containing the encoded field types in declaration
7604/// order.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007605static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
7606 const RecordDecl *RD,
7607 const CodeGen::CodeGenModule &CGM,
7608 TypeStringCache &TSC) {
Stephen Hines176edba2014-12-01 14:53:08 -08007609 for (const auto *Field : RD->fields()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007610 SmallStringEnc Enc;
7611 Enc += "m(";
Stephen Hines176edba2014-12-01 14:53:08 -08007612 Enc += Field->getName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007613 Enc += "){";
Stephen Hines176edba2014-12-01 14:53:08 -08007614 if (Field->isBitField()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007615 Enc += "b(";
7616 llvm::raw_svector_ostream OS(Enc);
Stephen Hines176edba2014-12-01 14:53:08 -08007617 OS << Field->getBitWidthValue(CGM.getContext());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007618 Enc += ':';
7619 }
Stephen Hines176edba2014-12-01 14:53:08 -08007620 if (!appendType(Enc, Field->getType(), CGM, TSC))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007621 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08007622 if (Field->isBitField())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007623 Enc += ')';
7624 Enc += '}';
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07007625 FE.emplace_back(!Field->getName().empty(), Enc);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007626 }
7627 return true;
7628}
7629
7630/// Appends structure and union types to Enc and adds encoding to cache.
7631/// Recursively calls appendType (via extractFieldType) for each field.
7632/// Union types have their fields ordered according to the ABI.
7633static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
7634 const CodeGen::CodeGenModule &CGM,
7635 TypeStringCache &TSC, const IdentifierInfo *ID) {
7636 // Append the cached TypeString if we have one.
7637 StringRef TypeString = TSC.lookupStr(ID);
7638 if (!TypeString.empty()) {
7639 Enc += TypeString;
7640 return true;
7641 }
7642
7643 // Start to emit an incomplete TypeString.
7644 size_t Start = Enc.size();
7645 Enc += (RT->isUnionType()? 'u' : 's');
7646 Enc += '(';
7647 if (ID)
7648 Enc += ID->getName();
7649 Enc += "){";
7650
7651 // We collect all encoded fields and order as necessary.
7652 bool IsRecursive = false;
7653 const RecordDecl *RD = RT->getDecl()->getDefinition();
7654 if (RD && !RD->field_empty()) {
7655 // An incomplete TypeString stub is placed in the cache for this RecordType
7656 // so that recursive calls to this RecordType will use it whilst building a
7657 // complete TypeString for this RecordType.
7658 SmallVector<FieldEncoding, 16> FE;
7659 std::string StubEnc(Enc.substr(Start).str());
7660 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString.
7661 TSC.addIncomplete(ID, std::move(StubEnc));
7662 if (!extractFieldType(FE, RD, CGM, TSC)) {
7663 (void) TSC.removeIncomplete(ID);
7664 return false;
7665 }
7666 IsRecursive = TSC.removeIncomplete(ID);
7667 // The ABI requires unions to be sorted but not structures.
7668 // See FieldEncoding::operator< for sort algorithm.
7669 if (RT->isUnionType())
7670 std::sort(FE.begin(), FE.end());
7671 // We can now complete the TypeString.
7672 unsigned E = FE.size();
7673 for (unsigned I = 0; I != E; ++I) {
7674 if (I)
7675 Enc += ',';
7676 Enc += FE[I].str();
7677 }
7678 }
7679 Enc += '}';
7680 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
7681 return true;
7682}
7683
7684/// Appends enum types to Enc and adds the encoding to the cache.
7685static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
7686 TypeStringCache &TSC,
7687 const IdentifierInfo *ID) {
7688 // Append the cached TypeString if we have one.
7689 StringRef TypeString = TSC.lookupStr(ID);
7690 if (!TypeString.empty()) {
7691 Enc += TypeString;
7692 return true;
7693 }
7694
7695 size_t Start = Enc.size();
7696 Enc += "e(";
7697 if (ID)
7698 Enc += ID->getName();
7699 Enc += "){";
7700
7701 // We collect all encoded enumerations and order them alphanumerically.
7702 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
7703 SmallVector<FieldEncoding, 16> FE;
7704 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
7705 ++I) {
7706 SmallStringEnc EnumEnc;
7707 EnumEnc += "m(";
7708 EnumEnc += I->getName();
7709 EnumEnc += "){";
7710 I->getInitVal().toString(EnumEnc);
7711 EnumEnc += '}';
7712 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
7713 }
7714 std::sort(FE.begin(), FE.end());
7715 unsigned E = FE.size();
7716 for (unsigned I = 0; I != E; ++I) {
7717 if (I)
7718 Enc += ',';
7719 Enc += FE[I].str();
7720 }
7721 }
7722 Enc += '}';
7723 TSC.addIfComplete(ID, Enc.substr(Start), false);
7724 return true;
7725}
7726
7727/// Appends type's qualifier to Enc.
7728/// This is done prior to appending the type's encoding.
7729static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
7730 // Qualifiers are emitted in alphabetical order.
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007731 static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007732 int Lookup = 0;
7733 if (QT.isConstQualified())
7734 Lookup += 1<<0;
7735 if (QT.isRestrictQualified())
7736 Lookup += 1<<1;
7737 if (QT.isVolatileQualified())
7738 Lookup += 1<<2;
7739 Enc += Table[Lookup];
7740}
7741
7742/// Appends built-in types to Enc.
7743static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
7744 const char *EncType;
7745 switch (BT->getKind()) {
7746 case BuiltinType::Void:
7747 EncType = "0";
7748 break;
7749 case BuiltinType::Bool:
7750 EncType = "b";
7751 break;
7752 case BuiltinType::Char_U:
7753 EncType = "uc";
7754 break;
7755 case BuiltinType::UChar:
7756 EncType = "uc";
7757 break;
7758 case BuiltinType::SChar:
7759 EncType = "sc";
7760 break;
7761 case BuiltinType::UShort:
7762 EncType = "us";
7763 break;
7764 case BuiltinType::Short:
7765 EncType = "ss";
7766 break;
7767 case BuiltinType::UInt:
7768 EncType = "ui";
7769 break;
7770 case BuiltinType::Int:
7771 EncType = "si";
7772 break;
7773 case BuiltinType::ULong:
7774 EncType = "ul";
7775 break;
7776 case BuiltinType::Long:
7777 EncType = "sl";
7778 break;
7779 case BuiltinType::ULongLong:
7780 EncType = "ull";
7781 break;
7782 case BuiltinType::LongLong:
7783 EncType = "sll";
7784 break;
7785 case BuiltinType::Float:
7786 EncType = "ft";
7787 break;
7788 case BuiltinType::Double:
7789 EncType = "d";
7790 break;
7791 case BuiltinType::LongDouble:
7792 EncType = "ld";
7793 break;
7794 default:
7795 return false;
7796 }
7797 Enc += EncType;
7798 return true;
7799}
7800
7801/// Appends a pointer encoding to Enc before calling appendType for the pointee.
7802static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
7803 const CodeGen::CodeGenModule &CGM,
7804 TypeStringCache &TSC) {
7805 Enc += "p(";
7806 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
7807 return false;
7808 Enc += ')';
7809 return true;
7810}
7811
7812/// Appends array encoding to Enc before calling appendType for the element.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007813static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
7814 const ArrayType *AT,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007815 const CodeGen::CodeGenModule &CGM,
7816 TypeStringCache &TSC, StringRef NoSizeEnc) {
7817 if (AT->getSizeModifier() != ArrayType::Normal)
7818 return false;
7819 Enc += "a(";
7820 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
7821 CAT->getSize().toStringUnsigned(Enc);
7822 else
7823 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
7824 Enc += ':';
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007825 // The Qualifiers should be attached to the type rather than the array.
7826 appendQualifier(Enc, QT);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007827 if (!appendType(Enc, AT->getElementType(), CGM, TSC))
7828 return false;
7829 Enc += ')';
7830 return true;
7831}
7832
7833/// Appends a function encoding to Enc, calling appendType for the return type
7834/// and the arguments.
7835static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
7836 const CodeGen::CodeGenModule &CGM,
7837 TypeStringCache &TSC) {
7838 Enc += "f{";
7839 if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
7840 return false;
7841 Enc += "}(";
7842 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
7843 // N.B. we are only interested in the adjusted param types.
7844 auto I = FPT->param_type_begin();
7845 auto E = FPT->param_type_end();
7846 if (I != E) {
7847 do {
7848 if (!appendType(Enc, *I, CGM, TSC))
7849 return false;
7850 ++I;
7851 if (I != E)
7852 Enc += ',';
7853 } while (I != E);
7854 if (FPT->isVariadic())
7855 Enc += ",va";
7856 } else {
7857 if (FPT->isVariadic())
7858 Enc += "va";
7859 else
7860 Enc += '0';
7861 }
7862 }
7863 Enc += ')';
7864 return true;
7865}
7866
7867/// Handles the type's qualifier before dispatching a call to handle specific
7868/// type encodings.
7869static bool appendType(SmallStringEnc &Enc, QualType QType,
7870 const CodeGen::CodeGenModule &CGM,
7871 TypeStringCache &TSC) {
7872
7873 QualType QT = QType.getCanonicalType();
7874
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007875 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
7876 // The Qualifiers should be attached to the type rather than the array.
7877 // Thus we don't call appendQualifier() here.
7878 return appendArrayType(Enc, QT, AT, CGM, TSC, "");
7879
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007880 appendQualifier(Enc, QT);
7881
7882 if (const BuiltinType *BT = QT->getAs<BuiltinType>())
7883 return appendBuiltinType(Enc, BT);
7884
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007885 if (const PointerType *PT = QT->getAs<PointerType>())
7886 return appendPointerType(Enc, PT, CGM, TSC);
7887
7888 if (const EnumType *ET = QT->getAs<EnumType>())
7889 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
7890
7891 if (const RecordType *RT = QT->getAsStructureType())
7892 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7893
7894 if (const RecordType *RT = QT->getAsUnionType())
7895 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7896
7897 if (const FunctionType *FT = QT->getAs<FunctionType>())
7898 return appendFunctionType(Enc, FT, CGM, TSC);
7899
7900 return false;
7901}
7902
7903static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
7904 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
7905 if (!D)
7906 return false;
7907
7908 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
7909 if (FD->getLanguageLinkage() != CLanguageLinkage)
7910 return false;
7911 return appendType(Enc, FD->getType(), CGM, TSC);
7912 }
7913
7914 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
7915 if (VD->getLanguageLinkage() != CLanguageLinkage)
7916 return false;
7917 QualType QT = VD->getType().getCanonicalType();
7918 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
7919 // Global ArrayTypes are given a size of '*' if the size is unknown.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007920 // The Qualifiers should be attached to the type rather than the array.
7921 // Thus we don't call appendQualifier() here.
7922 return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007923 }
7924 return appendType(Enc, QT, CGM, TSC);
7925 }
7926 return false;
7927}
7928
7929
Robert Lytton5f15f4d2013-08-13 09:43:10 +00007930//===----------------------------------------------------------------------===//
7931// Driver code
7932//===----------------------------------------------------------------------===//
7933
Stephen Hines176edba2014-12-01 14:53:08 -08007934const llvm::Triple &CodeGenModule::getTriple() const {
7935 return getTarget().getTriple();
7936}
7937
7938bool CodeGenModule::supportsCOMDAT() const {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007939 return getTriple().supportsCOMDAT();
Stephen Hines176edba2014-12-01 14:53:08 -08007940}
7941
Chris Lattnerea044322010-07-29 02:01:43 +00007942const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00007943 if (TheTargetCodeGenInfo)
7944 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00007945
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007946 // Helper to set the unique_ptr while still keeping the return value.
7947 auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
7948 this->TheTargetCodeGenInfo.reset(P);
7949 return *P;
7950 };
7951
John McCall64aa4b32013-04-16 22:48:15 +00007952 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00007953 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00007954 default:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007955 return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00007956
Derek Schuff9ed63f82012-09-06 17:37:28 +00007957 case llvm::Triple::le32:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007958 return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00007959 case llvm::Triple::mips:
7960 case llvm::Triple::mipsel:
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007961 if (Triple.getOS() == llvm::Triple::NaCl)
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007962 return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
7963 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00007964
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00007965 case llvm::Triple::mips64:
7966 case llvm::Triple::mips64el:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007967 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00007968
Tim Northoverc264e162013-01-31 12:13:10 +00007969 case llvm::Triple::aarch64:
Stephen Hines176edba2014-12-01 14:53:08 -08007970 case llvm::Triple::aarch64_be: {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007971 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07007972 if (getTarget().getABI() == "darwinpcs")
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007973 Kind = AArch64ABIInfo::DarwinPCS;
7974
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007975 return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007976 }
Tim Northoverc264e162013-01-31 12:13:10 +00007977
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007978 case llvm::Triple::wasm32:
7979 case llvm::Triple::wasm64:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007980 return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types));
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08007981
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00007982 case llvm::Triple::arm:
Stephen Hines651f13c2014-04-23 16:59:28 -07007983 case llvm::Triple::armeb:
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00007984 case llvm::Triple::thumb:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007985 case llvm::Triple::thumbeb: {
7986 if (Triple.getOS() == llvm::Triple::Win32) {
7987 return SetCGInfo(
7988 new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
Sandeep Patel34c1af82011-04-05 00:23:47 +00007989 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00007990
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07007991 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
7992 StringRef ABIStr = getTarget().getABI();
7993 if (ABIStr == "apcs-gnu")
7994 Kind = ARMABIInfo::APCS;
7995 else if (ABIStr == "aapcs16")
7996 Kind = ARMABIInfo::AAPCS16_VFP;
7997 else if (CodeGenOpts.FloatABI == "hard" ||
7998 (CodeGenOpts.FloatABI != "soft" &&
7999 (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
8000 Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
8001 Triple.getEnvironment() == llvm::Triple::EABIHF)))
8002 Kind = ARMABIInfo::AAPCS_VFP;
8003
8004 return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
8005 }
8006
John McCallec853ba2010-03-11 00:10:12 +00008007 case llvm::Triple::ppc:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008008 return SetCGInfo(
8009 new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft"));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00008010 case llvm::Triple::ppc64:
Stephen Hines176edba2014-12-01 14:53:08 -08008011 if (Triple.isOSBinFormatELF()) {
8012 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
8013 if (getTarget().getABI() == "elfv2")
8014 Kind = PPC64_SVR4_ABIInfo::ELFv2;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07008015 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Stephen Hines176edba2014-12-01 14:53:08 -08008016
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008017 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX));
Stephen Hines176edba2014-12-01 14:53:08 -08008018 } else
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008019 return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
Stephen Hines176edba2014-12-01 14:53:08 -08008020 case llvm::Triple::ppc64le: {
Bill Schmidtea7fb0c2013-07-26 01:36:11 +00008021 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
Stephen Hines176edba2014-12-01 14:53:08 -08008022 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07008023 if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
Stephen Hines176edba2014-12-01 14:53:08 -08008024 Kind = PPC64_SVR4_ABIInfo::ELFv1;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07008025 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Stephen Hines176edba2014-12-01 14:53:08 -08008026
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008027 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX));
Stephen Hines176edba2014-12-01 14:53:08 -08008028 }
John McCallec853ba2010-03-11 00:10:12 +00008029
Peter Collingbourneedb66f32012-05-20 23:28:41 +00008030 case llvm::Triple::nvptx:
8031 case llvm::Triple::nvptx64:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008032 return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00008033
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00008034 case llvm::Triple::msp430:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008035 return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00008036
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07008037 case llvm::Triple::systemz: {
8038 bool HasVector = getTarget().getABI() == "vector";
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008039 return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector));
Pirama Arumuga Nainarb6d69932015-07-01 12:25:36 -07008040 }
Ulrich Weigandb8409212013-05-06 16:26:41 +00008041
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00008042 case llvm::Triple::tce:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008043 return SetCGInfo(new TCETargetCodeGenInfo(Types));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00008044
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00008045 case llvm::Triple::x86: {
John McCallb8b52972013-06-18 02:46:29 +00008046 bool IsDarwinVectorABI = Triple.isOSDarwin();
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08008047 bool RetSmallStructInRegABI =
John McCallb8b52972013-06-18 02:46:29 +00008048 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008049 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00008050
John McCallb8b52972013-06-18 02:46:29 +00008051 if (Triple.getOS() == llvm::Triple::Win32) {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008052 return SetCGInfo(new WinX86_32TargetCodeGenInfo(
8053 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
8054 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
John McCallb8b52972013-06-18 02:46:29 +00008055 } else {
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008056 return SetCGInfo(new X86_32TargetCodeGenInfo(
8057 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
8058 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
8059 CodeGenOpts.FloatABI == "soft"));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00008060 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00008061 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00008062
Eli Friedmanee1ad992011-12-02 00:11:43 +00008063 case llvm::Triple::x86_64: {
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08008064 StringRef ABI = getTarget().getABI();
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008065 X86AVXABILevel AVXLevel =
8066 (ABI == "avx512"
8067 ? X86AVXABILevel::AVX512
8068 : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
Pirama Arumuga Nainar87d948e2016-03-03 15:49:35 -08008069
Chris Lattnerf13721d2010-08-31 16:44:54 +00008070 switch (Triple.getOS()) {
8071 case llvm::Triple::Win32:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008072 return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008073 case llvm::Triple::PS4:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008074 return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel));
Chris Lattnerf13721d2010-08-31 16:44:54 +00008075 default:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008076 return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
Chris Lattnerf13721d2010-08-31 16:44:54 +00008077 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00008078 }
Tony Linthicum96319392011-12-12 21:14:55 +00008079 case llvm::Triple::hexagon:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008080 return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
8081 case llvm::Triple::lanai:
8082 return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008083 case llvm::Triple::r600:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008084 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07008085 case llvm::Triple::amdgcn:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008086 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
8087 case llvm::Triple::sparc:
8088 return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
Jakob Stoklund Olesen107196c2013-05-27 21:48:25 +00008089 case llvm::Triple::sparcv9:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008090 return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
Robert Lytton5f15f4d2013-08-13 09:43:10 +00008091 case llvm::Triple::xcore:
Pirama Arumuga Nainar4967a712016-09-19 22:19:55 -07008092 return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
8093 case llvm::Triple::spir:
8094 case llvm::Triple::spir64:
8095 return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00008096 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00008097}