blob: 340fd55cc7bb26991d7d65670a6b0d80527874b4 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-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 Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
Yaxun Liuc2a87a02017-10-14 12:23:50 +000017#include "CGBlocks.h"
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +000018#include "CGCXXABI.h"
Reid Kleckner9b3e3df2014-09-04 20:04:38 +000019#include "CGValue.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000020#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000022#include "clang/CodeGen/CGFunctionInfo.h"
John McCall12f23522016-04-04 18:33:08 +000023#include "clang/CodeGen/SwiftCallingConv.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000024#include "clang/Frontend/CodeGenOptions.h"
Matt Arsenault43fae6c2014-12-04 20:38:18 +000025#include "llvm/ADT/StringExtras.h"
Coby Tayree7b49dc92017-08-24 09:07:34 +000026#include "llvm/ADT/StringSwitch.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000027#include "llvm/ADT/Triple.h"
Yaxun Liu98f0c432017-10-14 12:51:52 +000028#include "llvm/ADT/Twine.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000029#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/Type.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000031#include "llvm/Support/raw_ostream.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000032#include <algorithm> // std::sort
Robert Lytton844aeeb2014-05-02 09:33:20 +000033
Anton Korobeynikov244360d2009-06-05 22:08:42 +000034using namespace clang;
35using namespace CodeGen;
36
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +000037// Helper for coercing an aggregate argument or return value into an integer
38// array of the same size (including padding) and alignment. This alternate
39// coercion happens only for the RenderScript ABI and can be removed after
40// runtimes that rely on it are no longer supported.
41//
42// RenderScript assumes that the size of the argument / return value in the IR
43// is the same as the size of the corresponding qualified type. This helper
44// coerces the aggregate type into an array of the same size (including
45// padding). This coercion is used in lieu of expansion of struct members or
46// other canonical coercions that return a coerced-type of larger size.
47//
48// Ty - The argument / return value type
49// Context - The associated ASTContext
50// LLVMContext - The associated LLVMContext
51static ABIArgInfo coerceToIntArray(QualType Ty,
52 ASTContext &Context,
53 llvm::LLVMContext &LLVMContext) {
54 // Alignment and Size are measured in bits.
55 const uint64_t Size = Context.getTypeSize(Ty);
56 const uint64_t Alignment = Context.getTypeAlign(Ty);
57 llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
58 const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
59 return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
60}
61
John McCall943fae92010-05-27 06:19:26 +000062static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
63 llvm::Value *Array,
64 llvm::Value *Value,
65 unsigned FirstIndex,
66 unsigned LastIndex) {
67 // Alternatively, we could emit this as a loop in the source.
68 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
David Blaikiefb901c7a2015-04-04 15:12:29 +000069 llvm::Value *Cell =
70 Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
John McCall7f416cc2015-09-08 08:05:57 +000071 Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
John McCall943fae92010-05-27 06:19:26 +000072 }
73}
74
John McCalla1dee5302010-08-22 10:59:02 +000075static bool isAggregateTypeForABI(QualType T) {
John McCall47fb9502013-03-07 21:37:08 +000076 return !CodeGenFunction::hasScalarEvaluationKind(T) ||
John McCalla1dee5302010-08-22 10:59:02 +000077 T->isMemberFunctionPointerType();
78}
79
John McCall7f416cc2015-09-08 08:05:57 +000080ABIArgInfo
81ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign,
82 llvm::Type *Padding) const {
83 return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
84 ByRef, Realign, Padding);
85}
86
87ABIArgInfo
88ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
89 return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
90 /*ByRef*/ false, Realign);
91}
92
Charles Davisc7d5c942015-09-17 20:55:33 +000093Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
94 QualType Ty) const {
95 return Address::invalid();
96}
97
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000098ABIInfo::~ABIInfo() {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +000099
John McCall12f23522016-04-04 18:33:08 +0000100/// Does the given lowering require more than the given number of
101/// registers when expanded?
102///
103/// This is intended to be the basis of a reasonable basic implementation
104/// of should{Pass,Return}IndirectlyForSwift.
105///
106/// For most targets, a limit of four total registers is reasonable; this
107/// limits the amount of code required in order to move around the value
108/// in case it wasn't produced immediately prior to the call by the caller
109/// (or wasn't produced in exactly the right registers) or isn't used
110/// immediately within the callee. But some targets may need to further
111/// limit the register count due to an inability to support that many
112/// return registers.
113static bool occupiesMoreThan(CodeGenTypes &cgt,
114 ArrayRef<llvm::Type*> scalarTypes,
115 unsigned maxAllRegisters) {
116 unsigned intCount = 0, fpCount = 0;
117 for (llvm::Type *type : scalarTypes) {
118 if (type->isPointerTy()) {
119 intCount++;
120 } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
121 auto ptrWidth = cgt.getTarget().getPointerWidth(0);
122 intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
123 } else {
124 assert(type->isVectorTy() || type->isFloatingPointTy());
125 fpCount++;
126 }
127 }
128
129 return (intCount + fpCount > maxAllRegisters);
130}
131
132bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
133 llvm::Type *eltTy,
134 unsigned numElts) const {
135 // The default implementation of this assumes that the target guarantees
136 // 128-bit SIMD support but nothing more.
137 return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16);
138}
139
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000140static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
Mark Lacey3825e832013-10-06 01:33:34 +0000141 CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000142 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
143 if (!RD)
144 return CGCXXABI::RAA_Default;
Mark Lacey3825e832013-10-06 01:33:34 +0000145 return CXXABI.getRecordArgABI(RD);
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000146}
147
148static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
Mark Lacey3825e832013-10-06 01:33:34 +0000149 CGCXXABI &CXXABI) {
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000150 const RecordType *RT = T->getAs<RecordType>();
151 if (!RT)
152 return CGCXXABI::RAA_Default;
Mark Lacey3825e832013-10-06 01:33:34 +0000153 return getRecordArgABI(RT, CXXABI);
154}
155
Reid Klecknerb1be6832014-11-15 01:41:41 +0000156/// Pass transparent unions as if they were the type of the first element. Sema
157/// should ensure that all elements of the union have the same "machine type".
158static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
159 if (const RecordType *UT = Ty->getAsUnionType()) {
160 const RecordDecl *UD = UT->getDecl();
161 if (UD->hasAttr<TransparentUnionAttr>()) {
162 assert(!UD->field_empty() && "sema created an empty transparent union");
163 return UD->field_begin()->getType();
164 }
165 }
166 return Ty;
167}
168
Mark Lacey3825e832013-10-06 01:33:34 +0000169CGCXXABI &ABIInfo::getCXXABI() const {
170 return CGT.getCXXABI();
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000171}
172
Chris Lattner2b037972010-07-29 02:01:43 +0000173ASTContext &ABIInfo::getContext() const {
174 return CGT.getContext();
175}
176
177llvm::LLVMContext &ABIInfo::getVMContext() const {
178 return CGT.getLLVMContext();
179}
180
Micah Villmowdd31ca12012-10-08 16:25:52 +0000181const llvm::DataLayout &ABIInfo::getDataLayout() const {
182 return CGT.getDataLayout();
Chris Lattner2b037972010-07-29 02:01:43 +0000183}
184
John McCallc8e01702013-04-16 22:48:15 +0000185const TargetInfo &ABIInfo::getTarget() const {
186 return CGT.getTarget();
187}
Chris Lattner2b037972010-07-29 02:01:43 +0000188
Richard Smithf667ad52017-08-26 01:04:35 +0000189const CodeGenOptions &ABIInfo::getCodeGenOpts() const {
190 return CGT.getCodeGenOpts();
191}
192
193bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); }
Nirav Dave9a8f97e2016-02-22 16:48:42 +0000194
Reid Klecknere9f6a712014-10-31 17:10:41 +0000195bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
196 return false;
197}
198
199bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
200 uint64_t Members) const {
201 return false;
202}
203
Petar Jovanovic1a3f9652015-05-26 21:07:19 +0000204bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
205 return false;
206}
207
Yaron Kerencdae9412016-01-29 19:38:18 +0000208LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000209 raw_ostream &OS = llvm::errs();
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000210 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000211 switch (TheKind) {
212 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000213 OS << "Direct Type=";
Chris Lattner2192fe52011-07-18 04:24:23 +0000214 if (llvm::Type *Ty = getCoerceToType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000215 Ty->print(OS);
216 else
217 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000218 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000219 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000220 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000221 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000222 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000223 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000224 break;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000225 case InAlloca:
226 OS << "InAlloca Offset=" << getInAllocaFieldIndex();
227 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000228 case Indirect:
John McCall7f416cc2015-09-08 08:05:57 +0000229 OS << "Indirect Align=" << getIndirectAlign().getQuantity()
Joerg Sonnenberger4921fe22011-07-15 18:23:44 +0000230 << " ByVal=" << getIndirectByVal()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000231 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000232 break;
233 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000234 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000235 break;
John McCallf26e73d2016-03-11 04:30:43 +0000236 case CoerceAndExpand:
237 OS << "CoerceAndExpand Type=";
238 getCoerceAndExpandType()->print(OS);
239 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000240 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +0000241 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000242}
243
Petar Jovanovic402257b2015-12-04 00:26:47 +0000244// Dynamically round a pointer up to a multiple of the given alignment.
245static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
246 llvm::Value *Ptr,
247 CharUnits Align) {
248 llvm::Value *PtrAsInt = Ptr;
249 // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
250 PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
251 PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
252 llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
253 PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
254 llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
255 PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
256 Ptr->getType(),
257 Ptr->getName() + ".aligned");
258 return PtrAsInt;
259}
260
John McCall7f416cc2015-09-08 08:05:57 +0000261/// Emit va_arg for a platform using the common void* representation,
262/// where arguments are simply emitted in an array of slots on the stack.
263///
264/// This version implements the core direct-value passing rules.
265///
266/// \param SlotSize - The size and alignment of a stack slot.
267/// Each argument will be allocated to a multiple of this number of
268/// slots, and all the slots will be aligned to this value.
269/// \param AllowHigherAlign - The slot alignment is not a cap;
270/// an argument type with an alignment greater than the slot size
271/// will be emitted on a higher-alignment address, potentially
272/// leaving one or more empty slots behind as padding. If this
273/// is false, the returned address might be less-aligned than
274/// DirectAlign.
275static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
276 Address VAListAddr,
277 llvm::Type *DirectTy,
278 CharUnits DirectSize,
279 CharUnits DirectAlign,
280 CharUnits SlotSize,
281 bool AllowHigherAlign) {
282 // Cast the element type to i8* if necessary. Some platforms define
283 // va_list as a struct containing an i8* instead of just an i8*.
284 if (VAListAddr.getElementType() != CGF.Int8PtrTy)
285 VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
286
287 llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
288
289 // If the CC aligns values higher than the slot size, do so if needed.
290 Address Addr = Address::invalid();
291 if (AllowHigherAlign && DirectAlign > SlotSize) {
Petar Jovanovic402257b2015-12-04 00:26:47 +0000292 Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
293 DirectAlign);
John McCall7f416cc2015-09-08 08:05:57 +0000294 } else {
Petar Jovanovic402257b2015-12-04 00:26:47 +0000295 Addr = Address(Ptr, SlotSize);
John McCall7f416cc2015-09-08 08:05:57 +0000296 }
297
298 // Advance the pointer past the argument, then store that back.
Rui Ueyama83aa9792016-01-14 21:00:27 +0000299 CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
John McCall7f416cc2015-09-08 08:05:57 +0000300 llvm::Value *NextPtr =
301 CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize,
302 "argp.next");
303 CGF.Builder.CreateStore(NextPtr, VAListAddr);
304
305 // If the argument is smaller than a slot, and this is a big-endian
306 // target, the argument will be right-adjusted in its slot.
Strahinja Petrovic515a1eb2016-06-24 12:12:41 +0000307 if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
308 !DirectTy->isStructTy()) {
John McCall7f416cc2015-09-08 08:05:57 +0000309 Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
310 }
311
312 Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
313 return Addr;
314}
315
316/// Emit va_arg for a platform using the common void* representation,
317/// where arguments are simply emitted in an array of slots on the stack.
318///
319/// \param IsIndirect - Values of this type are passed indirectly.
320/// \param ValueInfo - The size and alignment of this type, generally
321/// computed with getContext().getTypeInfoInChars(ValueTy).
322/// \param SlotSizeAndAlign - The size and alignment of a stack slot.
323/// Each argument will be allocated to a multiple of this number of
324/// slots, and all the slots will be aligned to this value.
325/// \param AllowHigherAlign - The slot alignment is not a cap;
326/// an argument type with an alignment greater than the slot size
327/// will be emitted on a higher-alignment address, potentially
328/// leaving one or more empty slots behind as padding.
329static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
330 QualType ValueTy, bool IsIndirect,
331 std::pair<CharUnits, CharUnits> ValueInfo,
332 CharUnits SlotSizeAndAlign,
333 bool AllowHigherAlign) {
334 // The size and alignment of the value that was passed directly.
335 CharUnits DirectSize, DirectAlign;
336 if (IsIndirect) {
337 DirectSize = CGF.getPointerSize();
338 DirectAlign = CGF.getPointerAlign();
339 } else {
340 DirectSize = ValueInfo.first;
341 DirectAlign = ValueInfo.second;
342 }
343
344 // Cast the address we've calculated to the right type.
345 llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
346 if (IsIndirect)
347 DirectTy = DirectTy->getPointerTo(0);
348
349 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
350 DirectSize, DirectAlign,
351 SlotSizeAndAlign,
352 AllowHigherAlign);
353
354 if (IsIndirect) {
355 Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second);
356 }
357
358 return Addr;
359
360}
361
362static Address emitMergePHI(CodeGenFunction &CGF,
363 Address Addr1, llvm::BasicBlock *Block1,
364 Address Addr2, llvm::BasicBlock *Block2,
365 const llvm::Twine &Name = "") {
366 assert(Addr1.getType() == Addr2.getType());
367 llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
368 PHI->addIncoming(Addr1.getPointer(), Block1);
369 PHI->addIncoming(Addr2.getPointer(), Block2);
370 CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
371 return Address(PHI, Align);
372}
373
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000374TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
375
John McCall3480ef22011-08-30 01:42:09 +0000376// If someone can figure out a general rule for this, that would be great.
377// It's probably just doomed to be platform-dependent, though.
378unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
379 // Verified for:
380 // x86-64 FreeBSD, Linux, Darwin
381 // x86-32 FreeBSD, Linux, Darwin
382 // PowerPC Linux, Darwin
383 // ARM Darwin (*not* EABI)
Tim Northover9bb857a2013-01-31 12:13:10 +0000384 // AArch64 Linux
John McCall3480ef22011-08-30 01:42:09 +0000385 return 32;
386}
387
John McCalla729c622012-02-17 03:33:10 +0000388bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
389 const FunctionNoProtoType *fnType) const {
John McCallcbc038a2011-09-21 08:08:30 +0000390 // The following conventions are known to require this to be false:
391 // x86_stdcall
392 // MIPS
393 // For everything else, we just prefer false unless we opt out.
394 return false;
395}
396
Reid Klecknere43f0fe2013-05-08 13:44:39 +0000397void
398TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
399 llvm::SmallString<24> &Opt) const {
400 // This assumes the user is passing a library name like "rt" instead of a
401 // filename like "librt.a/so", and that they don't care whether it's static or
402 // dynamic.
403 Opt = "-l";
404 Opt += Lib;
405}
406
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000407unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
Pekka Jaaskelainenfc2629a2017-06-01 07:18:49 +0000408 // OpenCL kernels are called via an explicit runtime API with arguments
409 // set with clSetKernelArg(), not as normal sub-functions.
410 // Return SPIR_KERNEL by default as the kernel calling convention to
411 // ensure the fingerprint is fixed such way that each OpenCL argument
412 // gets one matching argument in the produced kernel function argument
413 // list to enable feasible implementation of clSetKernelArg() with
414 // aggregates etc. In case we would use the default C calling conv here,
415 // clSetKernelArg() might break depending on the target-specific
416 // conventions; different targets might split structs passed as values
417 // to multiple function arguments etc.
418 return llvm::CallingConv::SPIR_KERNEL;
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000419}
Yaxun Liu37ceede2016-07-20 19:21:11 +0000420
Yaxun Liu402804b2016-12-15 08:09:08 +0000421llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
422 llvm::PointerType *T, QualType QT) const {
423 return llvm::ConstantPointerNull::get(T);
424}
425
Yaxun Liucbf647c2017-07-08 13:24:52 +0000426unsigned TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
427 const VarDecl *D) const {
428 assert(!CGM.getLangOpts().OpenCL &&
429 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
430 "Address space agnostic languages only");
Yaxun Liu7bce6422017-07-08 19:13:41 +0000431 return D ? D->getType().getAddressSpace()
432 : static_cast<unsigned>(LangAS::Default);
Yaxun Liucbf647c2017-07-08 13:24:52 +0000433}
434
Yaxun Liu402804b2016-12-15 08:09:08 +0000435llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000436 CodeGen::CodeGenFunction &CGF, llvm::Value *Src, unsigned SrcAddr,
437 unsigned DestAddr, llvm::Type *DestTy, bool isNonNull) const {
Yaxun Liu402804b2016-12-15 08:09:08 +0000438 // Since target may map different address spaces in AST to the same address
439 // space, an address space conversion may end up as a bitcast.
Yaxun Liucbf647c2017-07-08 13:24:52 +0000440 if (auto *C = dyn_cast<llvm::Constant>(Src))
441 return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000442 return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DestTy);
Yaxun Liu402804b2016-12-15 08:09:08 +0000443}
444
Yaxun Liucbf647c2017-07-08 13:24:52 +0000445llvm::Constant *
446TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
447 unsigned SrcAddr, unsigned DestAddr,
448 llvm::Type *DestTy) const {
449 // Since target may map different address spaces in AST to the same address
450 // space, an address space conversion may end up as a bitcast.
451 return llvm::ConstantExpr::getPointerCast(Src, DestTy);
452}
453
Yaxun Liu39195062017-08-04 18:16:31 +0000454llvm::SyncScope::ID
455TargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, llvm::LLVMContext &C) const {
456 return C.getOrInsertSyncScopeID(""); /* default sync scope */
457}
458
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000459static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000460
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000461/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000462/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000463static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
464 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000465 if (FD->isUnnamedBitfield())
466 return true;
467
468 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000469
Eli Friedman0b3f2012011-11-18 03:47:20 +0000470 // Constant arrays of empty records count as empty, strip them off.
471 // Constant arrays of zero length always count as empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000472 if (AllowArrays)
Eli Friedman0b3f2012011-11-18 03:47:20 +0000473 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
474 if (AT->getSize() == 0)
475 return true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000476 FT = AT->getElementType();
Eli Friedman0b3f2012011-11-18 03:47:20 +0000477 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000478
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000479 const RecordType *RT = FT->getAs<RecordType>();
480 if (!RT)
481 return false;
482
483 // C++ record fields are never empty, at least in the Itanium ABI.
484 //
485 // FIXME: We should use a predicate for whether this behavior is true in the
486 // current ABI.
487 if (isa<CXXRecordDecl>(RT->getDecl()))
488 return false;
489
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000490 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000491}
492
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +0000493/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000494/// fields. Note that a structure with a flexible array member is not
495/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000496static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000497 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000498 if (!RT)
Denis Zobnin380b2242016-02-11 11:26:03 +0000499 return false;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000500 const RecordDecl *RD = RT->getDecl();
501 if (RD->hasFlexibleArrayMember())
502 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000503
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000504 // If this is a C++ record, check the bases first.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000505 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +0000506 for (const auto &I : CXXRD->bases())
507 if (!isEmptyRecord(Context, I.getType(), true))
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000508 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000509
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000510 for (const auto *I : RD->fields())
511 if (!isEmptyField(Context, I, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000512 return false;
513 return true;
514}
515
516/// isSingleElementStruct - Determine if a structure is a "single
517/// element struct", i.e. it has exactly one non-empty field or
518/// exactly one field which is itself a single element
519/// struct. Structures with flexible array members are never
520/// considered single element structs.
521///
522/// \return The field declaration for the single non-empty field, if
523/// it exists.
524static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Benjamin Kramer83b1bf32015-03-02 16:09:24 +0000525 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000526 if (!RT)
Craig Topper8a13c412014-05-21 05:09:00 +0000527 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000528
529 const RecordDecl *RD = RT->getDecl();
530 if (RD->hasFlexibleArrayMember())
Craig Topper8a13c412014-05-21 05:09:00 +0000531 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000532
Craig Topper8a13c412014-05-21 05:09:00 +0000533 const Type *Found = nullptr;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000534
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000535 // If this is a C++ record, check the bases first.
536 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000537 for (const auto &I : CXXRD->bases()) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000538 // Ignore empty records.
Aaron Ballman574705e2014-03-13 15:41:46 +0000539 if (isEmptyRecord(Context, I.getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000540 continue;
541
542 // If we already found an element then this isn't a single-element struct.
543 if (Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000544 return nullptr;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000545
546 // If this is non-empty and not a single element struct, the composite
547 // cannot be a single element struct.
Aaron Ballman574705e2014-03-13 15:41:46 +0000548 Found = isSingleElementStruct(I.getType(), Context);
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000549 if (!Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000550 return nullptr;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000551 }
552 }
553
554 // Check for single element.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000555 for (const auto *FD : RD->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000556 QualType FT = FD->getType();
557
558 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000559 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000560 continue;
561
562 // If we already found an element then this isn't a single-element
563 // struct.
564 if (Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000565 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000566
567 // Treat single element arrays as the element.
568 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
569 if (AT->getSize().getZExtValue() != 1)
570 break;
571 FT = AT->getElementType();
572 }
573
John McCalla1dee5302010-08-22 10:59:02 +0000574 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000575 Found = FT.getTypePtr();
576 } else {
577 Found = isSingleElementStruct(FT, Context);
578 if (!Found)
Craig Topper8a13c412014-05-21 05:09:00 +0000579 return nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000580 }
581 }
582
Eli Friedmanee945342011-11-18 01:25:50 +0000583 // We don't consider a struct a single-element struct if it has
584 // padding beyond the element type.
585 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
Craig Topper8a13c412014-05-21 05:09:00 +0000586 return nullptr;
Eli Friedmanee945342011-11-18 01:25:50 +0000587
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000588 return Found;
589}
590
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000591namespace {
James Y Knight29b5f082016-02-24 02:59:33 +0000592Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
593 const ABIArgInfo &AI) {
594 // This default implementation defers to the llvm backend's va_arg
595 // instruction. It can handle only passing arguments directly
596 // (typically only handled in the backend for primitive types), or
597 // aggregates passed indirectly by pointer (NOTE: if the "byval"
598 // flag has ABI impact in the callee, this implementation cannot
599 // work.)
600
601 // Only a few cases are covered here at the moment -- those needed
602 // by the default abi.
603 llvm::Value *Val;
604
605 if (AI.isIndirect()) {
606 assert(!AI.getPaddingType() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000607 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000608 assert(
609 !AI.getIndirectRealign() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000610 "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000611
612 auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
613 CharUnits TyAlignForABI = TyInfo.second;
614
615 llvm::Type *BaseTy =
616 llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
617 llvm::Value *Addr =
618 CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
619 return Address(Addr, TyAlignForABI);
620 } else {
621 assert((AI.isDirect() || AI.isExtend()) &&
622 "Unexpected ArgInfo Kind in generic VAArg emitter!");
623
624 assert(!AI.getInReg() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000625 "Unexpected InReg seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000626 assert(!AI.getPaddingType() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000627 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000628 assert(!AI.getDirectOffset() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000629 "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000630 assert(!AI.getCoerceToType() &&
Richard Smith81ef0e12016-05-14 01:21:40 +0000631 "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
James Y Knight29b5f082016-02-24 02:59:33 +0000632
633 Address Temp = CGF.CreateMemTemp(Ty, "varet");
634 Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
635 CGF.Builder.CreateStore(Val, Temp);
636 return Temp;
637 }
638}
639
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000640/// DefaultABIInfo - The default implementation for ABI specific
641/// details. This implementation provides information which results in
642/// self-consistent and sensible LLVM IR generation, but does not
643/// conform to any particular ABI.
644class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000645public:
646 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000647
Chris Lattner458b2aa2010-07-29 02:16:43 +0000648 ABIArgInfo classifyReturnType(QualType RetTy) const;
649 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000650
Craig Topper4f12f102014-03-12 06:41:41 +0000651 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +0000652 if (!getCXXABI().classifyReturnType(FI))
653 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +0000654 for (auto &I : FI.arguments())
655 I.info = classifyArgumentType(I.type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000656 }
657
John McCall7f416cc2015-09-08 08:05:57 +0000658 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
James Y Knight29b5f082016-02-24 02:59:33 +0000659 QualType Ty) const override {
660 return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
661 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000662};
663
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000664class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
665public:
Chris Lattner2b037972010-07-29 02:01:43 +0000666 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
667 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000668};
669
Chris Lattner458b2aa2010-07-29 02:16:43 +0000670ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerac385062015-05-18 22:46:30 +0000671 Ty = useFirstFieldIfTransparentUnion(Ty);
672
673 if (isAggregateTypeForABI(Ty)) {
674 // Records with non-trivial destructors/copy-constructors should not be
675 // passed by value.
676 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000677 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Reid Klecknerac385062015-05-18 22:46:30 +0000678
John McCall7f416cc2015-09-08 08:05:57 +0000679 return getNaturalAlignIndirect(Ty);
Reid Klecknerac385062015-05-18 22:46:30 +0000680 }
Daniel Dunbar557893d2010-04-21 19:10:51 +0000681
Chris Lattner9723d6c2010-03-11 18:19:55 +0000682 // Treat an enum type as its underlying type.
683 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
684 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000685
Chris Lattner9723d6c2010-03-11 18:19:55 +0000686 return (Ty->isPromotableIntegerType() ?
687 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000688}
689
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000690ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
691 if (RetTy->isVoidType())
692 return ABIArgInfo::getIgnore();
693
694 if (isAggregateTypeForABI(RetTy))
John McCall7f416cc2015-09-08 08:05:57 +0000695 return getNaturalAlignIndirect(RetTy);
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000696
697 // Treat an enum type as its underlying type.
698 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
699 RetTy = EnumTy->getDecl()->getIntegerType();
700
701 return (RetTy->isPromotableIntegerType() ?
702 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
703}
704
Derek Schuff09338a22012-09-06 17:37:28 +0000705//===----------------------------------------------------------------------===//
Dan Gohmanc2853072015-09-03 22:51:53 +0000706// WebAssembly ABI Implementation
707//
708// This is a very simple ABI that relies a lot on DefaultABIInfo.
709//===----------------------------------------------------------------------===//
710
711class WebAssemblyABIInfo final : public DefaultABIInfo {
712public:
713 explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT)
714 : DefaultABIInfo(CGT) {}
715
716private:
717 ABIArgInfo classifyReturnType(QualType RetTy) const;
718 ABIArgInfo classifyArgumentType(QualType Ty) const;
719
720 // DefaultABIInfo's classifyReturnType and classifyArgumentType are
Richard Smith81ef0e12016-05-14 01:21:40 +0000721 // non-virtual, but computeInfo and EmitVAArg are virtual, so we
James Y Knight29b5f082016-02-24 02:59:33 +0000722 // overload them.
Dan Gohmanc2853072015-09-03 22:51:53 +0000723 void computeInfo(CGFunctionInfo &FI) const override {
724 if (!getCXXABI().classifyReturnType(FI))
725 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
726 for (auto &Arg : FI.arguments())
727 Arg.info = classifyArgumentType(Arg.type);
728 }
Dan Gohman1fcd10c2016-02-22 19:17:40 +0000729
730 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
731 QualType Ty) const override;
Dan Gohmanc2853072015-09-03 22:51:53 +0000732};
733
734class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
735public:
736 explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
737 : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
738};
739
740/// \brief Classify argument of given type \p Ty.
741ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
742 Ty = useFirstFieldIfTransparentUnion(Ty);
743
744 if (isAggregateTypeForABI(Ty)) {
745 // Records with non-trivial destructors/copy-constructors should not be
746 // passed by value.
Dan Gohmanc2853072015-09-03 22:51:53 +0000747 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000748 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Dan Gohmanc2853072015-09-03 22:51:53 +0000749 // Ignore empty structs/unions.
750 if (isEmptyRecord(getContext(), Ty, true))
751 return ABIArgInfo::getIgnore();
752 // Lower single-element structs to just pass a regular value. TODO: We
753 // could do reasonable-size multiple-element structs too, using getExpand(),
754 // though watch out for things like bitfields.
755 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
756 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
Dan Gohmanc2853072015-09-03 22:51:53 +0000757 }
758
759 // Otherwise just do the default thing.
760 return DefaultABIInfo::classifyArgumentType(Ty);
761}
762
763ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
764 if (isAggregateTypeForABI(RetTy)) {
765 // Records with non-trivial destructors/copy-constructors should not be
766 // returned by value.
767 if (!getRecordArgABI(RetTy, getCXXABI())) {
768 // Ignore empty structs/unions.
769 if (isEmptyRecord(getContext(), RetTy, true))
770 return ABIArgInfo::getIgnore();
771 // Lower single-element structs to just return a regular value. TODO: We
772 // could do reasonable-size multiple-element structs too, using
773 // ABIArgInfo::getDirect().
774 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
775 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
776 }
777 }
778
779 // Otherwise just do the default thing.
780 return DefaultABIInfo::classifyReturnType(RetTy);
781}
782
Dan Gohman1fcd10c2016-02-22 19:17:40 +0000783Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
784 QualType Ty) const {
785 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false,
786 getContext().getTypeInfoInChars(Ty),
787 CharUnits::fromQuantity(4),
788 /*AllowHigherAlign=*/ true);
789}
790
Dan Gohmanc2853072015-09-03 22:51:53 +0000791//===----------------------------------------------------------------------===//
Derek Schuff09338a22012-09-06 17:37:28 +0000792// le32/PNaCl bitcode ABI Implementation
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000793//
794// This is a simplified version of the x86_32 ABI. Arguments and return values
795// are always passed on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000796//===----------------------------------------------------------------------===//
797
798class PNaClABIInfo : public ABIInfo {
799 public:
800 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
801
802 ABIArgInfo classifyReturnType(QualType RetTy) const;
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000803 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Derek Schuff09338a22012-09-06 17:37:28 +0000804
Craig Topper4f12f102014-03-12 06:41:41 +0000805 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +0000806 Address EmitVAArg(CodeGenFunction &CGF,
807 Address VAListAddr, QualType Ty) const override;
Derek Schuff09338a22012-09-06 17:37:28 +0000808};
809
810class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
811 public:
812 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
813 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
814};
815
816void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +0000817 if (!getCXXABI().classifyReturnType(FI))
Derek Schuff09338a22012-09-06 17:37:28 +0000818 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
819
Reid Kleckner40ca9132014-05-13 22:05:45 +0000820 for (auto &I : FI.arguments())
821 I.info = classifyArgumentType(I.type);
822}
Derek Schuff09338a22012-09-06 17:37:28 +0000823
John McCall7f416cc2015-09-08 08:05:57 +0000824Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
825 QualType Ty) const {
James Y Knight29b5f082016-02-24 02:59:33 +0000826 // The PNaCL ABI is a bit odd, in that varargs don't use normal
827 // function classification. Structs get passed directly for varargs
828 // functions, through a rewriting transform in
829 // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
830 // this target to actually support a va_arg instructions with an
831 // aggregate type, unlike other targets.
832 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Derek Schuff09338a22012-09-06 17:37:28 +0000833}
834
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000835/// \brief Classify argument of given type \p Ty.
836ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
Derek Schuff09338a22012-09-06 17:37:28 +0000837 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +0000838 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +0000839 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
840 return getNaturalAlignIndirect(Ty);
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000841 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
842 // Treat an enum type as its underlying type.
Derek Schuff09338a22012-09-06 17:37:28 +0000843 Ty = EnumTy->getDecl()->getIntegerType();
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000844 } else if (Ty->isFloatingType()) {
845 // Floating-point types don't go inreg.
846 return ABIArgInfo::getDirect();
Derek Schuff09338a22012-09-06 17:37:28 +0000847 }
Eli Bendersky4f6791c2013-04-08 21:31:01 +0000848
849 return (Ty->isPromotableIntegerType() ?
850 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Derek Schuff09338a22012-09-06 17:37:28 +0000851}
852
853ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
854 if (RetTy->isVoidType())
855 return ABIArgInfo::getIgnore();
856
Eli Benderskye20dad62013-04-04 22:49:35 +0000857 // In the PNaCl ABI we always return records/structures on the stack.
Derek Schuff09338a22012-09-06 17:37:28 +0000858 if (isAggregateTypeForABI(RetTy))
John McCall7f416cc2015-09-08 08:05:57 +0000859 return getNaturalAlignIndirect(RetTy);
Derek Schuff09338a22012-09-06 17:37:28 +0000860
861 // Treat an enum type as its underlying type.
862 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
863 RetTy = EnumTy->getDecl()->getIntegerType();
864
865 return (RetTy->isPromotableIntegerType() ?
866 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
867}
868
Chad Rosier651c1832013-03-25 21:00:27 +0000869/// IsX86_MMXType - Return true if this is an MMX type.
870bool IsX86_MMXType(llvm::Type *IRType) {
871 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
Bill Wendling5cd41c42010-10-18 03:41:31 +0000872 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
873 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
874 IRType->getScalarSizeInBits() != 64;
875}
876
Jay Foad7c57be32011-07-11 09:56:20 +0000877static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000878 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000879 llvm::Type* Ty) {
Coby Tayree7b49dc92017-08-24 09:07:34 +0000880 bool IsMMXCons = llvm::StringSwitch<bool>(Constraint)
881 .Cases("y", "&y", "^Ym", true)
882 .Default(false);
883 if (IsMMXCons && Ty->isVectorTy()) {
Tim Northover0ae93912013-06-07 00:04:50 +0000884 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
885 // Invalid MMX constraint
Craig Topper8a13c412014-05-21 05:09:00 +0000886 return nullptr;
Tim Northover0ae93912013-06-07 00:04:50 +0000887 }
888
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000889 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
Tim Northover0ae93912013-06-07 00:04:50 +0000890 }
891
892 // No operation needed
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000893 return Ty;
894}
895
Reid Kleckner80944df2014-10-31 22:00:51 +0000896/// Returns true if this type can be passed in SSE registers with the
897/// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
898static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
899 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Erich Keanede1b2a92017-07-21 18:50:36 +0000900 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) {
901 if (BT->getKind() == BuiltinType::LongDouble) {
902 if (&Context.getTargetInfo().getLongDoubleFormat() ==
903 &llvm::APFloat::x87DoubleExtended())
904 return false;
905 }
Reid Kleckner80944df2014-10-31 22:00:51 +0000906 return true;
Erich Keanede1b2a92017-07-21 18:50:36 +0000907 }
Reid Kleckner80944df2014-10-31 22:00:51 +0000908 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
909 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
910 // registers specially.
911 unsigned VecSize = Context.getTypeSize(VT);
912 if (VecSize == 128 || VecSize == 256 || VecSize == 512)
913 return true;
914 }
915 return false;
916}
917
918/// Returns true if this aggregate is small enough to be passed in SSE registers
919/// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
920static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
921 return NumMembers <= 4;
922}
923
Erich Keane521ed962017-01-05 00:20:51 +0000924/// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
925static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
926 auto AI = ABIArgInfo::getDirect(T);
927 AI.setInReg(true);
928 AI.setCanBeFlattened(false);
929 return AI;
930}
931
Chris Lattner0cf24192010-06-28 20:05:43 +0000932//===----------------------------------------------------------------------===//
933// X86-32 ABI Implementation
934//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000935
Reid Kleckner661f35b2014-01-18 01:12:41 +0000936/// \brief Similar to llvm::CCState, but for Clang.
937struct CCState {
Reid Kleckner80944df2014-10-31 22:00:51 +0000938 CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
Reid Kleckner661f35b2014-01-18 01:12:41 +0000939
940 unsigned CC;
941 unsigned FreeRegs;
Reid Kleckner80944df2014-10-31 22:00:51 +0000942 unsigned FreeSSERegs;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000943};
944
Erich Keane521ed962017-01-05 00:20:51 +0000945enum {
946 // Vectorcall only allows the first 6 parameters to be passed in registers.
947 VectorcallMaxParamNumAsReg = 6
948};
949
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000950/// X86_32ABIInfo - The X86-32 ABI information.
John McCall12f23522016-04-04 18:33:08 +0000951class X86_32ABIInfo : public SwiftABIInfo {
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000952 enum Class {
953 Integer,
954 Float
955 };
956
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000957 static const unsigned MinABIStackAlignInBytes = 4;
958
David Chisnallde3a0692009-08-17 23:08:21 +0000959 bool IsDarwinVectorABI;
Michael Kupersteindc745202015-10-19 07:52:25 +0000960 bool IsRetSmallStructInRegABI;
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +0000961 bool IsWin32StructABI;
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +0000962 bool IsSoftFloatABI;
Michael Kuperstein68901882015-10-25 08:18:20 +0000963 bool IsMCUABI;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000964 unsigned DefaultNumRegisterParameters;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000965
966 static bool isRegisterSize(unsigned Size) {
967 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
968 }
969
Reid Kleckner80944df2014-10-31 22:00:51 +0000970 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
971 // FIXME: Assumes vectorcall is in use.
972 return isX86VectorTypeForVectorCall(getContext(), Ty);
973 }
974
975 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
976 uint64_t NumMembers) const override {
977 // FIXME: Assumes vectorcall is in use.
978 return isX86VectorCallAggregateSmallEnough(NumMembers);
979 }
980
Reid Kleckner40ca9132014-05-13 22:05:45 +0000981 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000982
Daniel Dunbar557893d2010-04-21 19:10:51 +0000983 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
984 /// such that the argument will be passed in memory.
Reid Kleckner661f35b2014-01-18 01:12:41 +0000985 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
986
John McCall7f416cc2015-09-08 08:05:57 +0000987 ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000988
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000989 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000990 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000991
Rafael Espindola06b2b4a2012-07-31 02:44:24 +0000992 Class classify(QualType Ty) const;
Reid Kleckner40ca9132014-05-13 22:05:45 +0000993 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
Reid Kleckner661f35b2014-01-18 01:12:41 +0000994 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
Erich Keane4bd39302017-06-21 16:37:22 +0000995
Michael Kupersteinf3163dc2015-12-28 14:39:54 +0000996 /// \brief Updates the number of available free registers, returns
997 /// true if any registers were allocated.
998 bool updateFreeRegs(QualType Ty, CCState &State) const;
999
1000 bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
1001 bool &NeedsPadding) const;
1002 bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001003
Reid Kleckner04046052016-05-02 17:41:07 +00001004 bool canExpandIndirectArgument(QualType Ty) const;
1005
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001006 /// \brief Rewrite the function info so that all memory arguments use
1007 /// inalloca.
1008 void rewriteWithInAlloca(CGFunctionInfo &FI) const;
1009
1010 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +00001011 CharUnits &StackOffset, ABIArgInfo &Info,
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001012 QualType Type) const;
Erich Keane521ed962017-01-05 00:20:51 +00001013 void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
1014 bool &UsedInAlloca) const;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001015
Rafael Espindola75419dc2012-07-23 23:30:29 +00001016public:
1017
Craig Topper4f12f102014-03-12 06:41:41 +00001018 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00001019 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1020 QualType Ty) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001021
Michael Kupersteindc745202015-10-19 07:52:25 +00001022 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1023 bool RetSmallStructInRegABI, bool Win32StructABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00001024 unsigned NumRegisterParameters, bool SoftFloatABI)
John McCall12f23522016-04-04 18:33:08 +00001025 : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
Michael Kupersteindc745202015-10-19 07:52:25 +00001026 IsRetSmallStructInRegABI(RetSmallStructInRegABI),
1027 IsWin32StructABI(Win32StructABI),
Manuel Klimekab2e28e2015-10-19 08:43:46 +00001028 IsSoftFloatABI(SoftFloatABI),
Michael Kupersteind749f232015-10-27 07:46:22 +00001029 IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
Manuel Klimekab2e28e2015-10-19 08:43:46 +00001030 DefaultNumRegisterParameters(NumRegisterParameters) {}
John McCall12f23522016-04-04 18:33:08 +00001031
1032 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
1033 ArrayRef<llvm::Type*> scalars,
1034 bool asReturnValue) const override {
1035 // LLVM's x86-32 lowering currently only assigns up to three
1036 // integer registers and three fp registers. Oddly, it'll use up to
1037 // four vector registers for vectors, but those can overlap with the
1038 // scalar registers.
1039 return occupiesMoreThan(CGT, scalars, /*total*/ 3);
1040 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00001041
1042 bool isSwiftErrorInRegister() const override {
1043 // x86-32 lowering does not support passing swifterror in a register.
1044 return false;
1045 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001046};
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001047
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001048class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
1049public:
Michael Kupersteindc745202015-10-19 07:52:25 +00001050 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1051 bool RetSmallStructInRegABI, bool Win32StructABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00001052 unsigned NumRegisterParameters, bool SoftFloatABI)
1053 : TargetCodeGenInfo(new X86_32ABIInfo(
1054 CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
1055 NumRegisterParameters, SoftFloatABI)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +00001056
John McCall1fe2a8c2013-06-18 02:46:29 +00001057 static bool isStructReturnInRegABI(
1058 const llvm::Triple &Triple, const CodeGenOptions &Opts);
1059
Eric Christopher162c91c2015-06-05 22:03:00 +00001060 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00001061 CodeGen::CodeGenModule &CGM,
1062 ForDefinition_t IsForDefinition) const override;
John McCallbeec5a02010-03-06 00:35:14 +00001063
Craig Topper4f12f102014-03-12 06:41:41 +00001064 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallbeec5a02010-03-06 00:35:14 +00001065 // Darwin uses different dwarf register numbers for EH.
John McCallc8e01702013-04-16 22:48:15 +00001066 if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
John McCallbeec5a02010-03-06 00:35:14 +00001067 return 4;
1068 }
1069
1070 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00001071 llvm::Value *Address) const override;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001072
Jay Foad7c57be32011-07-11 09:56:20 +00001073 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001074 StringRef Constraint,
Craig Topper4f12f102014-03-12 06:41:41 +00001075 llvm::Type* Ty) const override {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001076 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1077 }
1078
Reid Kleckner9b3e3df2014-09-04 20:04:38 +00001079 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
1080 std::string &Constraints,
1081 std::vector<llvm::Type *> &ResultRegTypes,
1082 std::vector<llvm::Type *> &ResultTruncRegTypes,
1083 std::vector<LValue> &ResultRegDests,
1084 std::string &AsmString,
1085 unsigned NumOutputs) const override;
1086
Craig Topper4f12f102014-03-12 06:41:41 +00001087 llvm::Constant *
1088 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Peter Collingbourneb453cd62013-10-20 21:29:19 +00001089 unsigned Sig = (0xeb << 0) | // jmp rel8
1090 (0x06 << 8) | // .+0x08
Vedant Kumarbb5d4852017-09-13 00:04:35 +00001091 ('v' << 16) |
1092 ('2' << 24);
Peter Collingbourneb453cd62013-10-20 21:29:19 +00001093 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1094 }
John McCall01391782016-02-05 21:37:38 +00001095
1096 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
1097 return "movl\t%ebp, %ebp"
Oliver Stannard7f188642017-08-21 09:54:46 +00001098 "\t\t// marker for objc_retainAutoreleaseReturnValue";
John McCall01391782016-02-05 21:37:38 +00001099 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001100};
1101
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001102}
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001103
Reid Kleckner9b3e3df2014-09-04 20:04:38 +00001104/// Rewrite input constraint references after adding some output constraints.
1105/// In the case where there is one output and one input and we add one output,
1106/// we need to replace all operand references greater than or equal to 1:
1107/// mov $0, $1
1108/// mov eax, $1
1109/// The result will be:
1110/// mov $0, $2
1111/// mov eax, $2
1112static void rewriteInputConstraintReferences(unsigned FirstIn,
1113 unsigned NumNewOuts,
1114 std::string &AsmString) {
1115 std::string Buf;
1116 llvm::raw_string_ostream OS(Buf);
1117 size_t Pos = 0;
1118 while (Pos < AsmString.size()) {
1119 size_t DollarStart = AsmString.find('$', Pos);
1120 if (DollarStart == std::string::npos)
1121 DollarStart = AsmString.size();
1122 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1123 if (DollarEnd == std::string::npos)
1124 DollarEnd = AsmString.size();
1125 OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1126 Pos = DollarEnd;
1127 size_t NumDollars = DollarEnd - DollarStart;
1128 if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1129 // We have an operand reference.
1130 size_t DigitStart = Pos;
1131 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1132 if (DigitEnd == std::string::npos)
1133 DigitEnd = AsmString.size();
1134 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1135 unsigned OperandIndex;
1136 if (!OperandStr.getAsInteger(10, OperandIndex)) {
1137 if (OperandIndex >= FirstIn)
1138 OperandIndex += NumNewOuts;
1139 OS << OperandIndex;
1140 } else {
1141 OS << OperandStr;
1142 }
1143 Pos = DigitEnd;
1144 }
1145 }
1146 AsmString = std::move(OS.str());
1147}
1148
1149/// Add output constraints for EAX:EDX because they are return registers.
1150void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1151 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1152 std::vector<llvm::Type *> &ResultRegTypes,
1153 std::vector<llvm::Type *> &ResultTruncRegTypes,
1154 std::vector<LValue> &ResultRegDests, std::string &AsmString,
1155 unsigned NumOutputs) const {
1156 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1157
1158 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1159 // larger.
1160 if (!Constraints.empty())
1161 Constraints += ',';
1162 if (RetWidth <= 32) {
1163 Constraints += "={eax}";
1164 ResultRegTypes.push_back(CGF.Int32Ty);
1165 } else {
1166 // Use the 'A' constraint for EAX:EDX.
1167 Constraints += "=A";
1168 ResultRegTypes.push_back(CGF.Int64Ty);
1169 }
1170
1171 // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1172 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1173 ResultTruncRegTypes.push_back(CoerceTy);
1174
1175 // Coerce the integer by bitcasting the return slot pointer.
1176 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
1177 CoerceTy->getPointerTo()));
1178 ResultRegDests.push_back(ReturnSlot);
1179
1180 rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1181}
1182
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001183/// shouldReturnTypeInRegister - Determine if the given type should be
Michael Kuperstein68901882015-10-25 08:18:20 +00001184/// returned in a register (for the Darwin and MCU ABI).
Reid Kleckner40ca9132014-05-13 22:05:45 +00001185bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1186 ASTContext &Context) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001187 uint64_t Size = Context.getTypeSize(Ty);
1188
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001189 // For i386, type must be register sized.
1190 // For the MCU ABI, it only needs to be <= 8-byte
1191 if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1192 return false;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001193
1194 if (Ty->isVectorType()) {
1195 // 64- and 128- bit vectors inside structures are not returned in
1196 // registers.
1197 if (Size == 64 || Size == 128)
1198 return false;
1199
1200 return true;
1201 }
1202
Daniel Dunbar4bd95c62010-05-15 00:00:30 +00001203 // If this is a builtin, pointer, enum, complex type, member pointer, or
1204 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +00001205 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +00001206 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +00001207 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001208 return true;
1209
1210 // Arrays are treated like records.
1211 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Reid Kleckner40ca9132014-05-13 22:05:45 +00001212 return shouldReturnTypeInRegister(AT->getElementType(), Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001213
1214 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001215 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001216 if (!RT) return false;
1217
Anders Carlsson40446e82010-01-27 03:25:19 +00001218 // FIXME: Traverse bases here too.
1219
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001220 // Structure types are passed in register if all fields would be
1221 // passed in a register.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001222 for (const auto *FD : RT->getDecl()->fields()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001223 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001224 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001225 continue;
1226
1227 // Check fields recursively.
Reid Kleckner40ca9132014-05-13 22:05:45 +00001228 if (!shouldReturnTypeInRegister(FD->getType(), Context))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001229 return false;
1230 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001231 return true;
1232}
1233
Reid Kleckner04046052016-05-02 17:41:07 +00001234static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
1235 // Treat complex types as the element type.
1236 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
1237 Ty = CTy->getElementType();
1238
1239 // Check for a type which we know has a simple scalar argument-passing
1240 // convention without any padding. (We're specifically looking for 32
1241 // and 64-bit integer and integer-equivalents, float, and double.)
1242 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
1243 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
1244 return false;
1245
1246 uint64_t Size = Context.getTypeSize(Ty);
1247 return Size == 32 || Size == 64;
1248}
1249
Reid Kleckner791bbf62017-01-13 17:18:19 +00001250static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
1251 uint64_t &Size) {
1252 for (const auto *FD : RD->fields()) {
1253 // Scalar arguments on the stack get 4 byte alignment on x86. If the
1254 // argument is smaller than 32-bits, expanding the struct will create
1255 // alignment padding.
1256 if (!is32Or64BitBasicType(FD->getType(), Context))
1257 return false;
1258
1259 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
1260 // how to expand them yet, and the predicate for telling if a bitfield still
1261 // counts as "basic" is more complicated than what we were doing previously.
1262 if (FD->isBitField())
1263 return false;
1264
1265 Size += Context.getTypeSize(FD->getType());
1266 }
1267 return true;
1268}
1269
1270static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
1271 uint64_t &Size) {
1272 // Don't do this if there are any non-empty bases.
1273 for (const CXXBaseSpecifier &Base : RD->bases()) {
1274 if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
1275 Size))
1276 return false;
1277 }
1278 if (!addFieldSizes(Context, RD, Size))
1279 return false;
1280 return true;
1281}
1282
Reid Kleckner04046052016-05-02 17:41:07 +00001283/// Test whether an argument type which is to be passed indirectly (on the
1284/// stack) would have the equivalent layout if it was expanded into separate
1285/// arguments. If so, we prefer to do the latter to avoid inhibiting
1286/// optimizations.
1287bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
1288 // We can only expand structure types.
1289 const RecordType *RT = Ty->getAs<RecordType>();
1290 if (!RT)
1291 return false;
1292 const RecordDecl *RD = RT->getDecl();
Reid Kleckner791bbf62017-01-13 17:18:19 +00001293 uint64_t Size = 0;
Reid Kleckner04046052016-05-02 17:41:07 +00001294 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Reid Kleckner791bbf62017-01-13 17:18:19 +00001295 if (!IsWin32StructABI) {
Reid Kleckner04046052016-05-02 17:41:07 +00001296 // On non-Windows, we have to conservatively match our old bitcode
1297 // prototypes in order to be ABI-compatible at the bitcode level.
1298 if (!CXXRD->isCLike())
1299 return false;
1300 } else {
1301 // Don't do this for dynamic classes.
1302 if (CXXRD->isDynamicClass())
1303 return false;
Reid Kleckner04046052016-05-02 17:41:07 +00001304 }
Reid Kleckner791bbf62017-01-13 17:18:19 +00001305 if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
Reid Kleckner04046052016-05-02 17:41:07 +00001306 return false;
Reid Kleckner791bbf62017-01-13 17:18:19 +00001307 } else {
1308 if (!addFieldSizes(getContext(), RD, Size))
Reid Kleckner04046052016-05-02 17:41:07 +00001309 return false;
Reid Kleckner04046052016-05-02 17:41:07 +00001310 }
1311
1312 // We can do this if there was no alignment padding.
1313 return Size == getContext().getTypeSize(Ty);
1314}
1315
John McCall7f416cc2015-09-08 08:05:57 +00001316ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001317 // If the return value is indirect, then the hidden argument is consuming one
1318 // integer register.
1319 if (State.FreeRegs) {
1320 --State.FreeRegs;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001321 if (!IsMCUABI)
1322 return getNaturalAlignIndirectInReg(RetTy);
Reid Kleckner661f35b2014-01-18 01:12:41 +00001323 }
John McCall7f416cc2015-09-08 08:05:57 +00001324 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
Reid Kleckner661f35b2014-01-18 01:12:41 +00001325}
1326
Eric Christopher7565e0d2015-05-29 23:09:49 +00001327ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1328 CCState &State) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001329 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001330 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001331
Reid Kleckner80944df2014-10-31 22:00:51 +00001332 const Type *Base = nullptr;
1333 uint64_t NumElts = 0;
Erich Keane757d3172016-11-02 18:29:35 +00001334 if ((State.CC == llvm::CallingConv::X86_VectorCall ||
1335 State.CC == llvm::CallingConv::X86_RegCall) &&
Reid Kleckner80944df2014-10-31 22:00:51 +00001336 isHomogeneousAggregate(RetTy, Base, NumElts)) {
1337 // The LLVM struct type for such an aggregate should lower properly.
1338 return ABIArgInfo::getDirect();
1339 }
1340
Chris Lattner458b2aa2010-07-29 02:16:43 +00001341 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001342 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +00001343 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001344 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001345
1346 // 128-bit vectors are a special case; they are returned in
1347 // registers and we need to make sure to pick a type the LLVM
1348 // backend will like.
1349 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001350 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +00001351 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001352
1353 // Always return in register if it fits in a general purpose
1354 // register, or if it is 64 bits and has a single element.
1355 if ((Size == 8 || Size == 16 || Size == 32) ||
1356 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001357 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00001358 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001359
John McCall7f416cc2015-09-08 08:05:57 +00001360 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001361 }
1362
1363 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +00001364 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001365
John McCalla1dee5302010-08-22 10:59:02 +00001366 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +00001367 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +00001368 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001369 if (RT->getDecl()->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00001370 return getIndirectReturnResult(RetTy, State);
Anders Carlsson5789c492009-10-20 22:07:59 +00001371 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001372
David Chisnallde3a0692009-08-17 23:08:21 +00001373 // If specified, structs and unions are always indirect.
Michael Kupersteindc745202015-10-19 07:52:25 +00001374 if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
John McCall7f416cc2015-09-08 08:05:57 +00001375 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001376
Denis Zobnin380b2242016-02-11 11:26:03 +00001377 // Ignore empty structs/unions.
1378 if (isEmptyRecord(getContext(), RetTy, true))
1379 return ABIArgInfo::getIgnore();
1380
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001381 // Small structures which are register sized are generally returned
1382 // in a register.
Reid Kleckner40ca9132014-05-13 22:05:45 +00001383 if (shouldReturnTypeInRegister(RetTy, getContext())) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001384 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanee945342011-11-18 01:25:50 +00001385
1386 // As a special-case, if the struct is a "single-element" struct, and
1387 // the field is of type "float" or "double", return it in a
Eli Friedmana98d1f82012-01-25 22:46:34 +00001388 // floating-point register. (MSVC does not apply this special case.)
1389 // We apply a similar transformation for pointer types to improve the
1390 // quality of the generated IR.
Eli Friedmanee945342011-11-18 01:25:50 +00001391 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00001392 if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
Eli Friedmana98d1f82012-01-25 22:46:34 +00001393 || SeltTy->hasPointerRepresentation())
Eli Friedmanee945342011-11-18 01:25:50 +00001394 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1395
1396 // FIXME: We should be able to narrow this integer in cases with dead
1397 // padding.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001398 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001399 }
1400
John McCall7f416cc2015-09-08 08:05:57 +00001401 return getIndirectReturnResult(RetTy, State);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001402 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001403
Chris Lattner458b2aa2010-07-29 02:16:43 +00001404 // Treat an enum type as its underlying type.
1405 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1406 RetTy = EnumTy->getDecl()->getIntegerType();
1407
1408 return (RetTy->isPromotableIntegerType() ?
1409 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001410}
1411
Eli Friedman7919bea2012-06-05 19:40:46 +00001412static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
1413 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1414}
1415
Daniel Dunbared23de32010-09-16 20:42:00 +00001416static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
1417 const RecordType *RT = Ty->getAs<RecordType>();
1418 if (!RT)
1419 return 0;
1420 const RecordDecl *RD = RT->getDecl();
1421
1422 // If this is a C++ record, check the bases first.
1423 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +00001424 for (const auto &I : CXXRD->bases())
1425 if (!isRecordWithSSEVectorType(Context, I.getType()))
Daniel Dunbared23de32010-09-16 20:42:00 +00001426 return false;
1427
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001428 for (const auto *i : RD->fields()) {
Daniel Dunbared23de32010-09-16 20:42:00 +00001429 QualType FT = i->getType();
1430
Eli Friedman7919bea2012-06-05 19:40:46 +00001431 if (isSSEVectorType(Context, FT))
Daniel Dunbared23de32010-09-16 20:42:00 +00001432 return true;
1433
1434 if (isRecordWithSSEVectorType(Context, FT))
1435 return true;
1436 }
1437
1438 return false;
1439}
1440
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001441unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1442 unsigned Align) const {
1443 // Otherwise, if the alignment is less than or equal to the minimum ABI
1444 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001445 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001446 return 0; // Use default alignment.
1447
1448 // On non-Darwin, the stack type alignment is always 4.
1449 if (!IsDarwinVectorABI) {
1450 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001451 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001452 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001453
Daniel Dunbared23de32010-09-16 20:42:00 +00001454 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7919bea2012-06-05 19:40:46 +00001455 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
1456 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbared23de32010-09-16 20:42:00 +00001457 return 16;
1458
1459 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +00001460}
1461
Rafael Espindola703c47f2012-10-19 05:04:37 +00001462ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
Reid Kleckner661f35b2014-01-18 01:12:41 +00001463 CCState &State) const {
Rafael Espindola703c47f2012-10-19 05:04:37 +00001464 if (!ByVal) {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001465 if (State.FreeRegs) {
1466 --State.FreeRegs; // Non-byval indirects just use one pointer.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001467 if (!IsMCUABI)
1468 return getNaturalAlignIndirectInReg(Ty);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001469 }
John McCall7f416cc2015-09-08 08:05:57 +00001470 return getNaturalAlignIndirect(Ty, false);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001471 }
Daniel Dunbar53fac692010-04-21 19:49:55 +00001472
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001473 // Compute the byval alignment.
1474 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1475 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1476 if (StackAlign == 0)
John McCall7f416cc2015-09-08 08:05:57 +00001477 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +00001478
1479 // If the stack alignment is less than the type alignment, realign the
1480 // argument.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001481 bool Realign = TypeAlign > StackAlign;
John McCall7f416cc2015-09-08 08:05:57 +00001482 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1483 /*ByVal=*/true, Realign);
Daniel Dunbar557893d2010-04-21 19:10:51 +00001484}
1485
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001486X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1487 const Type *T = isSingleElementStruct(Ty, getContext());
1488 if (!T)
1489 T = Ty.getTypePtr();
1490
1491 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1492 BuiltinType::Kind K = BT->getKind();
1493 if (K == BuiltinType::Float || K == BuiltinType::Double)
1494 return Float;
1495 }
1496 return Integer;
1497}
1498
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001499bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00001500 if (!IsSoftFloatABI) {
1501 Class C = classify(Ty);
1502 if (C == Float)
1503 return false;
1504 }
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001505
Rafael Espindola077dd592012-10-24 01:58:58 +00001506 unsigned Size = getContext().getTypeSize(Ty);
1507 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindolae2a9e902012-10-23 02:04:01 +00001508
1509 if (SizeInRegs == 0)
1510 return false;
1511
Michael Kuperstein68901882015-10-25 08:18:20 +00001512 if (!IsMCUABI) {
1513 if (SizeInRegs > State.FreeRegs) {
1514 State.FreeRegs = 0;
1515 return false;
1516 }
1517 } else {
1518 // The MCU psABI allows passing parameters in-reg even if there are
1519 // earlier parameters that are passed on the stack. Also,
1520 // it does not allow passing >8-byte structs in-register,
1521 // even if there are 3 free registers available.
1522 if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1523 return false;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001524 }
Rafael Espindola703c47f2012-10-19 05:04:37 +00001525
Reid Kleckner661f35b2014-01-18 01:12:41 +00001526 State.FreeRegs -= SizeInRegs;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001527 return true;
1528}
1529
1530bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1531 bool &InReg,
1532 bool &NeedsPadding) const {
Reid Kleckner04046052016-05-02 17:41:07 +00001533 // On Windows, aggregates other than HFAs are never passed in registers, and
1534 // they do not consume register slots. Homogenous floating-point aggregates
1535 // (HFAs) have already been dealt with at this point.
1536 if (IsWin32StructABI && isAggregateTypeForABI(Ty))
1537 return false;
1538
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001539 NeedsPadding = false;
1540 InReg = !IsMCUABI;
1541
1542 if (!updateFreeRegs(Ty, State))
1543 return false;
1544
1545 if (IsMCUABI)
1546 return true;
Rafael Espindola077dd592012-10-24 01:58:58 +00001547
Reid Kleckner80944df2014-10-31 22:00:51 +00001548 if (State.CC == llvm::CallingConv::X86_FastCall ||
Erich Keane757d3172016-11-02 18:29:35 +00001549 State.CC == llvm::CallingConv::X86_VectorCall ||
1550 State.CC == llvm::CallingConv::X86_RegCall) {
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001551 if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
Rafael Espindolafad28de2012-10-24 01:59:00 +00001552 NeedsPadding = true;
1553
Rafael Espindola077dd592012-10-24 01:58:58 +00001554 return false;
1555 }
1556
Rafael Espindola703c47f2012-10-19 05:04:37 +00001557 return true;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001558}
1559
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001560bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1561 if (!updateFreeRegs(Ty, State))
1562 return false;
1563
1564 if (IsMCUABI)
1565 return false;
1566
1567 if (State.CC == llvm::CallingConv::X86_FastCall ||
Erich Keane757d3172016-11-02 18:29:35 +00001568 State.CC == llvm::CallingConv::X86_VectorCall ||
1569 State.CC == llvm::CallingConv::X86_RegCall) {
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001570 if (getContext().getTypeSize(Ty) > 32)
1571 return false;
1572
1573 return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1574 Ty->isReferenceType());
1575 }
1576
1577 return true;
1578}
1579
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001580ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1581 CCState &State) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001582 // FIXME: Set alignment on indirect arguments.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001583
Reid Klecknerb1be6832014-11-15 01:41:41 +00001584 Ty = useFirstFieldIfTransparentUnion(Ty);
1585
Reid Kleckner80944df2014-10-31 22:00:51 +00001586 // Check with the C++ ABI first.
1587 const RecordType *RT = Ty->getAs<RecordType>();
1588 if (RT) {
1589 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1590 if (RAA == CGCXXABI::RAA_Indirect) {
1591 return getIndirectResult(Ty, false, State);
1592 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1593 // The field index doesn't matter, we'll fix it up later.
1594 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1595 }
1596 }
1597
Erich Keane4bd39302017-06-21 16:37:22 +00001598 // Regcall uses the concept of a homogenous vector aggregate, similar
1599 // to other targets.
Reid Kleckner80944df2014-10-31 22:00:51 +00001600 const Type *Base = nullptr;
1601 uint64_t NumElts = 0;
Erich Keane4bd39302017-06-21 16:37:22 +00001602 if (State.CC == llvm::CallingConv::X86_RegCall &&
Reid Kleckner80944df2014-10-31 22:00:51 +00001603 isHomogeneousAggregate(Ty, Base, NumElts)) {
Erich Keane521ed962017-01-05 00:20:51 +00001604
Erich Keane4bd39302017-06-21 16:37:22 +00001605 if (State.FreeSSERegs >= NumElts) {
1606 State.FreeSSERegs -= NumElts;
1607 if (Ty->isBuiltinType() || Ty->isVectorType())
Reid Kleckner80944df2014-10-31 22:00:51 +00001608 return ABIArgInfo::getDirect();
Erich Keane4bd39302017-06-21 16:37:22 +00001609 return ABIArgInfo::getExpand();
Reid Kleckner80944df2014-10-31 22:00:51 +00001610 }
Erich Keane4bd39302017-06-21 16:37:22 +00001611 return getIndirectResult(Ty, /*ByVal=*/false, State);
Reid Kleckner80944df2014-10-31 22:00:51 +00001612 }
1613
1614 if (isAggregateTypeForABI(Ty)) {
Reid Kleckner04046052016-05-02 17:41:07 +00001615 // Structures with flexible arrays are always indirect.
1616 // FIXME: This should not be byval!
1617 if (RT && RT->getDecl()->hasFlexibleArrayMember())
1618 return getIndirectResult(Ty, true, State);
Daniel Dunbar557893d2010-04-21 19:10:51 +00001619
Reid Kleckner04046052016-05-02 17:41:07 +00001620 // Ignore empty structs/unions on non-Windows.
1621 if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001622 return ABIArgInfo::getIgnore();
1623
Rafael Espindolafad28de2012-10-24 01:59:00 +00001624 llvm::LLVMContext &LLVMContext = getVMContext();
1625 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
Reid Kleckner04046052016-05-02 17:41:07 +00001626 bool NeedsPadding = false;
1627 bool InReg;
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001628 if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
Rafael Espindola703c47f2012-10-19 05:04:37 +00001629 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Craig Topperac9201a2013-07-08 04:47:18 +00001630 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001631 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001632 if (InReg)
1633 return ABIArgInfo::getDirectInReg(Result);
1634 else
1635 return ABIArgInfo::getDirect(Result);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001636 }
Craig Topper8a13c412014-05-21 05:09:00 +00001637 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
Rafael Espindola703c47f2012-10-19 05:04:37 +00001638
Daniel Dunbar11c08c82009-11-09 01:33:53 +00001639 // Expand small (<= 128-bit) record types when we know that the stack layout
1640 // of those arguments will match the struct. This is important because the
1641 // LLVM backend isn't smart enough to remove byval, which inhibits many
1642 // optimizations.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001643 // Don't do this for the MCU if there are still free integer registers
1644 // (see X86_64 ABI for full explanation).
Reid Kleckner04046052016-05-02 17:41:07 +00001645 if (getContext().getTypeSize(Ty) <= 4 * 32 &&
1646 (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty))
Reid Kleckner661f35b2014-01-18 01:12:41 +00001647 return ABIArgInfo::getExpandWithPadding(
Reid Kleckner80944df2014-10-31 22:00:51 +00001648 State.CC == llvm::CallingConv::X86_FastCall ||
Erich Keane757d3172016-11-02 18:29:35 +00001649 State.CC == llvm::CallingConv::X86_VectorCall ||
1650 State.CC == llvm::CallingConv::X86_RegCall,
Reid Kleckner80944df2014-10-31 22:00:51 +00001651 PaddingType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001652
Reid Kleckner661f35b2014-01-18 01:12:41 +00001653 return getIndirectResult(Ty, true, State);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001654 }
1655
Chris Lattnerd774ae92010-08-26 20:05:13 +00001656 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +00001657 // On Darwin, some vectors are passed in memory, we handle this by passing
1658 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +00001659 if (IsDarwinVectorABI) {
1660 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +00001661 if ((Size == 8 || Size == 16 || Size == 32) ||
1662 (Size == 64 && VT->getNumElements() == 1))
1663 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1664 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +00001665 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001666
Chad Rosier651c1832013-03-25 21:00:27 +00001667 if (IsX86_MMXType(CGT.ConvertType(Ty)))
1668 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001669
Chris Lattnerd774ae92010-08-26 20:05:13 +00001670 return ABIArgInfo::getDirect();
1671 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001672
1673
Chris Lattner458b2aa2010-07-29 02:16:43 +00001674 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1675 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +00001676
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001677 bool InReg = shouldPrimitiveUseInReg(Ty, State);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001678
1679 if (Ty->isPromotableIntegerType()) {
1680 if (InReg)
1681 return ABIArgInfo::getExtendInReg();
1682 return ABIArgInfo::getExtend();
1683 }
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001684
Rafael Espindola703c47f2012-10-19 05:04:37 +00001685 if (InReg)
1686 return ABIArgInfo::getDirectInReg();
1687 return ABIArgInfo::getDirect();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001688}
1689
Erich Keane521ed962017-01-05 00:20:51 +00001690void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
1691 bool &UsedInAlloca) const {
Erich Keane4bd39302017-06-21 16:37:22 +00001692 // Vectorcall x86 works subtly different than in x64, so the format is
1693 // a bit different than the x64 version. First, all vector types (not HVAs)
1694 // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers.
1695 // This differs from the x64 implementation, where the first 6 by INDEX get
1696 // registers.
1697 // After that, integers AND HVAs are assigned Left to Right in the same pass.
1698 // Integers are passed as ECX/EDX if one is available (in order). HVAs will
1699 // first take up the remaining YMM/XMM registers. If insufficient registers
1700 // remain but an integer register (ECX/EDX) is available, it will be passed
1701 // in that, else, on the stack.
Erich Keane521ed962017-01-05 00:20:51 +00001702 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00001703 // First pass do all the vector types.
1704 const Type *Base = nullptr;
1705 uint64_t NumElts = 0;
1706 const QualType& Ty = I.type;
1707 if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
1708 isHomogeneousAggregate(Ty, Base, NumElts)) {
1709 if (State.FreeSSERegs >= NumElts) {
1710 State.FreeSSERegs -= NumElts;
1711 I.info = ABIArgInfo::getDirect();
1712 } else {
1713 I.info = classifyArgumentType(Ty, State);
1714 }
1715 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1716 }
Erich Keane521ed962017-01-05 00:20:51 +00001717 }
Erich Keane4bd39302017-06-21 16:37:22 +00001718
Erich Keane521ed962017-01-05 00:20:51 +00001719 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00001720 // Second pass, do the rest!
1721 const Type *Base = nullptr;
1722 uint64_t NumElts = 0;
1723 const QualType& Ty = I.type;
1724 bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts);
1725
1726 if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) {
1727 // Assign true HVAs (non vector/native FP types).
1728 if (State.FreeSSERegs >= NumElts) {
1729 State.FreeSSERegs -= NumElts;
1730 I.info = getDirectX86Hva();
1731 } else {
1732 I.info = getIndirectResult(Ty, /*ByVal=*/false, State);
1733 }
1734 } else if (!IsHva) {
1735 // Assign all Non-HVAs, so this will exclude Vector/FP args.
1736 I.info = classifyArgumentType(Ty, State);
1737 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1738 }
Erich Keane521ed962017-01-05 00:20:51 +00001739 }
1740}
1741
Rafael Espindolaa6472962012-07-24 00:01:07 +00001742void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner661f35b2014-01-18 01:12:41 +00001743 CCState State(FI.getCallingConvention());
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001744 if (IsMCUABI)
1745 State.FreeRegs = 3;
1746 else if (State.CC == llvm::CallingConv::X86_FastCall)
Reid Kleckner661f35b2014-01-18 01:12:41 +00001747 State.FreeRegs = 2;
Reid Kleckner80944df2014-10-31 22:00:51 +00001748 else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1749 State.FreeRegs = 2;
1750 State.FreeSSERegs = 6;
1751 } else if (FI.getHasRegParm())
Reid Kleckner661f35b2014-01-18 01:12:41 +00001752 State.FreeRegs = FI.getRegParm();
Erich Keane757d3172016-11-02 18:29:35 +00001753 else if (State.CC == llvm::CallingConv::X86_RegCall) {
1754 State.FreeRegs = 5;
1755 State.FreeSSERegs = 8;
1756 } else
Reid Kleckner661f35b2014-01-18 01:12:41 +00001757 State.FreeRegs = DefaultNumRegisterParameters;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001758
Reid Kleckner677539d2014-07-10 01:58:55 +00001759 if (!getCXXABI().classifyReturnType(FI)) {
Reid Kleckner40ca9132014-05-13 22:05:45 +00001760 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
Reid Kleckner677539d2014-07-10 01:58:55 +00001761 } else if (FI.getReturnInfo().isIndirect()) {
1762 // The C++ ABI is not aware of register usage, so we have to check if the
1763 // return value was sret and put it in a register ourselves if appropriate.
1764 if (State.FreeRegs) {
1765 --State.FreeRegs; // The sret parameter consumes a register.
Michael Kupersteinf3163dc2015-12-28 14:39:54 +00001766 if (!IsMCUABI)
1767 FI.getReturnInfo().setInReg(true);
Reid Kleckner677539d2014-07-10 01:58:55 +00001768 }
1769 }
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001770
Peter Collingbournef7706832014-12-12 23:41:25 +00001771 // The chain argument effectively gives us another free register.
1772 if (FI.isChainCall())
1773 ++State.FreeRegs;
1774
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001775 bool UsedInAlloca = false;
Erich Keane521ed962017-01-05 00:20:51 +00001776 if (State.CC == llvm::CallingConv::X86_VectorCall) {
1777 computeVectorCallArgs(FI, State, UsedInAlloca);
1778 } else {
1779 // If not vectorcall, revert to normal behavior.
1780 for (auto &I : FI.arguments()) {
1781 I.info = classifyArgumentType(I.type, State);
1782 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1783 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001784 }
1785
1786 // If we needed to use inalloca for any argument, do a second pass and rewrite
1787 // all the memory arguments to use inalloca.
1788 if (UsedInAlloca)
1789 rewriteWithInAlloca(FI);
1790}
1791
1792void
1793X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +00001794 CharUnits &StackOffset, ABIArgInfo &Info,
1795 QualType Type) const {
1796 // Arguments are always 4-byte-aligned.
1797 CharUnits FieldAlign = CharUnits::fromQuantity(4);
1798
1799 assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct");
Reid Klecknerd378a712014-04-10 19:09:43 +00001800 Info = ABIArgInfo::getInAlloca(FrameFields.size());
1801 FrameFields.push_back(CGT.ConvertTypeForMem(Type));
John McCall7f416cc2015-09-08 08:05:57 +00001802 StackOffset += getContext().getTypeSizeInChars(Type);
Reid Klecknerd378a712014-04-10 19:09:43 +00001803
John McCall7f416cc2015-09-08 08:05:57 +00001804 // Insert padding bytes to respect alignment.
1805 CharUnits FieldEnd = StackOffset;
Rui Ueyama83aa9792016-01-14 21:00:27 +00001806 StackOffset = FieldEnd.alignTo(FieldAlign);
John McCall7f416cc2015-09-08 08:05:57 +00001807 if (StackOffset != FieldEnd) {
1808 CharUnits NumBytes = StackOffset - FieldEnd;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001809 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
John McCall7f416cc2015-09-08 08:05:57 +00001810 Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001811 FrameFields.push_back(Ty);
1812 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001813}
1814
Reid Kleckner852361d2014-07-26 00:12:26 +00001815static bool isArgInAlloca(const ABIArgInfo &Info) {
1816 // Leave ignored and inreg arguments alone.
1817 switch (Info.getKind()) {
1818 case ABIArgInfo::InAlloca:
1819 return true;
1820 case ABIArgInfo::Indirect:
1821 assert(Info.getIndirectByVal());
1822 return true;
1823 case ABIArgInfo::Ignore:
1824 return false;
1825 case ABIArgInfo::Direct:
1826 case ABIArgInfo::Extend:
Reid Kleckner852361d2014-07-26 00:12:26 +00001827 if (Info.getInReg())
1828 return false;
1829 return true;
Reid Kleckner04046052016-05-02 17:41:07 +00001830 case ABIArgInfo::Expand:
1831 case ABIArgInfo::CoerceAndExpand:
1832 // These are aggregate types which are never passed in registers when
1833 // inalloca is involved.
1834 return true;
Reid Kleckner852361d2014-07-26 00:12:26 +00001835 }
1836 llvm_unreachable("invalid enum");
1837}
1838
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001839void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1840 assert(IsWin32StructABI && "inalloca only supported on win32");
1841
1842 // Build a packed struct type for all of the arguments in memory.
1843 SmallVector<llvm::Type *, 6> FrameFields;
1844
John McCall7f416cc2015-09-08 08:05:57 +00001845 // The stack alignment is always 4.
1846 CharUnits StackAlign = CharUnits::fromQuantity(4);
1847
1848 CharUnits StackOffset;
Reid Kleckner852361d2014-07-26 00:12:26 +00001849 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1850
1851 // Put 'this' into the struct before 'sret', if necessary.
1852 bool IsThisCall =
1853 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
1854 ABIArgInfo &Ret = FI.getReturnInfo();
1855 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
1856 isArgInAlloca(I->info)) {
1857 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1858 ++I;
1859 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001860
1861 // Put the sret parameter into the inalloca struct if it's in memory.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001862 if (Ret.isIndirect() && !Ret.getInReg()) {
1863 CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1864 addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
Reid Klecknerfab1e892014-02-25 00:59:14 +00001865 // On Windows, the hidden sret parameter is always returned in eax.
1866 Ret.setInAllocaSRet(IsWin32StructABI);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001867 }
1868
1869 // Skip the 'this' parameter in ecx.
Reid Kleckner852361d2014-07-26 00:12:26 +00001870 if (IsThisCall)
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001871 ++I;
1872
1873 // Put arguments passed in memory into the struct.
1874 for (; I != E; ++I) {
Reid Kleckner852361d2014-07-26 00:12:26 +00001875 if (isArgInAlloca(I->info))
1876 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001877 }
1878
1879 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
John McCall7f416cc2015-09-08 08:05:57 +00001880 /*isPacked=*/true),
1881 StackAlign);
Rafael Espindolaa6472962012-07-24 00:01:07 +00001882}
1883
John McCall7f416cc2015-09-08 08:05:57 +00001884Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
1885 Address VAListAddr, QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001886
John McCall7f416cc2015-09-08 08:05:57 +00001887 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001888
John McCall7f416cc2015-09-08 08:05:57 +00001889 // x86-32 changes the alignment of certain arguments on the stack.
1890 //
1891 // Just messing with TypeInfo like this works because we never pass
1892 // anything indirectly.
1893 TypeInfo.second = CharUnits::fromQuantity(
1894 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
Eli Friedman1d7dd3b2011-11-18 02:12:09 +00001895
John McCall7f416cc2015-09-08 08:05:57 +00001896 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
1897 TypeInfo, CharUnits::fromQuantity(4),
1898 /*AllowHigherAlign*/ true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001899}
1900
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001901bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
1902 const llvm::Triple &Triple, const CodeGenOptions &Opts) {
1903 assert(Triple.getArch() == llvm::Triple::x86);
1904
1905 switch (Opts.getStructReturnConvention()) {
1906 case CodeGenOptions::SRCK_Default:
1907 break;
1908 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return
1909 return false;
1910 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return
1911 return true;
1912 }
1913
Michael Kupersteind749f232015-10-27 07:46:22 +00001914 if (Triple.isOSDarwin() || Triple.isOSIAMCU())
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001915 return true;
1916
1917 switch (Triple.getOS()) {
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001918 case llvm::Triple::DragonFly:
1919 case llvm::Triple::FreeBSD:
1920 case llvm::Triple::OpenBSD:
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001921 case llvm::Triple::Win32:
Reid Kleckner2918fef2014-11-24 22:05:42 +00001922 return true;
Richard Sandiforddcb8d9c2014-07-08 11:10:34 +00001923 default:
1924 return false;
1925 }
1926}
1927
Simon Atanasyan1a116db2017-07-20 20:34:18 +00001928void X86_32TargetCodeGenInfo::setTargetAttributes(
1929 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
1930 ForDefinition_t IsForDefinition) const {
1931 if (!IsForDefinition)
1932 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00001933 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Charles Davis4ea31ab2010-02-13 15:54:06 +00001934 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1935 // Get the LLVM function.
1936 llvm::Function *Fn = cast<llvm::Function>(GV);
1937
1938 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendlinga514ebc2012-10-15 20:36:26 +00001939 llvm::AttrBuilder B;
Bill Wendlingccf94c92012-10-14 03:28:14 +00001940 B.addStackAlignmentAttr(16);
Reid Kleckneree4930b2017-05-02 22:07:37 +00001941 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
Charles Davis4ea31ab2010-02-13 15:54:06 +00001942 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00001943 if (FD->hasAttr<AnyX86InterruptAttr>()) {
1944 llvm::Function *Fn = cast<llvm::Function>(GV);
1945 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
1946 }
Charles Davis4ea31ab2010-02-13 15:54:06 +00001947 }
1948}
1949
John McCallbeec5a02010-03-06 00:35:14 +00001950bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1951 CodeGen::CodeGenFunction &CGF,
1952 llvm::Value *Address) const {
1953 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallbeec5a02010-03-06 00:35:14 +00001954
Chris Lattnerece04092012-02-07 00:39:47 +00001955 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001956
John McCallbeec5a02010-03-06 00:35:14 +00001957 // 0-7 are the eight integer registers; the order is different
1958 // on Darwin (for EH), but the range is the same.
1959 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +00001960 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +00001961
John McCallc8e01702013-04-16 22:48:15 +00001962 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
John McCallbeec5a02010-03-06 00:35:14 +00001963 // 12-16 are st(0..4). Not sure why we stop at 4.
1964 // These have size 16, which is sizeof(long double) on
1965 // platforms with 8-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001966 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCall943fae92010-05-27 06:19:26 +00001967 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001968
John McCallbeec5a02010-03-06 00:35:14 +00001969 } else {
1970 // 9 is %eflags, which doesn't get a size on Darwin for some
1971 // reason.
John McCall7f416cc2015-09-08 08:05:57 +00001972 Builder.CreateAlignedStore(
1973 Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
1974 CharUnits::One());
John McCallbeec5a02010-03-06 00:35:14 +00001975
1976 // 11-16 are st(0..5). Not sure why we stop at 5.
1977 // These have size 12, which is sizeof(long double) on
1978 // platforms with 4-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +00001979 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCall943fae92010-05-27 06:19:26 +00001980 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1981 }
John McCallbeec5a02010-03-06 00:35:14 +00001982
1983 return false;
1984}
1985
Chris Lattner0cf24192010-06-28 20:05:43 +00001986//===----------------------------------------------------------------------===//
1987// X86-64 ABI Implementation
1988//===----------------------------------------------------------------------===//
1989
1990
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001991namespace {
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001992/// The AVX ABI level for X86 targets.
1993enum class X86AVXABILevel {
1994 None,
Ahmed Bougacha0b938282015-06-22 21:31:43 +00001995 AVX,
1996 AVX512
Ahmed Bougachad39a4152015-06-22 21:30:39 +00001997};
1998
1999/// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
2000static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
2001 switch (AVXLevel) {
Ahmed Bougacha0b938282015-06-22 21:31:43 +00002002 case X86AVXABILevel::AVX512:
2003 return 512;
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002004 case X86AVXABILevel::AVX:
2005 return 256;
2006 case X86AVXABILevel::None:
2007 return 128;
2008 }
Yaron Kerenb76cb042015-06-23 09:45:42 +00002009 llvm_unreachable("Unknown AVXLevel");
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002010}
2011
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002012/// X86_64ABIInfo - The X86_64 ABI information.
John McCall12f23522016-04-04 18:33:08 +00002013class X86_64ABIInfo : public SwiftABIInfo {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002014 enum Class {
2015 Integer = 0,
2016 SSE,
2017 SSEUp,
2018 X87,
2019 X87Up,
2020 ComplexX87,
2021 NoClass,
2022 Memory
2023 };
2024
2025 /// merge - Implement the X86_64 ABI merging algorithm.
2026 ///
2027 /// Merge an accumulating classification \arg Accum with a field
2028 /// classification \arg Field.
2029 ///
2030 /// \param Accum - The accumulating classification. This should
2031 /// always be either NoClass or the result of a previous merge
2032 /// call. In addition, this should never be Memory (the caller
2033 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +00002034 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002035
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002036 /// postMerge - Implement the X86_64 ABI post merging algorithm.
2037 ///
2038 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
2039 /// final MEMORY or SSE classes when necessary.
2040 ///
2041 /// \param AggregateSize - The size of the current aggregate in
2042 /// the classification process.
2043 ///
2044 /// \param Lo - The classification for the parts of the type
2045 /// residing in the low word of the containing object.
2046 ///
2047 /// \param Hi - The classification for the parts of the type
2048 /// residing in the higher words of the containing object.
2049 ///
2050 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
2051
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002052 /// classify - Determine the x86_64 register classes in which the
2053 /// given type T should be passed.
2054 ///
2055 /// \param Lo - The classification for the parts of the type
2056 /// residing in the low word of the containing object.
2057 ///
2058 /// \param Hi - The classification for the parts of the type
2059 /// residing in the high word of the containing object.
2060 ///
2061 /// \param OffsetBase - The bit offset of this type in the
2062 /// containing object. Some parameters are classified different
2063 /// depending on whether they straddle an eightbyte boundary.
2064 ///
Eli Friedman96fd2642013-06-12 00:13:45 +00002065 /// \param isNamedArg - Whether the argument in question is a "named"
2066 /// argument, as used in AMD64-ABI 3.5.7.
2067 ///
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002068 /// If a word is unused its result will be NoClass; if a type should
2069 /// be passed in Memory then at least the classification of \arg Lo
2070 /// will be Memory.
2071 ///
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +00002072 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002073 ///
2074 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
2075 /// also be ComplexX87.
Eli Friedman96fd2642013-06-12 00:13:45 +00002076 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
2077 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002078
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002079 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattnera5f58b02011-07-09 17:41:47 +00002080 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
2081 unsigned IROffset, QualType SourceTy,
2082 unsigned SourceOffset) const;
2083 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
2084 unsigned IROffset, QualType SourceTy,
2085 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002086
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002087 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +00002088 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +00002089 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +00002090
2091 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002092 /// such that the argument will be passed in memory.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002093 ///
2094 /// \param freeIntRegs - The number of free integer registers remaining
2095 /// available.
2096 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002097
Chris Lattner458b2aa2010-07-29 02:16:43 +00002098 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002099
Erich Keane757d3172016-11-02 18:29:35 +00002100 ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
2101 unsigned &neededInt, unsigned &neededSSE,
Eli Friedman96fd2642013-06-12 00:13:45 +00002102 bool isNamedArg) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002103
Erich Keane757d3172016-11-02 18:29:35 +00002104 ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
2105 unsigned &NeededSSE) const;
2106
2107 ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
2108 unsigned &NeededSSE) const;
2109
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002110 bool IsIllegalVectorType(QualType Ty) const;
2111
John McCalle0fda732011-04-21 01:20:55 +00002112 /// The 0.98 ABI revision clarified a lot of ambiguities,
2113 /// unfortunately in ways that were not always consistent with
2114 /// certain previous compilers. In particular, platforms which
2115 /// required strict binary compatibility with older versions of GCC
2116 /// may need to exempt themselves.
2117 bool honorsRevision0_98() const {
John McCallc8e01702013-04-16 22:48:15 +00002118 return !getTarget().getTriple().isOSDarwin();
John McCalle0fda732011-04-21 01:20:55 +00002119 }
2120
Richard Smithf667ad52017-08-26 01:04:35 +00002121 /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to
2122 /// classify it as INTEGER (for compatibility with older clang compilers).
David Majnemere2ae2282016-03-04 05:26:16 +00002123 bool classifyIntegerMMXAsSSE() const {
Richard Smithf667ad52017-08-26 01:04:35 +00002124 // Clang <= 3.8 did not do this.
2125 if (getCodeGenOpts().getClangABICompat() <=
2126 CodeGenOptions::ClangABI::Ver3_8)
2127 return false;
2128
David Majnemere2ae2282016-03-04 05:26:16 +00002129 const llvm::Triple &Triple = getTarget().getTriple();
2130 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
2131 return false;
2132 if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
2133 return false;
2134 return true;
2135 }
2136
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002137 X86AVXABILevel AVXLevel;
Derek Schuffc7dd7222012-10-11 15:52:22 +00002138 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
2139 // 64-bit hardware.
2140 bool Has64BitPointers;
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002141
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002142public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002143 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
John McCall12f23522016-04-04 18:33:08 +00002144 SwiftABIInfo(CGT), AVXLevel(AVXLevel),
Derek Schuff8a872f32012-10-11 18:21:13 +00002145 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffc7dd7222012-10-11 15:52:22 +00002146 }
Chris Lattner22a931e2010-06-29 06:01:59 +00002147
John McCalla729c622012-02-17 03:33:10 +00002148 bool isPassedUsingAVXType(QualType type) const {
2149 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002150 // The freeIntRegs argument doesn't matter here.
Eli Friedman96fd2642013-06-12 00:13:45 +00002151 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
2152 /*isNamedArg*/true);
John McCalla729c622012-02-17 03:33:10 +00002153 if (info.isDirect()) {
2154 llvm::Type *ty = info.getCoerceToType();
2155 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
2156 return (vectorTy->getBitWidth() > 128);
2157 }
2158 return false;
2159 }
2160
Craig Topper4f12f102014-03-12 06:41:41 +00002161 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002162
John McCall7f416cc2015-09-08 08:05:57 +00002163 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2164 QualType Ty) const override;
Charles Davisc7d5c942015-09-17 20:55:33 +00002165 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
2166 QualType Ty) const override;
Peter Collingbourne69b004d2015-02-25 23:18:42 +00002167
2168 bool has64BitPointers() const {
2169 return Has64BitPointers;
2170 }
John McCall12f23522016-04-04 18:33:08 +00002171
2172 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
2173 ArrayRef<llvm::Type*> scalars,
2174 bool asReturnValue) const override {
2175 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2176 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00002177 bool isSwiftErrorInRegister() const override {
2178 return true;
2179 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002180};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002181
Chris Lattner04dc9572010-08-31 16:44:54 +00002182/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
Arnold Schwaighofer4fc955e2016-10-12 18:59:24 +00002183class WinX86_64ABIInfo : public SwiftABIInfo {
Chris Lattner04dc9572010-08-31 16:44:54 +00002184public:
Reid Kleckner11a17192015-10-28 22:29:52 +00002185 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT)
Arnold Schwaighofer4fc955e2016-10-12 18:59:24 +00002186 : SwiftABIInfo(CGT),
Reid Kleckner11a17192015-10-28 22:29:52 +00002187 IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002188
Craig Topper4f12f102014-03-12 06:41:41 +00002189 void computeInfo(CGFunctionInfo &FI) const override;
Chris Lattner04dc9572010-08-31 16:44:54 +00002190
John McCall7f416cc2015-09-08 08:05:57 +00002191 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2192 QualType Ty) const override;
Reid Kleckner80944df2014-10-31 22:00:51 +00002193
2194 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
2195 // FIXME: Assumes vectorcall is in use.
2196 return isX86VectorTypeForVectorCall(getContext(), Ty);
2197 }
2198
2199 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
2200 uint64_t NumMembers) const override {
2201 // FIXME: Assumes vectorcall is in use.
2202 return isX86VectorCallAggregateSmallEnough(NumMembers);
2203 }
Reid Kleckner11a17192015-10-28 22:29:52 +00002204
Arnold Schwaighofer4fc955e2016-10-12 18:59:24 +00002205 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
2206 ArrayRef<llvm::Type *> scalars,
2207 bool asReturnValue) const override {
2208 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2209 }
2210
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00002211 bool isSwiftErrorInRegister() const override {
2212 return true;
2213 }
2214
Reid Kleckner11a17192015-10-28 22:29:52 +00002215private:
Erich Keane521ed962017-01-05 00:20:51 +00002216 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
2217 bool IsVectorCall, bool IsRegCall) const;
2218 ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
2219 const ABIArgInfo &current) const;
2220 void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs,
2221 bool IsVectorCall, bool IsRegCall) const;
Reid Kleckner11a17192015-10-28 22:29:52 +00002222
Erich Keane521ed962017-01-05 00:20:51 +00002223 bool IsMingw64;
Chris Lattner04dc9572010-08-31 16:44:54 +00002224};
2225
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002226class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2227public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002228 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
Alexey Bataev00396512015-07-02 03:40:19 +00002229 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002230
John McCalla729c622012-02-17 03:33:10 +00002231 const X86_64ABIInfo &getABIInfo() const {
2232 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
2233 }
2234
Craig Topper4f12f102014-03-12 06:41:41 +00002235 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCallbeec5a02010-03-06 00:35:14 +00002236 return 7;
2237 }
2238
2239 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00002240 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00002241 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002242
John McCall943fae92010-05-27 06:19:26 +00002243 // 0-15 are the 16 integer registers.
2244 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00002245 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +00002246 return false;
2247 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00002248
Jay Foad7c57be32011-07-11 09:56:20 +00002249 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002250 StringRef Constraint,
Craig Topper4f12f102014-03-12 06:41:41 +00002251 llvm::Type* Ty) const override {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00002252 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
2253 }
2254
John McCalla729c622012-02-17 03:33:10 +00002255 bool isNoProtoCallVariadic(const CallArgList &args,
Craig Topper4f12f102014-03-12 06:41:41 +00002256 const FunctionNoProtoType *fnType) const override {
John McCallcbc038a2011-09-21 08:08:30 +00002257 // The default CC on x86-64 sets %al to the number of SSA
2258 // registers used, and GCC sets this when calling an unprototyped
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002259 // function, so we override the default behavior. However, don't do
Eli Friedmanb8e45b22011-12-06 03:08:26 +00002260 // that when AVX types are involved: the ABI explicitly states it is
2261 // undefined, and it doesn't work in practice because of how the ABI
2262 // defines varargs anyway.
Reid Kleckner78af0702013-08-27 23:08:25 +00002263 if (fnType->getCallConv() == CC_C) {
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002264 bool HasAVXType = false;
John McCalla729c622012-02-17 03:33:10 +00002265 for (CallArgList::const_iterator
2266 it = args.begin(), ie = args.end(); it != ie; ++it) {
2267 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
2268 HasAVXType = true;
2269 break;
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002270 }
2271 }
John McCalla729c622012-02-17 03:33:10 +00002272
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00002273 if (!HasAVXType)
2274 return true;
2275 }
John McCallcbc038a2011-09-21 08:08:30 +00002276
John McCalla729c622012-02-17 03:33:10 +00002277 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCallcbc038a2011-09-21 08:08:30 +00002278 }
2279
Craig Topper4f12f102014-03-12 06:41:41 +00002280 llvm::Constant *
2281 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
Vedant Kumarbb5d4852017-09-13 00:04:35 +00002282 unsigned Sig = (0xeb << 0) | // jmp rel8
2283 (0x06 << 8) | // .+0x08
2284 ('v' << 16) |
2285 ('2' << 24);
Peter Collingbourneb453cd62013-10-20 21:29:19 +00002286 return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2287 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00002288
2289 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00002290 CodeGen::CodeGenModule &CGM,
2291 ForDefinition_t IsForDefinition) const override {
2292 if (!IsForDefinition)
2293 return;
Alexey Bataevd51e9932016-01-15 04:06:31 +00002294 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Erich Keanebb9c7042017-08-30 21:17:40 +00002295 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2296 // Get the LLVM function.
2297 auto *Fn = cast<llvm::Function>(GV);
2298
2299 // Now add the 'alignstack' attribute with a value of 16.
2300 llvm::AttrBuilder B;
2301 B.addStackAlignmentAttr(16);
2302 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
2303 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00002304 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2305 llvm::Function *Fn = cast<llvm::Function>(GV);
2306 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2307 }
2308 }
2309 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002310};
2311
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002312class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo {
2313public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002314 PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2315 : X86_64TargetCodeGenInfo(CGT, AVXLevel) {}
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002316
2317 void getDependentLibraryOption(llvm::StringRef Lib,
Alexander Kornienko34eb2072015-04-11 02:00:23 +00002318 llvm::SmallString<24> &Opt) const override {
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002319 Opt = "\01";
Yunzhong Gaod65200c2015-07-20 17:46:56 +00002320 // If the argument contains a space, enclose it in quotes.
2321 if (Lib.find(" ") != StringRef::npos)
2322 Opt += "\"" + Lib.str() + "\"";
2323 else
2324 Opt += Lib;
Alex Rosenberg12207fa2015-01-27 14:47:44 +00002325 }
2326};
2327
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002328static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
Michael Kupersteinf0e4ccf2015-02-16 11:57:43 +00002329 // If the argument does not end in .lib, automatically add the suffix.
2330 // If the argument contains a space, enclose it in quotes.
2331 // This matches the behavior of MSVC.
2332 bool Quote = (Lib.find(" ") != StringRef::npos);
2333 std::string ArgStr = Quote ? "\"" : "";
2334 ArgStr += Lib;
Rui Ueyama727025a2013-10-31 19:12:53 +00002335 if (!Lib.endswith_lower(".lib"))
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002336 ArgStr += ".lib";
Michael Kupersteinf0e4ccf2015-02-16 11:57:43 +00002337 ArgStr += Quote ? "\"" : "";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002338 return ArgStr;
2339}
2340
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002341class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2342public:
John McCall1fe2a8c2013-06-18 02:46:29 +00002343 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Michael Kupersteindc745202015-10-19 07:52:25 +00002344 bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2345 unsigned NumRegisterParameters)
2346 : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
Michael Kupersteinb1ec50d2015-10-19 08:09:43 +00002347 Win32StructABI, NumRegisterParameters, false) {}
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002348
Eric Christopher162c91c2015-06-05 22:03:00 +00002349 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00002350 CodeGen::CodeGenModule &CGM,
2351 ForDefinition_t IsForDefinition) const override;
Hans Wennborg77dc2362015-01-20 19:45:50 +00002352
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002353 void getDependentLibraryOption(llvm::StringRef Lib,
Craig Topper4f12f102014-03-12 06:41:41 +00002354 llvm::SmallString<24> &Opt) const override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002355 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002356 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002357 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00002358
2359 void getDetectMismatchOption(llvm::StringRef Name,
2360 llvm::StringRef Value,
Craig Topper4f12f102014-03-12 06:41:41 +00002361 llvm::SmallString<32> &Opt) const override {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00002362 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00002363 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002364};
2365
Hans Wennborg77dc2362015-01-20 19:45:50 +00002366static void addStackProbeSizeTargetAttribute(const Decl *D,
2367 llvm::GlobalValue *GV,
2368 CodeGen::CodeGenModule &CGM) {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00002369 if (D && isa<FunctionDecl>(D)) {
Hans Wennborg77dc2362015-01-20 19:45:50 +00002370 if (CGM.getCodeGenOpts().StackProbeSize != 4096) {
2371 llvm::Function *Fn = cast<llvm::Function>(GV);
2372
Eric Christopher7565e0d2015-05-29 23:09:49 +00002373 Fn->addFnAttr("stack-probe-size",
2374 llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
Hans Wennborg77dc2362015-01-20 19:45:50 +00002375 }
2376 }
2377}
2378
Simon Atanasyan1a116db2017-07-20 20:34:18 +00002379void WinX86_32TargetCodeGenInfo::setTargetAttributes(
2380 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
2381 ForDefinition_t IsForDefinition) const {
2382 X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition);
2383 if (!IsForDefinition)
2384 return;
Hans Wennborg77dc2362015-01-20 19:45:50 +00002385 addStackProbeSizeTargetAttribute(D, GV, CGM);
2386}
2387
Chris Lattner04dc9572010-08-31 16:44:54 +00002388class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2389public:
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002390 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2391 X86AVXABILevel AVXLevel)
Alexey Bataev00396512015-07-02 03:40:19 +00002392 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
Chris Lattner04dc9572010-08-31 16:44:54 +00002393
Eric Christopher162c91c2015-06-05 22:03:00 +00002394 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00002395 CodeGen::CodeGenModule &CGM,
2396 ForDefinition_t IsForDefinition) const override;
Hans Wennborg77dc2362015-01-20 19:45:50 +00002397
Craig Topper4f12f102014-03-12 06:41:41 +00002398 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
Chris Lattner04dc9572010-08-31 16:44:54 +00002399 return 7;
2400 }
2401
2402 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00002403 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00002404 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002405
Chris Lattner04dc9572010-08-31 16:44:54 +00002406 // 0-15 are the 16 integer registers.
2407 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00002408 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattner04dc9572010-08-31 16:44:54 +00002409 return false;
2410 }
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002411
2412 void getDependentLibraryOption(llvm::StringRef Lib,
Craig Topper4f12f102014-03-12 06:41:41 +00002413 llvm::SmallString<24> &Opt) const override {
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002414 Opt = "/DEFAULTLIB:";
Aaron Ballmanef50ee92013-05-24 15:06:56 +00002415 Opt += qualifyWindowsLibrary(Lib);
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002416 }
Aaron Ballman5d041be2013-06-04 02:07:14 +00002417
2418 void getDetectMismatchOption(llvm::StringRef Name,
2419 llvm::StringRef Value,
Craig Topper4f12f102014-03-12 06:41:41 +00002420 llvm::SmallString<32> &Opt) const override {
Eli Friedmanf60b8ce2013-06-07 22:42:22 +00002421 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
Aaron Ballman5d041be2013-06-04 02:07:14 +00002422 }
Chris Lattner04dc9572010-08-31 16:44:54 +00002423};
2424
Simon Atanasyan1a116db2017-07-20 20:34:18 +00002425void WinX86_64TargetCodeGenInfo::setTargetAttributes(
2426 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
2427 ForDefinition_t IsForDefinition) const {
2428 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition);
2429 if (!IsForDefinition)
2430 return;
Alexey Bataevd51e9932016-01-15 04:06:31 +00002431 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Erich Keanebb9c7042017-08-30 21:17:40 +00002432 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2433 // Get the LLVM function.
2434 auto *Fn = cast<llvm::Function>(GV);
2435
2436 // Now add the 'alignstack' attribute with a value of 16.
2437 llvm::AttrBuilder B;
2438 B.addStackAlignmentAttr(16);
2439 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
2440 }
Alexey Bataevd51e9932016-01-15 04:06:31 +00002441 if (FD->hasAttr<AnyX86InterruptAttr>()) {
2442 llvm::Function *Fn = cast<llvm::Function>(GV);
2443 Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2444 }
2445 }
2446
Hans Wennborg77dc2362015-01-20 19:45:50 +00002447 addStackProbeSizeTargetAttribute(D, GV, CGM);
2448}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002449}
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002450
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002451void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2452 Class &Hi) const {
2453 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2454 //
2455 // (a) If one of the classes is Memory, the whole argument is passed in
2456 // memory.
2457 //
2458 // (b) If X87UP is not preceded by X87, the whole argument is passed in
2459 // memory.
2460 //
2461 // (c) If the size of the aggregate exceeds two eightbytes and the first
2462 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2463 // argument is passed in memory. NOTE: This is necessary to keep the
2464 // ABI working for processors that don't support the __m256 type.
2465 //
2466 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2467 //
2468 // Some of these are enforced by the merging logic. Others can arise
2469 // only with unions; for example:
2470 // union { _Complex double; unsigned; }
2471 //
2472 // Note that clauses (b) and (c) were added in 0.98.
2473 //
2474 if (Hi == Memory)
2475 Lo = Memory;
2476 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2477 Lo = Memory;
2478 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2479 Lo = Memory;
2480 if (Hi == SSEUp && Lo != SSE)
2481 Hi = SSE;
2482}
2483
Chris Lattnerd776fb12010-06-28 21:43:59 +00002484X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002485 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2486 // classified recursively so that always two fields are
2487 // considered. The resulting class is calculated according to
2488 // the classes of the fields in the eightbyte:
2489 //
2490 // (a) If both classes are equal, this is the resulting class.
2491 //
2492 // (b) If one of the classes is NO_CLASS, the resulting class is
2493 // the other class.
2494 //
2495 // (c) If one of the classes is MEMORY, the result is the MEMORY
2496 // class.
2497 //
2498 // (d) If one of the classes is INTEGER, the result is the
2499 // INTEGER.
2500 //
2501 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2502 // MEMORY is used as class.
2503 //
2504 // (f) Otherwise class SSE is used.
2505
2506 // Accum should never be memory (we should have returned) or
2507 // ComplexX87 (because this cannot be passed in a structure).
2508 assert((Accum != Memory && Accum != ComplexX87) &&
2509 "Invalid accumulated classification during merge.");
2510 if (Accum == Field || Field == NoClass)
2511 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002512 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002513 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002514 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002515 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002516 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002517 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002518 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2519 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002520 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002521 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002522}
2523
Chris Lattner5c740f12010-06-30 19:14:05 +00002524void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Eli Friedman96fd2642013-06-12 00:13:45 +00002525 Class &Lo, Class &Hi, bool isNamedArg) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002526 // FIXME: This code can be simplified by introducing a simple value class for
2527 // Class pairs with appropriate constructor methods for the various
2528 // situations.
2529
2530 // FIXME: Some of the split computations are wrong; unaligned vectors
2531 // shouldn't be passed in registers for example, so there is no chance they
2532 // can straddle an eightbyte. Verify & simplify.
2533
2534 Lo = Hi = NoClass;
2535
2536 Class &Current = OffsetBase < 64 ? Lo : Hi;
2537 Current = Memory;
2538
John McCall9dd450b2009-09-21 23:43:11 +00002539 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002540 BuiltinType::Kind k = BT->getKind();
2541
2542 if (k == BuiltinType::Void) {
2543 Current = NoClass;
2544 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2545 Lo = Integer;
2546 Hi = Integer;
2547 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2548 Current = Integer;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002549 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002550 Current = SSE;
2551 } else if (k == BuiltinType::LongDouble) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002552 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002553 if (LDF == &llvm::APFloat::IEEEquad()) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002554 Lo = SSE;
2555 Hi = SSEUp;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002556 } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002557 Lo = X87;
2558 Hi = X87Up;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002559 } else if (LDF == &llvm::APFloat::IEEEdouble()) {
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002560 Current = SSE;
2561 } else
2562 llvm_unreachable("unexpected long double representation!");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002563 }
2564 // FIXME: _Decimal32 and _Decimal64 are SSE.
2565 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00002566 return;
2567 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002568
Chris Lattnerd776fb12010-06-28 21:43:59 +00002569 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002570 // Classify the underlying integer type.
Eli Friedman96fd2642013-06-12 00:13:45 +00002571 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002572 return;
2573 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002574
Chris Lattnerd776fb12010-06-28 21:43:59 +00002575 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002576 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00002577 return;
2578 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002579
Chris Lattnerd776fb12010-06-28 21:43:59 +00002580 if (Ty->isMemberPointerType()) {
Jan Wen Voung01c21e82014-10-02 16:56:57 +00002581 if (Ty->isMemberFunctionPointerType()) {
2582 if (Has64BitPointers) {
2583 // If Has64BitPointers, this is an {i64, i64}, so classify both
2584 // Lo and Hi now.
2585 Lo = Hi = Integer;
2586 } else {
2587 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2588 // straddles an eightbyte boundary, Hi should be classified as well.
2589 uint64_t EB_FuncPtr = (OffsetBase) / 64;
2590 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2591 if (EB_FuncPtr != EB_ThisAdj) {
2592 Lo = Hi = Integer;
2593 } else {
2594 Current = Integer;
2595 }
2596 }
2597 } else {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00002598 Current = Integer;
Jan Wen Voung01c21e82014-10-02 16:56:57 +00002599 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00002600 return;
2601 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002602
Chris Lattnerd776fb12010-06-28 21:43:59 +00002603 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002604 uint64_t Size = getContext().getTypeSize(VT);
David Majnemerf8d14db2015-07-17 05:49:13 +00002605 if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2606 // gcc passes the following as integer:
2607 // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2608 // 2 bytes - <2 x char>, <1 x short>
2609 // 1 byte - <1 x char>
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002610 Current = Integer;
2611
2612 // If this type crosses an eightbyte boundary, it should be
2613 // split.
David Majnemerf8d14db2015-07-17 05:49:13 +00002614 uint64_t EB_Lo = (OffsetBase) / 64;
2615 uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2616 if (EB_Lo != EB_Hi)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002617 Hi = Lo;
2618 } else if (Size == 64) {
David Majnemere2ae2282016-03-04 05:26:16 +00002619 QualType ElementType = VT->getElementType();
2620
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002621 // gcc passes <1 x double> in memory. :(
David Majnemere2ae2282016-03-04 05:26:16 +00002622 if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002623 return;
2624
David Majnemere2ae2282016-03-04 05:26:16 +00002625 // gcc passes <1 x long long> as SSE but clang used to unconditionally
2626 // pass them as integer. For platforms where clang is the de facto
2627 // platform compiler, we must continue to use integer.
2628 if (!classifyIntegerMMXAsSSE() &&
2629 (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2630 ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2631 ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2632 ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002633 Current = Integer;
2634 else
2635 Current = SSE;
2636
2637 // If this type crosses an eightbyte boundary, it should be
2638 // split.
2639 if (OffsetBase && OffsetBase != 64)
2640 Hi = Lo;
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002641 } else if (Size == 128 ||
2642 (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002643 // Arguments of 256-bits are split into four eightbyte chunks. The
2644 // least significant one belongs to class SSE and all the others to class
2645 // SSEUP. The original Lo and Hi design considers that types can't be
2646 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2647 // This design isn't correct for 256-bits, but since there're no cases
2648 // where the upper parts would need to be inspected, avoid adding
2649 // complexity and just consider Hi to match the 64-256 part.
Eli Friedman96fd2642013-06-12 00:13:45 +00002650 //
2651 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2652 // registers if they are "named", i.e. not part of the "..." of a
2653 // variadic function.
Ahmed Bougacha0b938282015-06-22 21:31:43 +00002654 //
2655 // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2656 // split into eight eightbyte chunks, one SSE and seven SSEUP.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002657 Lo = SSE;
2658 Hi = SSEUp;
2659 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00002660 return;
2661 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002662
Chris Lattnerd776fb12010-06-28 21:43:59 +00002663 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002664 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002665
Chris Lattner2b037972010-07-29 02:01:43 +00002666 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00002667 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002668 if (Size <= 64)
2669 Current = Integer;
2670 else if (Size <= 128)
2671 Lo = Hi = Integer;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002672 } else if (ET == getContext().FloatTy) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002673 Current = SSE;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002674 } else if (ET == getContext().DoubleTy) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002675 Lo = Hi = SSE;
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002676 } else if (ET == getContext().LongDoubleTy) {
2677 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002678 if (LDF == &llvm::APFloat::IEEEquad())
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002679 Current = Memory;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002680 else if (LDF == &llvm::APFloat::x87DoubleExtended())
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002681 Current = ComplexX87;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002682 else if (LDF == &llvm::APFloat::IEEEdouble())
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002683 Lo = Hi = SSE;
2684 else
2685 llvm_unreachable("unexpected long double representation!");
2686 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002687
2688 // If this complex type crosses an eightbyte boundary then it
2689 // should be split.
2690 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00002691 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002692 if (Hi == NoClass && EB_Real != EB_Imag)
2693 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002694
Chris Lattnerd776fb12010-06-28 21:43:59 +00002695 return;
2696 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002697
Chris Lattner2b037972010-07-29 02:01:43 +00002698 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002699 // Arrays are treated like structures.
2700
Chris Lattner2b037972010-07-29 02:01:43 +00002701 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002702
2703 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
David Majnemerb229cb02016-08-15 06:39:18 +00002704 // than eight eightbytes, ..., it has class MEMORY.
2705 if (Size > 512)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002706 return;
2707
2708 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2709 // fields, it has class MEMORY.
2710 //
2711 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00002712 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002713 return;
2714
2715 // Otherwise implement simplified merge. We could be smarter about
2716 // this, but it isn't worth it and would be harder to verify.
2717 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00002718 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002719 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00002720
2721 // The only case a 256-bit wide vector could be used is when the array
2722 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2723 // to work for sizes wider than 128, early check and fallback to memory.
David Majnemerb229cb02016-08-15 06:39:18 +00002724 //
2725 if (Size > 128 &&
2726 (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00002727 return;
2728
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002729 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
2730 Class FieldLo, FieldHi;
Eli Friedman96fd2642013-06-12 00:13:45 +00002731 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002732 Lo = merge(Lo, FieldLo);
2733 Hi = merge(Hi, FieldHi);
2734 if (Lo == Memory || Hi == Memory)
2735 break;
2736 }
2737
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002738 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002739 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002740 return;
2741 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002742
Chris Lattnerd776fb12010-06-28 21:43:59 +00002743 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00002744 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002745
2746 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
David Majnemerb229cb02016-08-15 06:39:18 +00002747 // than eight eightbytes, ..., it has class MEMORY.
2748 if (Size > 512)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002749 return;
2750
Anders Carlsson20759ad2009-09-16 15:53:40 +00002751 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
2752 // copy constructor or a non-trivial destructor, it is passed by invisible
2753 // reference.
Mark Lacey3825e832013-10-06 01:33:34 +00002754 if (getRecordArgABI(RT, getCXXABI()))
Anders Carlsson20759ad2009-09-16 15:53:40 +00002755 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002756
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002757 const RecordDecl *RD = RT->getDecl();
2758
2759 // Assume variable sized types are passed in memory.
2760 if (RD->hasFlexibleArrayMember())
2761 return;
2762
Chris Lattner2b037972010-07-29 02:01:43 +00002763 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002764
2765 // Reset Lo class, this will be recomputed.
2766 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002767
2768 // If this is a C++ record, classify the bases first.
2769 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +00002770 for (const auto &I : CXXRD->bases()) {
2771 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002772 "Unexpected base class!");
2773 const CXXRecordDecl *Base =
Aaron Ballman574705e2014-03-13 15:41:46 +00002774 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002775
2776 // Classify this field.
2777 //
2778 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
2779 // single eightbyte, each is classified separately. Each eightbyte gets
2780 // initialized to class NO_CLASS.
2781 Class FieldLo, FieldHi;
Benjamin Kramer2ef30312012-07-04 18:45:14 +00002782 uint64_t Offset =
2783 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Aaron Ballman574705e2014-03-13 15:41:46 +00002784 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002785 Lo = merge(Lo, FieldLo);
2786 Hi = merge(Hi, FieldHi);
David Majnemercefbc7c2015-07-08 05:14:29 +00002787 if (Lo == Memory || Hi == Memory) {
2788 postMerge(Size, Lo, Hi);
2789 return;
2790 }
Daniel Dunbare1cd0152009-11-22 23:01:23 +00002791 }
2792 }
2793
2794 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002795 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00002796 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002797 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002798 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2799 bool BitField = i->isBitField();
2800
David Majnemerb439dfe2016-08-15 07:20:40 +00002801 // Ignore padding bit-fields.
2802 if (BitField && i->isUnnamedBitfield())
2803 continue;
2804
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002805 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
2806 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002807 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002808 // The only case a 256-bit wide vector could be used is when the struct
2809 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2810 // to work for sizes wider than 128, early check and fallback to memory.
2811 //
David Majnemerb229cb02016-08-15 06:39:18 +00002812 if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) ||
2813 Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002814 Lo = Memory;
David Majnemer699dd042015-07-08 05:07:05 +00002815 postMerge(Size, Lo, Hi);
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00002816 return;
2817 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002818 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00002819 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002820 Lo = Memory;
David Majnemer699dd042015-07-08 05:07:05 +00002821 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002822 return;
2823 }
2824
2825 // Classify this field.
2826 //
2827 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
2828 // exceeds a single eightbyte, each is classified
2829 // separately. Each eightbyte gets initialized to class
2830 // NO_CLASS.
2831 Class FieldLo, FieldHi;
2832
2833 // Bit-fields require special handling, they do not force the
2834 // structure to be passed in memory even if unaligned, and
2835 // therefore they can straddle an eightbyte.
2836 if (BitField) {
David Majnemerb439dfe2016-08-15 07:20:40 +00002837 assert(!i->isUnnamedBitfield());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002838 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00002839 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002840
2841 uint64_t EB_Lo = Offset / 64;
2842 uint64_t EB_Hi = (Offset + Size - 1) / 64;
Sylvestre Ledru0c4813e2013-10-06 09:54:18 +00002843
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002844 if (EB_Lo) {
2845 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
2846 FieldLo = NoClass;
2847 FieldHi = Integer;
2848 } else {
2849 FieldLo = Integer;
2850 FieldHi = EB_Hi ? Integer : NoClass;
2851 }
2852 } else
Eli Friedman96fd2642013-06-12 00:13:45 +00002853 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002854 Lo = merge(Lo, FieldLo);
2855 Hi = merge(Hi, FieldHi);
2856 if (Lo == Memory || Hi == Memory)
2857 break;
2858 }
2859
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002860 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002861 }
2862}
2863
Chris Lattner22a931e2010-06-29 06:01:59 +00002864ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00002865 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2866 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00002867 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00002868 // Treat an enum type as its underlying type.
2869 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2870 Ty = EnumTy->getDecl()->getIntegerType();
2871
2872 return (Ty->isPromotableIntegerType() ?
2873 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2874 }
2875
John McCall7f416cc2015-09-08 08:05:57 +00002876 return getNaturalAlignIndirect(Ty);
Daniel Dunbar53fac692010-04-21 19:49:55 +00002877}
2878
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002879bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
2880 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
2881 uint64_t Size = getContext().getTypeSize(VecTy);
Ahmed Bougachad39a4152015-06-22 21:30:39 +00002882 unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002883 if (Size <= 64 || Size > LargestVector)
2884 return true;
2885 }
2886
2887 return false;
2888}
2889
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002890ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
2891 unsigned freeIntRegs) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002892 // If this is a scalar LLVM value then assume LLVM will pass it in the right
2893 // place naturally.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002894 //
2895 // This assumption is optimistic, as there could be free registers available
2896 // when we need to pass this argument in memory, and LLVM could try to pass
2897 // the argument in the free register. This does not seem to happen currently,
2898 // but this code would be much safer if we could mark the argument with
2899 // 'onstack'. See PR12193.
Eli Friedmanbfd5add2011-12-02 00:11:43 +00002900 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002901 // Treat an enum type as its underlying type.
2902 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2903 Ty = EnumTy->getDecl()->getIntegerType();
2904
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002905 return (Ty->isPromotableIntegerType() ?
2906 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002907 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002908
Mark Lacey3825e832013-10-06 01:33:34 +00002909 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00002910 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Anders Carlsson20759ad2009-09-16 15:53:40 +00002911
Chris Lattner44c2b902011-05-22 23:21:23 +00002912 // Compute the byval alignment. We specify the alignment of the byval in all
2913 // cases so that the mid-level optimizer knows the alignment of the byval.
2914 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002915
2916 // Attempt to avoid passing indirect results using byval when possible. This
2917 // is important for good codegen.
2918 //
2919 // We do this by coercing the value into a scalar type which the backend can
2920 // handle naturally (i.e., without using byval).
2921 //
2922 // For simplicity, we currently only do this when we have exhausted all of the
2923 // free integer registers. Doing this when there are free integer registers
2924 // would require more care, as we would have to ensure that the coerced value
2925 // did not claim the unused register. That would require either reording the
2926 // arguments to the function (so that any subsequent inreg values came first),
2927 // or only doing this optimization when there were no following arguments that
2928 // might be inreg.
2929 //
2930 // We currently expect it to be rare (particularly in well written code) for
2931 // arguments to be passed on the stack when there are still free integer
2932 // registers available (this would typically imply large structs being passed
2933 // by value), so this seems like a fair tradeoff for now.
2934 //
2935 // We can revisit this if the backend grows support for 'onstack' parameter
2936 // attributes. See PR12193.
2937 if (freeIntRegs == 0) {
2938 uint64_t Size = getContext().getTypeSize(Ty);
2939
2940 // If this type fits in an eightbyte, coerce it into the matching integral
2941 // type, which will end up on the stack (with alignment 8).
2942 if (Align == 8 && Size <= 64)
2943 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2944 Size));
2945 }
2946
John McCall7f416cc2015-09-08 08:05:57 +00002947 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002948}
2949
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002950/// The ABI specifies that a value should be passed in a full vector XMM/YMM
2951/// register. Pick an LLVM IR type that will be passed as a vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002952llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002953 // Wrapper structs/arrays that only contain vectors are passed just like
2954 // vectors; strip them off if present.
2955 if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
2956 Ty = QualType(InnerTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002957
Sanjay Pateleb2af4e2015-02-16 17:26:51 +00002958 llvm::Type *IRType = CGT.ConvertType(Ty);
Chih-Hung Hsieh241a8902015-08-10 17:33:31 +00002959 if (isa<llvm::VectorType>(IRType) ||
2960 IRType->getTypeID() == llvm::Type::FP128TyID)
Andrea Di Biagioe7347c62015-06-02 19:34:40 +00002961 return IRType;
2962
2963 // We couldn't find the preferred IR vector type for 'Ty'.
2964 uint64_t Size = getContext().getTypeSize(Ty);
David Majnemerb229cb02016-08-15 06:39:18 +00002965 assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
Andrea Di Biagioe7347c62015-06-02 19:34:40 +00002966
2967 // Return a LLVM IR vector type based on the size of 'Ty'.
2968 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
2969 Size / 64);
Chris Lattner4200fe42010-07-29 04:56:46 +00002970}
2971
Chris Lattnerc8b7b532010-07-29 07:30:00 +00002972/// BitsContainNoUserData - Return true if the specified [start,end) bit range
2973/// is known to either be off the end of the specified type or being in
2974/// alignment padding. The user type specified is known to be at most 128 bits
2975/// in size, and have passed through X86_64ABIInfo::classify with a successful
2976/// classification that put one of the two halves in the INTEGER class.
2977///
2978/// It is conservatively correct to return false.
2979static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
2980 unsigned EndBit, ASTContext &Context) {
2981 // If the bytes being queried are off the end of the type, there is no user
2982 // data hiding here. This handles analysis of builtins, vectors and other
2983 // types that don't contain interesting padding.
2984 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
2985 if (TySize <= StartBit)
2986 return true;
2987
Chris Lattner98076a22010-07-29 07:43:55 +00002988 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2989 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
2990 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
2991
2992 // Check each element to see if the element overlaps with the queried range.
2993 for (unsigned i = 0; i != NumElts; ++i) {
2994 // If the element is after the span we care about, then we're done..
2995 unsigned EltOffset = i*EltSize;
2996 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002997
Chris Lattner98076a22010-07-29 07:43:55 +00002998 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
2999 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
3000 EndBit-EltOffset, Context))
3001 return false;
3002 }
3003 // If it overlaps no elements, then it is safe to process as padding.
3004 return true;
3005 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003006
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003007 if (const RecordType *RT = Ty->getAs<RecordType>()) {
3008 const RecordDecl *RD = RT->getDecl();
3009 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003010
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003011 // If this is a C++ record, check the bases first.
3012 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
Aaron Ballman574705e2014-03-13 15:41:46 +00003013 for (const auto &I : CXXRD->bases()) {
3014 assert(!I.isVirtual() && !I.getType()->isDependentType() &&
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003015 "Unexpected base class!");
3016 const CXXRecordDecl *Base =
Aaron Ballman574705e2014-03-13 15:41:46 +00003017 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003018
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003019 // If the base is after the span we care about, ignore it.
Benjamin Kramer2ef30312012-07-04 18:45:14 +00003020 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003021 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003022
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003023 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
Aaron Ballman574705e2014-03-13 15:41:46 +00003024 if (!BitsContainNoUserData(I.getType(), BaseStart,
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003025 EndBit-BaseOffset, Context))
3026 return false;
3027 }
3028 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003029
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003030 // Verify that no field has data that overlaps the region of interest. Yes
3031 // this could be sped up a lot by being smarter about queried fields,
3032 // however we're only looking at structs up to 16 bytes, so we don't care
3033 // much.
3034 unsigned idx = 0;
3035 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3036 i != e; ++i, ++idx) {
3037 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003038
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003039 // If we found a field after the region we care about, then we're done.
3040 if (FieldOffset >= EndBit) break;
3041
3042 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
3043 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
3044 Context))
3045 return false;
3046 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003047
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003048 // If nothing in this record overlapped the area of interest, then we're
3049 // clean.
3050 return true;
3051 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003052
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003053 return false;
3054}
3055
Chris Lattnere556a712010-07-29 18:39:32 +00003056/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
3057/// float member at the specified offset. For example, {int,{float}} has a
3058/// float at offset 4. It is conservatively correct for this routine to return
3059/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00003060static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmowdd31ca12012-10-08 16:25:52 +00003061 const llvm::DataLayout &TD) {
Chris Lattnere556a712010-07-29 18:39:32 +00003062 // Base case if we find a float.
3063 if (IROffset == 0 && IRType->isFloatTy())
3064 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003065
Chris Lattnere556a712010-07-29 18:39:32 +00003066 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00003067 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00003068 const llvm::StructLayout *SL = TD.getStructLayout(STy);
3069 unsigned Elt = SL->getElementContainingOffset(IROffset);
3070 IROffset -= SL->getElementOffset(Elt);
3071 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
3072 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003073
Chris Lattnere556a712010-07-29 18:39:32 +00003074 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00003075 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3076 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00003077 unsigned EltSize = TD.getTypeAllocSize(EltTy);
3078 IROffset -= IROffset/EltSize*EltSize;
3079 return ContainsFloatAtOffset(EltTy, IROffset, TD);
3080 }
3081
3082 return false;
3083}
3084
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003085
3086/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
3087/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003088llvm::Type *X86_64ABIInfo::
3089GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003090 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00003091 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003092 // pass as float if the last 4 bytes is just padding. This happens for
3093 // structs that contain 3 floats.
3094 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
3095 SourceOffset*8+64, getContext()))
3096 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003097
Chris Lattnere556a712010-07-29 18:39:32 +00003098 // We want to pass as <2 x float> if the LLVM IR type contains a float at
3099 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
3100 // case.
Micah Villmowdd31ca12012-10-08 16:25:52 +00003101 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
3102 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner9f8b4512010-08-25 23:39:14 +00003103 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003104
Chris Lattner7f4b81a2010-07-29 18:13:09 +00003105 return llvm::Type::getDoubleTy(getVMContext());
3106}
3107
3108
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003109/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
3110/// an 8-byte GPR. This means that we either have a scalar or we are talking
3111/// about the high or low part of an up-to-16-byte struct. This routine picks
3112/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003113/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
3114/// etc).
3115///
3116/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
3117/// the source type. IROffset is an offset in bytes into the LLVM IR type that
3118/// the 8-byte value references. PrefType may be null.
3119///
Alp Toker9907f082014-07-09 14:06:35 +00003120/// SourceTy is the source-level type for the entire argument. SourceOffset is
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003121/// an offset into this that we're processing (which is always either 0 or 8).
3122///
Chris Lattnera5f58b02011-07-09 17:41:47 +00003123llvm::Type *X86_64ABIInfo::
3124GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003125 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003126 // If we're dealing with an un-offset LLVM IR type, then it means that we're
3127 // returning an 8-byte unit starting with it. See if we can safely use it.
3128 if (IROffset == 0) {
3129 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffc7dd7222012-10-11 15:52:22 +00003130 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
3131 IRType->isIntegerTy(64))
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003132 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003133
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003134 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
3135 // goodness in the source type is just tail padding. This is allowed to
3136 // kick in for struct {double,int} on the int, but not on
3137 // struct{double,int,int} because we wouldn't return the second int. We
3138 // have to do this analysis on the source type because we can't depend on
3139 // unions being lowered a specific way etc.
3140 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffc7dd7222012-10-11 15:52:22 +00003141 IRType->isIntegerTy(32) ||
3142 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
3143 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
3144 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003145
Chris Lattnerc8b7b532010-07-29 07:30:00 +00003146 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
3147 SourceOffset*8+64, getContext()))
3148 return IRType;
3149 }
3150 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003151
Chris Lattner2192fe52011-07-18 04:24:23 +00003152 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003153 // If this is a struct, recurse into the field at the specified offset.
Micah Villmowdd31ca12012-10-08 16:25:52 +00003154 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003155 if (IROffset < SL->getSizeInBytes()) {
3156 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
3157 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003158
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003159 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
3160 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003161 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003162 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003163
Chris Lattner2192fe52011-07-18 04:24:23 +00003164 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00003165 llvm::Type *EltTy = ATy->getElementType();
Micah Villmowdd31ca12012-10-08 16:25:52 +00003166 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner98076a22010-07-29 07:43:55 +00003167 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00003168 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
3169 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00003170 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003171
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003172 // Okay, we don't have any better idea of what to pass, so we pass this in an
3173 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00003174 unsigned TySizeInBytes =
3175 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003176
Chris Lattner3f763422010-07-29 17:34:39 +00003177 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003178
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003179 // It is always safe to classify this as an integer type up to i64 that
3180 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00003181 return llvm::IntegerType::get(getVMContext(),
3182 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00003183}
3184
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003185
3186/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
3187/// be used as elements of a two register pair to pass or return, return a
3188/// first class aggregate to represent them. For example, if the low part of
3189/// a by-value argument should be passed as i32* and the high part as float,
3190/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003191static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00003192GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmowdd31ca12012-10-08 16:25:52 +00003193 const llvm::DataLayout &TD) {
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003194 // In order to correctly satisfy the ABI, we need to the high part to start
3195 // at offset 8. If the high and low parts we inferred are both 4-byte types
3196 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
3197 // the second element at offset 8. Check for this:
3198 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
3199 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Rui Ueyama83aa9792016-01-14 21:00:27 +00003200 unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003201 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003202
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003203 // To handle this, we have to increase the size of the low part so that the
3204 // second element will start at an 8 byte offset. We can't increase the size
3205 // of the second element because it might make us access off the end of the
3206 // struct.
3207 if (HiStart != 8) {
Derek Schuff5ec51282015-06-24 22:36:38 +00003208 // There are usually two sorts of types the ABI generation code can produce
3209 // for the low part of a pair that aren't 8 bytes in size: float or
3210 // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and
3211 // NaCl).
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003212 // Promote these to a larger type.
3213 if (Lo->isFloatTy())
3214 Lo = llvm::Type::getDoubleTy(Lo->getContext());
3215 else {
Derek Schuff3c6a48d2015-06-24 22:36:36 +00003216 assert((Lo->isIntegerTy() || Lo->isPointerTy())
3217 && "Invalid/unknown lo type");
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003218 Lo = llvm::Type::getInt64Ty(Lo->getContext());
3219 }
3220 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003221
Serge Guelton1d993272017-05-09 19:31:30 +00003222 llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003223
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003224 // Verify that the second element is at an 8-byte offset.
3225 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3226 "Invalid x86-64 argument pair!");
3227 return Result;
3228}
3229
Chris Lattner31faff52010-07-28 23:06:14 +00003230ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00003231classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00003232 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3233 // classification algorithm.
3234 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00003235 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
Chris Lattner31faff52010-07-28 23:06:14 +00003236
3237 // Check some invariants.
3238 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00003239 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3240
Craig Topper8a13c412014-05-21 05:09:00 +00003241 llvm::Type *ResType = nullptr;
Chris Lattner31faff52010-07-28 23:06:14 +00003242 switch (Lo) {
3243 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003244 if (Hi == NoClass)
3245 return ABIArgInfo::getIgnore();
3246 // If the low part is just padding, it takes no register, leave ResType
3247 // null.
3248 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3249 "Unknown missing lo part");
3250 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003251
3252 case SSEUp:
3253 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00003254 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00003255
3256 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3257 // hidden argument.
3258 case Memory:
3259 return getIndirectReturnResult(RetTy);
3260
3261 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3262 // available register of the sequence %rax, %rdx is used.
3263 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003264 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003265
Chris Lattner1f3a0632010-07-29 21:42:50 +00003266 // If we have a sign or zero extended integer, make sure to return Extend
3267 // so that the parameter gets the right LLVM IR attributes.
3268 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3269 // Treat an enum type as its underlying type.
3270 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3271 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003272
Chris Lattner1f3a0632010-07-29 21:42:50 +00003273 if (RetTy->isIntegralOrEnumerationType() &&
3274 RetTy->isPromotableIntegerType())
3275 return ABIArgInfo::getExtend();
3276 }
Chris Lattner31faff52010-07-28 23:06:14 +00003277 break;
3278
3279 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3280 // available SSE register of the sequence %xmm0, %xmm1 is used.
3281 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003282 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00003283 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003284
3285 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3286 // returned on the X87 stack in %st0 as 80-bit x87 number.
3287 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00003288 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00003289 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003290
3291 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3292 // part of the value is returned in %st0 and the imaginary part in
3293 // %st1.
3294 case ComplexX87:
3295 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00003296 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Serge Guelton1d993272017-05-09 19:31:30 +00003297 llvm::Type::getX86_FP80Ty(getVMContext()));
Chris Lattner31faff52010-07-28 23:06:14 +00003298 break;
3299 }
3300
Craig Topper8a13c412014-05-21 05:09:00 +00003301 llvm::Type *HighPart = nullptr;
Chris Lattner31faff52010-07-28 23:06:14 +00003302 switch (Hi) {
3303 // Memory was handled previously and X87 should
3304 // never occur as a hi class.
3305 case Memory:
3306 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00003307 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00003308
3309 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00003310 case NoClass:
3311 break;
Chris Lattner31faff52010-07-28 23:06:14 +00003312
Chris Lattner52b3c132010-09-01 00:20:33 +00003313 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003314 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003315 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3316 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00003317 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00003318 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003319 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003320 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3321 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00003322 break;
3323
3324 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003325 // is passed in the next available eightbyte chunk if the last used
3326 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00003327 //
Chris Lattner57540c52011-04-15 05:22:18 +00003328 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00003329 case SSEUp:
3330 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003331 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00003332 break;
3333
3334 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3335 // returned together with the previous X87 value in %st0.
3336 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00003337 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00003338 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00003339 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00003340 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00003341 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00003342 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00003343 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
3344 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00003345 }
Chris Lattner31faff52010-07-28 23:06:14 +00003346 break;
3347 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003348
Chris Lattner52b3c132010-09-01 00:20:33 +00003349 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003350 // known to pass in the high eightbyte of the result. We do this by forming a
3351 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00003352 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00003353 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner31faff52010-07-28 23:06:14 +00003354
Chris Lattner1f3a0632010-07-29 21:42:50 +00003355 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00003356}
3357
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003358ABIArgInfo X86_64ABIInfo::classifyArgumentType(
Eli Friedman96fd2642013-06-12 00:13:45 +00003359 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
3360 bool isNamedArg)
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003361 const
3362{
Reid Klecknerb1be6832014-11-15 01:41:41 +00003363 Ty = useFirstFieldIfTransparentUnion(Ty);
3364
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003365 X86_64ABIInfo::Class Lo, Hi;
Eli Friedman96fd2642013-06-12 00:13:45 +00003366 classify(Ty, 0, Lo, Hi, isNamedArg);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003367
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003368 // Check some invariants.
3369 // FIXME: Enforce these by construction.
3370 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003371 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3372
3373 neededInt = 0;
3374 neededSSE = 0;
Craig Topper8a13c412014-05-21 05:09:00 +00003375 llvm::Type *ResType = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003376 switch (Lo) {
3377 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003378 if (Hi == NoClass)
3379 return ABIArgInfo::getIgnore();
3380 // If the low part is just padding, it takes no register, leave ResType
3381 // null.
3382 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3383 "Unknown missing lo part");
3384 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003385
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003386 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3387 // on the stack.
3388 case Memory:
3389
3390 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3391 // COMPLEX_X87, it is passed in memory.
3392 case X87:
3393 case ComplexX87:
Mark Lacey3825e832013-10-06 01:33:34 +00003394 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
Eli Friedman4774b7e2011-06-29 07:04:55 +00003395 ++neededInt;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00003396 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003397
3398 case SSEUp:
3399 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00003400 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003401
3402 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3403 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3404 // and %r9 is used.
3405 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00003406 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003407
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003408 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003409 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00003410
3411 // If we have a sign or zero extended integer, make sure to return Extend
3412 // so that the parameter gets the right LLVM IR attributes.
3413 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3414 // Treat an enum type as its underlying type.
3415 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3416 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003417
Chris Lattner1f3a0632010-07-29 21:42:50 +00003418 if (Ty->isIntegralOrEnumerationType() &&
3419 Ty->isPromotableIntegerType())
3420 return ABIArgInfo::getExtend();
3421 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003422
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003423 break;
3424
3425 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3426 // available SSE register is used, the registers are taken in the
3427 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00003428 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00003429 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00003430 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00003431 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003432 break;
3433 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00003434 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003435
Craig Topper8a13c412014-05-21 05:09:00 +00003436 llvm::Type *HighPart = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003437 switch (Hi) {
3438 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00003439 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003440 // which is passed in memory.
3441 case Memory:
3442 case X87:
3443 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00003444 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003445
3446 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003447
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003448 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003449 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00003450 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00003451 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003452
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003453 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3454 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003455 break;
3456
3457 // X87Up generally doesn't occur here (long double is passed in
3458 // memory), except in situations involving unions.
3459 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003460 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00003461 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003462
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003463 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
3464 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003465
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003466 ++neededSSE;
3467 break;
3468
3469 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3470 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003471 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003472 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00003473 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00003474 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003475 break;
3476 }
3477
Chris Lattnerbe5eb172010-09-01 00:24:35 +00003478 // If a high part was specified, merge it together with the low part. It is
3479 // known to pass in the high eightbyte of the result. We do this by forming a
3480 // first class struct aggregate with the high and low part: {low, high}
3481 if (HighPart)
Micah Villmowdd31ca12012-10-08 16:25:52 +00003482 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003483
Chris Lattner1f3a0632010-07-29 21:42:50 +00003484 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003485}
3486
Erich Keane757d3172016-11-02 18:29:35 +00003487ABIArgInfo
3488X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
3489 unsigned &NeededSSE) const {
3490 auto RT = Ty->getAs<RecordType>();
3491 assert(RT && "classifyRegCallStructType only valid with struct types");
3492
3493 if (RT->getDecl()->hasFlexibleArrayMember())
3494 return getIndirectReturnResult(Ty);
3495
3496 // Sum up bases
3497 if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
3498 if (CXXRD->isDynamicClass()) {
3499 NeededInt = NeededSSE = 0;
3500 return getIndirectReturnResult(Ty);
3501 }
3502
3503 for (const auto &I : CXXRD->bases())
3504 if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE)
3505 .isIndirect()) {
3506 NeededInt = NeededSSE = 0;
3507 return getIndirectReturnResult(Ty);
3508 }
3509 }
3510
3511 // Sum up members
3512 for (const auto *FD : RT->getDecl()->fields()) {
3513 if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) {
3514 if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE)
3515 .isIndirect()) {
3516 NeededInt = NeededSSE = 0;
3517 return getIndirectReturnResult(Ty);
3518 }
3519 } else {
3520 unsigned LocalNeededInt, LocalNeededSSE;
3521 if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt,
3522 LocalNeededSSE, true)
3523 .isIndirect()) {
3524 NeededInt = NeededSSE = 0;
3525 return getIndirectReturnResult(Ty);
3526 }
3527 NeededInt += LocalNeededInt;
3528 NeededSSE += LocalNeededSSE;
3529 }
3530 }
3531
3532 return ABIArgInfo::getDirect();
3533}
3534
3535ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty,
3536 unsigned &NeededInt,
3537 unsigned &NeededSSE) const {
3538
3539 NeededInt = 0;
3540 NeededSSE = 0;
3541
3542 return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE);
3543}
3544
Chris Lattner22326a12010-07-29 02:31:05 +00003545void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003546
Erich Keane757d3172016-11-02 18:29:35 +00003547 bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003548
3549 // Keep track of the number of assigned registers.
Erich Keane757d3172016-11-02 18:29:35 +00003550 unsigned FreeIntRegs = IsRegCall ? 11 : 6;
3551 unsigned FreeSSERegs = IsRegCall ? 16 : 8;
3552 unsigned NeededInt, NeededSSE;
3553
Erich Keanede1b2a92017-07-21 18:50:36 +00003554 if (!getCXXABI().classifyReturnType(FI)) {
3555 if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
3556 !FI.getReturnType()->getTypePtr()->isUnionType()) {
3557 FI.getReturnInfo() =
3558 classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE);
3559 if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3560 FreeIntRegs -= NeededInt;
3561 FreeSSERegs -= NeededSSE;
3562 } else {
3563 FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3564 }
3565 } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) {
3566 // Complex Long Double Type is passed in Memory when Regcall
3567 // calling convention is used.
3568 const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>();
3569 if (getContext().getCanonicalType(CT->getElementType()) ==
3570 getContext().LongDoubleTy)
3571 FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3572 } else
3573 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3574 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003575
3576 // If the return value is indirect, then the hidden argument is consuming one
3577 // integer register.
3578 if (FI.getReturnInfo().isIndirect())
Erich Keane757d3172016-11-02 18:29:35 +00003579 --FreeIntRegs;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003580
Peter Collingbournef7706832014-12-12 23:41:25 +00003581 // The chain argument effectively gives us another free register.
3582 if (FI.isChainCall())
Erich Keane757d3172016-11-02 18:29:35 +00003583 ++FreeIntRegs;
Peter Collingbournef7706832014-12-12 23:41:25 +00003584
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003585 unsigned NumRequiredArgs = FI.getNumRequiredArgs();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003586 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3587 // get assigned (in left-to-right order) for passing as follows...
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003588 unsigned ArgNo = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003589 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Alexey Samsonov34625dd2014-09-29 21:21:48 +00003590 it != ie; ++it, ++ArgNo) {
3591 bool IsNamedArg = ArgNo < NumRequiredArgs;
Eli Friedman96fd2642013-06-12 00:13:45 +00003592
Erich Keane757d3172016-11-02 18:29:35 +00003593 if (IsRegCall && it->type->isStructureOrClassType())
3594 it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE);
3595 else
3596 it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
3597 NeededSSE, IsNamedArg);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003598
3599 // AMD64-ABI 3.2.3p3: If there are no registers available for any
3600 // eightbyte of an argument, the whole argument is passed on the
3601 // stack. If registers have already been assigned for some
3602 // eightbytes of such an argument, the assignments get reverted.
Erich Keane757d3172016-11-02 18:29:35 +00003603 if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3604 FreeIntRegs -= NeededInt;
3605 FreeSSERegs -= NeededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003606 } else {
Erich Keane757d3172016-11-02 18:29:35 +00003607 it->info = getIndirectResult(it->type, FreeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003608 }
3609 }
3610}
3611
John McCall7f416cc2015-09-08 08:05:57 +00003612static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3613 Address VAListAddr, QualType Ty) {
3614 Address overflow_arg_area_p = CGF.Builder.CreateStructGEP(
3615 VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003616 llvm::Value *overflow_arg_area =
3617 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3618
3619 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3620 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedmana1748562011-11-18 02:44:19 +00003621 // It isn't stated explicitly in the standard, but in practice we use
3622 // alignment greater than 16 where necessary.
Petar Jovanovic402257b2015-12-04 00:26:47 +00003623 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3624 if (Align > CharUnits::fromQuantity(8)) {
3625 overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
3626 Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003627 }
3628
3629 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00003630 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003631 llvm::Value *Res =
3632 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00003633 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003634
3635 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
3636 // l->overflow_arg_area + sizeof(type).
3637 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
3638 // an 8 byte boundary.
3639
3640 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00003641 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00003642 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003643 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
3644 "overflow_arg_area.next");
3645 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
3646
3647 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
Petar Jovanovic402257b2015-12-04 00:26:47 +00003648 return Address(Res, Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003649}
3650
John McCall7f416cc2015-09-08 08:05:57 +00003651Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3652 QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003653 // Assume that va_list type is correct; should be pointer to LLVM type:
3654 // struct {
3655 // i32 gp_offset;
3656 // i32 fp_offset;
3657 // i8* overflow_arg_area;
3658 // i8* reg_save_area;
3659 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00003660 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003661
John McCall7f416cc2015-09-08 08:05:57 +00003662 Ty = getContext().getCanonicalType(Ty);
Eric Christopher7565e0d2015-05-29 23:09:49 +00003663 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
Eli Friedman96fd2642013-06-12 00:13:45 +00003664 /*isNamedArg*/false);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003665
3666 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
3667 // in the registers. If not go to step 7.
3668 if (!neededInt && !neededSSE)
John McCall7f416cc2015-09-08 08:05:57 +00003669 return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003670
3671 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
3672 // general purpose registers needed to pass type and num_fp to hold
3673 // the number of floating point registers needed.
3674
3675 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
3676 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
3677 // l->fp_offset > 304 - num_fp * 16 go to step 7.
3678 //
3679 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
3680 // register save space).
3681
Craig Topper8a13c412014-05-21 05:09:00 +00003682 llvm::Value *InRegs = nullptr;
John McCall7f416cc2015-09-08 08:05:57 +00003683 Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
3684 llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003685 if (neededInt) {
David Blaikie1ed728c2015-04-05 22:45:47 +00003686 gp_offset_p =
John McCall7f416cc2015-09-08 08:05:57 +00003687 CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(),
3688 "gp_offset_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003689 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00003690 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
3691 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003692 }
3693
3694 if (neededSSE) {
David Blaikie1ed728c2015-04-05 22:45:47 +00003695 fp_offset_p =
John McCall7f416cc2015-09-08 08:05:57 +00003696 CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4),
3697 "fp_offset_p");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003698 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
3699 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00003700 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
3701 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003702 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
3703 }
3704
3705 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3706 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
3707 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3708 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
3709
3710 // Emit code to load the value if it was passed in registers.
3711
3712 CGF.EmitBlock(InRegBlock);
3713
3714 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
3715 // an offset of l->gp_offset and/or l->fp_offset. This may require
3716 // copying to a temporary location in case the parameter is passed
3717 // in different register classes or requires an alignment greater
3718 // than 8 for general purpose registers and 16 for XMM registers.
3719 //
3720 // FIXME: This really results in shameful code when we end up needing to
3721 // collect arguments from different places; often what should result in a
3722 // simple assembling of a structure from scattered addresses has many more
3723 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00003724 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00003725 llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
3726 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)),
3727 "reg_save_area");
3728
3729 Address RegAddr = Address::invalid();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003730 if (neededInt && neededSSE) {
3731 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003732 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003733 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
John McCall7f416cc2015-09-08 08:05:57 +00003734 Address Tmp = CGF.CreateMemTemp(Ty);
3735 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003736 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003737 llvm::Type *TyLo = ST->getElementType(0);
3738 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00003739 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003740 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00003741 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
3742 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
John McCall7f416cc2015-09-08 08:05:57 +00003743 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
3744 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
Rafael Espindola0a500af2014-06-24 20:01:50 +00003745 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
3746 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003747
John McCall7f416cc2015-09-08 08:05:57 +00003748 // Copy the first element.
Peter Collingbourneb367c562016-11-28 22:30:21 +00003749 // FIXME: Our choice of alignment here and below is probably pessimistic.
3750 llvm::Value *V = CGF.Builder.CreateAlignedLoad(
3751 TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
3752 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo)));
John McCall7f416cc2015-09-08 08:05:57 +00003753 CGF.Builder.CreateStore(V,
3754 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3755
3756 // Copy the second element.
Peter Collingbourneb367c562016-11-28 22:30:21 +00003757 V = CGF.Builder.CreateAlignedLoad(
3758 TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
3759 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi)));
John McCall7f416cc2015-09-08 08:05:57 +00003760 CharUnits Offset = CharUnits::fromQuantity(
3761 getDataLayout().getStructLayout(ST)->getElementOffset(1));
3762 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset));
3763
3764 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003765 } else if (neededInt) {
John McCall7f416cc2015-09-08 08:05:57 +00003766 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
3767 CharUnits::fromQuantity(8));
3768 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003769
3770 // Copy to a temporary if necessary to ensure the appropriate alignment.
3771 std::pair<CharUnits, CharUnits> SizeAlign =
John McCall7f416cc2015-09-08 08:05:57 +00003772 getContext().getTypeInfoInChars(Ty);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003773 uint64_t TySize = SizeAlign.first.getQuantity();
John McCall7f416cc2015-09-08 08:05:57 +00003774 CharUnits TyAlign = SizeAlign.second;
3775
3776 // Copy into a temporary if the type is more aligned than the
3777 // register save area.
3778 if (TyAlign.getQuantity() > 8) {
3779 Address Tmp = CGF.CreateMemTemp(Ty);
3780 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
Eli Friedmanc11c1692013-06-07 23:20:55 +00003781 RegAddr = Tmp;
3782 }
John McCall7f416cc2015-09-08 08:05:57 +00003783
Chris Lattner0cf24192010-06-28 20:05:43 +00003784 } else if (neededSSE == 1) {
John McCall7f416cc2015-09-08 08:05:57 +00003785 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3786 CharUnits::fromQuantity(16));
3787 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003788 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00003789 assert(neededSSE == 2 && "Invalid number of needed registers!");
3790 // SSE registers are spaced 16 bytes apart in the register save
3791 // area, we need to collect the two eightbytes together.
John McCall7f416cc2015-09-08 08:05:57 +00003792 // The ABI isn't explicit about this, but it seems reasonable
3793 // to assume that the slots are 16-byte aligned, since the stack is
3794 // naturally 16-byte aligned and the prologue is expected to store
3795 // all the SSE registers to the RSA.
3796 Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3797 CharUnits::fromQuantity(16));
3798 Address RegAddrHi =
3799 CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
3800 CharUnits::fromQuantity(16));
Chris Lattnerece04092012-02-07 00:39:47 +00003801 llvm::Type *DoubleTy = CGF.DoubleTy;
Serge Guelton1d993272017-05-09 19:31:30 +00003802 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy);
John McCall7f416cc2015-09-08 08:05:57 +00003803 llvm::Value *V;
3804 Address Tmp = CGF.CreateMemTemp(Ty);
3805 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
3806 V = CGF.Builder.CreateLoad(
3807 CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy));
3808 CGF.Builder.CreateStore(V,
3809 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3810 V = CGF.Builder.CreateLoad(
3811 CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy));
3812 CGF.Builder.CreateStore(V,
3813 CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8)));
3814
3815 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003816 }
3817
3818 // AMD64-ABI 3.5.7p5: Step 5. Set:
3819 // l->gp_offset = l->gp_offset + num_gp * 8
3820 // l->fp_offset = l->fp_offset + num_fp * 16.
3821 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003822 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003823 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
3824 gp_offset_p);
3825 }
3826 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003827 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003828 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
3829 fp_offset_p);
3830 }
3831 CGF.EmitBranch(ContBlock);
3832
3833 // Emit code to load the value if it was passed in memory.
3834
3835 CGF.EmitBlock(InMemBlock);
John McCall7f416cc2015-09-08 08:05:57 +00003836 Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003837
3838 // Return the appropriate result.
3839
3840 CGF.EmitBlock(ContBlock);
John McCall7f416cc2015-09-08 08:05:57 +00003841 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
3842 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003843 return ResAddr;
3844}
3845
Charles Davisc7d5c942015-09-17 20:55:33 +00003846Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
3847 QualType Ty) const {
3848 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3849 CGF.getContext().getTypeInfoInChars(Ty),
3850 CharUnits::fromQuantity(8),
3851 /*allowHigherAlign*/ false);
3852}
3853
Erich Keane521ed962017-01-05 00:20:51 +00003854ABIArgInfo
3855WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
3856 const ABIArgInfo &current) const {
3857 // Assumes vectorCall calling convention.
3858 const Type *Base = nullptr;
3859 uint64_t NumElts = 0;
3860
3861 if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
3862 isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
3863 FreeSSERegs -= NumElts;
3864 return getDirectX86Hva();
3865 }
3866 return current;
3867}
3868
Reid Kleckner80944df2014-10-31 22:00:51 +00003869ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
Erich Keane521ed962017-01-05 00:20:51 +00003870 bool IsReturnType, bool IsVectorCall,
3871 bool IsRegCall) const {
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003872
3873 if (Ty->isVoidType())
3874 return ABIArgInfo::getIgnore();
3875
3876 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3877 Ty = EnumTy->getDecl()->getIntegerType();
3878
Reid Kleckner80944df2014-10-31 22:00:51 +00003879 TypeInfo Info = getContext().getTypeInfo(Ty);
3880 uint64_t Width = Info.Width;
Reid Kleckner11a17192015-10-28 22:29:52 +00003881 CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003882
Reid Kleckner9005f412014-05-02 00:51:20 +00003883 const RecordType *RT = Ty->getAs<RecordType>();
3884 if (RT) {
Reid Kleckner40ca9132014-05-13 22:05:45 +00003885 if (!IsReturnType) {
Mark Lacey3825e832013-10-06 01:33:34 +00003886 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00003887 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Timur Iskhodzhanov8fe501d2013-04-17 12:54:10 +00003888 }
3889
3890 if (RT->getDecl()->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00003891 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003892
Reid Kleckner9005f412014-05-02 00:51:20 +00003893 }
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00003894
Reid Kleckner80944df2014-10-31 22:00:51 +00003895 const Type *Base = nullptr;
3896 uint64_t NumElts = 0;
Erich Keane521ed962017-01-05 00:20:51 +00003897 // vectorcall adds the concept of a homogenous vector aggregate, similar to
3898 // other targets.
3899 if ((IsVectorCall || IsRegCall) &&
3900 isHomogeneousAggregate(Ty, Base, NumElts)) {
3901 if (IsRegCall) {
3902 if (FreeSSERegs >= NumElts) {
3903 FreeSSERegs -= NumElts;
3904 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
3905 return ABIArgInfo::getDirect();
3906 return ABIArgInfo::getExpand();
3907 }
3908 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3909 } else if (IsVectorCall) {
3910 if (FreeSSERegs >= NumElts &&
3911 (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
3912 FreeSSERegs -= NumElts;
Reid Kleckner80944df2014-10-31 22:00:51 +00003913 return ABIArgInfo::getDirect();
Erich Keane521ed962017-01-05 00:20:51 +00003914 } else if (IsReturnType) {
3915 return ABIArgInfo::getExpand();
3916 } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
3917 // HVAs are delayed and reclassified in the 2nd step.
3918 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3919 }
Reid Kleckner80944df2014-10-31 22:00:51 +00003920 }
Reid Kleckner80944df2014-10-31 22:00:51 +00003921 }
3922
Reid Klecknerec87fec2014-05-02 01:17:12 +00003923 if (Ty->isMemberPointerType()) {
Reid Kleckner7f5f0f32014-05-02 01:14:59 +00003924 // If the member pointer is represented by an LLVM int or ptr, pass it
3925 // directly.
3926 llvm::Type *LLTy = CGT.ConvertType(Ty);
3927 if (LLTy->isPointerTy() || LLTy->isIntegerTy())
3928 return ABIArgInfo::getDirect();
Reid Kleckner9005f412014-05-02 00:51:20 +00003929 }
3930
Michael Kuperstein4f818702015-02-24 09:35:58 +00003931 if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00003932 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
3933 // not 1, 2, 4, or 8 bytes, must be passed by reference."
Reid Kleckner80944df2014-10-31 22:00:51 +00003934 if (Width > 64 || !llvm::isPowerOf2_64(Width))
John McCall7f416cc2015-09-08 08:05:57 +00003935 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003936
Reid Kleckner9005f412014-05-02 00:51:20 +00003937 // Otherwise, coerce it to a small integer.
Reid Kleckner80944df2014-10-31 22:00:51 +00003938 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003939 }
3940
Julien Lerouge10dcff82014-08-27 00:36:55 +00003941 // Bool type is always extended to the ABI, other builtin types are not
3942 // extended.
3943 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3944 if (BT && BT->getKind() == BuiltinType::Bool)
Julien Lerougee8d34fa2014-08-26 22:11:53 +00003945 return ABIArgInfo::getExtend();
3946
Reid Kleckner11a17192015-10-28 22:29:52 +00003947 // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It
3948 // passes them indirectly through memory.
3949 if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) {
3950 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
Stephan Bergmann17c7f702016-12-14 11:57:17 +00003951 if (LDF == &llvm::APFloat::x87DoubleExtended())
Reid Kleckner11a17192015-10-28 22:29:52 +00003952 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3953 }
3954
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003955 return ABIArgInfo::getDirect();
3956}
3957
Erich Keane521ed962017-01-05 00:20:51 +00003958void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI,
3959 unsigned FreeSSERegs,
3960 bool IsVectorCall,
3961 bool IsRegCall) const {
3962 unsigned Count = 0;
3963 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00003964 // Vectorcall in x64 only permits the first 6 arguments to be passed
3965 // as XMM/YMM registers.
Erich Keane521ed962017-01-05 00:20:51 +00003966 if (Count < VectorcallMaxParamNumAsReg)
3967 I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
3968 else {
3969 // Since these cannot be passed in registers, pretend no registers
3970 // are left.
3971 unsigned ZeroSSERegsAvail = 0;
3972 I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false,
3973 IsVectorCall, IsRegCall);
3974 }
3975 ++Count;
3976 }
3977
Erich Keane521ed962017-01-05 00:20:51 +00003978 for (auto &I : FI.arguments()) {
Erich Keane4bd39302017-06-21 16:37:22 +00003979 I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info);
Erich Keane521ed962017-01-05 00:20:51 +00003980 }
3981}
3982
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00003983void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner80944df2014-10-31 22:00:51 +00003984 bool IsVectorCall =
3985 FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall;
Erich Keane757d3172016-11-02 18:29:35 +00003986 bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall;
Reid Kleckner37abaca2014-05-09 22:46:15 +00003987
Erich Keane757d3172016-11-02 18:29:35 +00003988 unsigned FreeSSERegs = 0;
3989 if (IsVectorCall) {
3990 // We can use up to 4 SSE return registers with vectorcall.
3991 FreeSSERegs = 4;
3992 } else if (IsRegCall) {
3993 // RegCall gives us 16 SSE registers.
3994 FreeSSERegs = 16;
3995 }
3996
Reid Kleckner80944df2014-10-31 22:00:51 +00003997 if (!getCXXABI().classifyReturnType(FI))
Erich Keane521ed962017-01-05 00:20:51 +00003998 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
3999 IsVectorCall, IsRegCall);
Reid Kleckner80944df2014-10-31 22:00:51 +00004000
Erich Keane757d3172016-11-02 18:29:35 +00004001 if (IsVectorCall) {
4002 // We can use up to 6 SSE register parameters with vectorcall.
4003 FreeSSERegs = 6;
4004 } else if (IsRegCall) {
Erich Keane521ed962017-01-05 00:20:51 +00004005 // RegCall gives us 16 SSE registers, we can reuse the return registers.
Erich Keane757d3172016-11-02 18:29:35 +00004006 FreeSSERegs = 16;
4007 }
4008
Erich Keane521ed962017-01-05 00:20:51 +00004009 if (IsVectorCall) {
4010 computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall);
4011 } else {
4012 for (auto &I : FI.arguments())
4013 I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
4014 }
4015
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00004016}
4017
John McCall7f416cc2015-09-08 08:05:57 +00004018Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4019 QualType Ty) const {
Reid Klecknerb04449d2016-08-25 20:42:26 +00004020
4021 bool IsIndirect = false;
4022
4023 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4024 // not 1, 2, 4, or 8 bytes, must be passed by reference."
4025 if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) {
4026 uint64_t Width = getContext().getTypeSize(Ty);
4027 IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4028 }
4029
4030 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
John McCall7f416cc2015-09-08 08:05:57 +00004031 CGF.getContext().getTypeInfoInChars(Ty),
4032 CharUnits::fromQuantity(8),
4033 /*allowHigherAlign*/ false);
Chris Lattner04dc9572010-08-31 16:44:54 +00004034}
Chris Lattner0cf24192010-06-28 20:05:43 +00004035
John McCallea8d8bb2010-03-11 00:10:12 +00004036// PowerPC-32
John McCallea8d8bb2010-03-11 00:10:12 +00004037namespace {
Roman Divacky8a12d842014-11-03 18:32:54 +00004038/// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
4039class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004040bool IsSoftFloatABI;
John McCallea8d8bb2010-03-11 00:10:12 +00004041public:
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004042 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI)
4043 : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {}
Roman Divacky8a12d842014-11-03 18:32:54 +00004044
John McCall7f416cc2015-09-08 08:05:57 +00004045 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4046 QualType Ty) const override;
Roman Divacky8a12d842014-11-03 18:32:54 +00004047};
4048
4049class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
4050public:
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004051 PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI)
4052 : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00004053
Craig Topper4f12f102014-03-12 06:41:41 +00004054 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallea8d8bb2010-03-11 00:10:12 +00004055 // This is recovered from gcc output.
4056 return 1; // r1 is the dedicated stack pointer
4057 }
4058
4059 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00004060 llvm::Value *Address) const override;
John McCallea8d8bb2010-03-11 00:10:12 +00004061};
4062
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004063}
John McCallea8d8bb2010-03-11 00:10:12 +00004064
James Y Knight29b5f082016-02-24 02:59:33 +00004065// TODO: this implementation is now likely redundant with
4066// DefaultABIInfo::EmitVAArg.
John McCall7f416cc2015-09-08 08:05:57 +00004067Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
4068 QualType Ty) const {
Roman Divacky039b9702016-02-20 08:31:24 +00004069 const unsigned OverflowLimit = 8;
Roman Divacky8a12d842014-11-03 18:32:54 +00004070 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4071 // TODO: Implement this. For now ignore.
4072 (void)CTy;
James Y Knight29b5f082016-02-24 02:59:33 +00004073 return Address::invalid(); // FIXME?
Roman Divacky8a12d842014-11-03 18:32:54 +00004074 }
4075
John McCall7f416cc2015-09-08 08:05:57 +00004076 // struct __va_list_tag {
4077 // unsigned char gpr;
4078 // unsigned char fpr;
4079 // unsigned short reserved;
4080 // void *overflow_arg_area;
4081 // void *reg_save_area;
4082 // };
4083
Roman Divacky8a12d842014-11-03 18:32:54 +00004084 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
Eric Christopher7565e0d2015-05-29 23:09:49 +00004085 bool isInt =
4086 Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004087 bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
John McCall7f416cc2015-09-08 08:05:57 +00004088
4089 // All aggregates are passed indirectly? That doesn't seem consistent
4090 // with the argument-lowering code.
4091 bool isIndirect = Ty->isAggregateType();
Roman Divacky8a12d842014-11-03 18:32:54 +00004092
4093 CGBuilderTy &Builder = CGF.Builder;
John McCall7f416cc2015-09-08 08:05:57 +00004094
4095 // The calling convention either uses 1-2 GPRs or 1 FPR.
4096 Address NumRegsAddr = Address::invalid();
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004097 if (isInt || IsSoftFloatABI) {
John McCall7f416cc2015-09-08 08:05:57 +00004098 NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr");
4099 } else {
4100 NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr");
Roman Divacky8a12d842014-11-03 18:32:54 +00004101 }
John McCall7f416cc2015-09-08 08:05:57 +00004102
4103 llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
4104
4105 // "Align" the register count when TY is i64.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004106 if (isI64 || (isF64 && IsSoftFloatABI)) {
John McCall7f416cc2015-09-08 08:05:57 +00004107 NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
4108 NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
4109 }
Roman Divacky8a12d842014-11-03 18:32:54 +00004110
Eric Christopher7565e0d2015-05-29 23:09:49 +00004111 llvm::Value *CC =
Roman Divacky039b9702016-02-20 08:31:24 +00004112 Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
Roman Divacky8a12d842014-11-03 18:32:54 +00004113
4114 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
4115 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
4116 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
4117
4118 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
4119
John McCall7f416cc2015-09-08 08:05:57 +00004120 llvm::Type *DirectTy = CGF.ConvertType(Ty);
4121 if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
Roman Divacky8a12d842014-11-03 18:32:54 +00004122
John McCall7f416cc2015-09-08 08:05:57 +00004123 // Case 1: consume registers.
4124 Address RegAddr = Address::invalid();
4125 {
4126 CGF.EmitBlock(UsingRegs);
4127
4128 Address RegSaveAreaPtr =
4129 Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8));
4130 RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
4131 CharUnits::fromQuantity(8));
4132 assert(RegAddr.getElementType() == CGF.Int8Ty);
4133
4134 // Floating-point registers start after the general-purpose registers.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004135 if (!(isInt || IsSoftFloatABI)) {
John McCall7f416cc2015-09-08 08:05:57 +00004136 RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
4137 CharUnits::fromQuantity(32));
4138 }
4139
4140 // Get the address of the saved value by scaling the number of
4141 // registers we've used by the number of
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004142 CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
John McCall7f416cc2015-09-08 08:05:57 +00004143 llvm::Value *RegOffset =
4144 Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
4145 RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
4146 RegAddr.getPointer(), RegOffset),
4147 RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
4148 RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
4149
4150 // Increase the used-register count.
Petar Jovanovic88a328f2015-12-14 17:51:50 +00004151 NumRegs =
4152 Builder.CreateAdd(NumRegs,
4153 Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
John McCall7f416cc2015-09-08 08:05:57 +00004154 Builder.CreateStore(NumRegs, NumRegsAddr);
4155
4156 CGF.EmitBranch(Cont);
Roman Divacky8a12d842014-11-03 18:32:54 +00004157 }
Roman Divacky8a12d842014-11-03 18:32:54 +00004158
John McCall7f416cc2015-09-08 08:05:57 +00004159 // Case 2: consume space in the overflow area.
4160 Address MemAddr = Address::invalid();
4161 {
4162 CGF.EmitBlock(UsingOverflow);
Roman Divacky8a12d842014-11-03 18:32:54 +00004163
Roman Divacky039b9702016-02-20 08:31:24 +00004164 Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
4165
John McCall7f416cc2015-09-08 08:05:57 +00004166 // Everything in the overflow area is rounded up to a size of at least 4.
4167 CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
4168
4169 CharUnits Size;
4170 if (!isIndirect) {
4171 auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
Rui Ueyama83aa9792016-01-14 21:00:27 +00004172 Size = TypeInfo.first.alignTo(OverflowAreaAlign);
John McCall7f416cc2015-09-08 08:05:57 +00004173 } else {
4174 Size = CGF.getPointerSize();
4175 }
4176
4177 Address OverflowAreaAddr =
4178 Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4));
Petar Jovanovic402257b2015-12-04 00:26:47 +00004179 Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
John McCall7f416cc2015-09-08 08:05:57 +00004180 OverflowAreaAlign);
Petar Jovanovic402257b2015-12-04 00:26:47 +00004181 // Round up address of argument to alignment
4182 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4183 if (Align > OverflowAreaAlign) {
4184 llvm::Value *Ptr = OverflowArea.getPointer();
4185 OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
4186 Align);
4187 }
4188
John McCall7f416cc2015-09-08 08:05:57 +00004189 MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
4190
4191 // Increase the overflow area.
4192 OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
4193 Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
4194 CGF.EmitBranch(Cont);
4195 }
Roman Divacky8a12d842014-11-03 18:32:54 +00004196
4197 CGF.EmitBlock(Cont);
4198
John McCall7f416cc2015-09-08 08:05:57 +00004199 // Merge the cases with a phi.
4200 Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
4201 "vaarg.addr");
Roman Divacky8a12d842014-11-03 18:32:54 +00004202
John McCall7f416cc2015-09-08 08:05:57 +00004203 // Load the pointer if the argument was passed indirectly.
4204 if (isIndirect) {
4205 Result = Address(Builder.CreateLoad(Result, "aggr"),
4206 getContext().getTypeAlignInChars(Ty));
Roman Divacky8a12d842014-11-03 18:32:54 +00004207 }
4208
4209 return Result;
4210}
4211
John McCallea8d8bb2010-03-11 00:10:12 +00004212bool
4213PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4214 llvm::Value *Address) const {
4215 // This is calculated from the LLVM and GCC tables and verified
4216 // against gcc output. AFAIK all ABIs use the same encoding.
4217
4218 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallea8d8bb2010-03-11 00:10:12 +00004219
Chris Lattnerece04092012-02-07 00:39:47 +00004220 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallea8d8bb2010-03-11 00:10:12 +00004221 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4222 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4223 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4224
4225 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00004226 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00004227
4228 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00004229 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00004230
4231 // 64-76 are various 4-byte special-purpose registers:
4232 // 64: mq
4233 // 65: lr
4234 // 66: ctr
4235 // 67: ap
4236 // 68-75 cr0-7
4237 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00004238 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00004239
4240 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00004241 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00004242
4243 // 109: vrsave
4244 // 110: vscr
4245 // 111: spe_acc
4246 // 112: spefscr
4247 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00004248 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00004249
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00004250 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00004251}
4252
Roman Divackyd966e722012-05-09 18:22:46 +00004253// PowerPC-64
4254
4255namespace {
Bill Schmidt25cb3492012-10-03 19:18:57 +00004256/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
James Y Knight29b5f082016-02-24 02:59:33 +00004257class PPC64_SVR4_ABIInfo : public ABIInfo {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004258public:
4259 enum ABIKind {
4260 ELFv1 = 0,
4261 ELFv2
4262 };
4263
4264private:
4265 static const unsigned GPRBits = 64;
4266 ABIKind Kind;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004267 bool HasQPX;
Hal Finkel415c2a32016-10-02 02:10:45 +00004268 bool IsSoftFloatABI;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004269
4270 // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
4271 // will be passed in a QPX register.
4272 bool IsQPXVectorTy(const Type *Ty) const {
4273 if (!HasQPX)
4274 return false;
4275
4276 if (const VectorType *VT = Ty->getAs<VectorType>()) {
4277 unsigned NumElements = VT->getNumElements();
4278 if (NumElements == 1)
4279 return false;
4280
4281 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
4282 if (getContext().getTypeSize(Ty) <= 256)
4283 return true;
4284 } else if (VT->getElementType()->
4285 isSpecificBuiltinType(BuiltinType::Float)) {
4286 if (getContext().getTypeSize(Ty) <= 128)
4287 return true;
4288 }
4289 }
4290
4291 return false;
4292 }
4293
4294 bool IsQPXVectorTy(QualType Ty) const {
4295 return IsQPXVectorTy(Ty.getTypePtr());
4296 }
Bill Schmidt25cb3492012-10-03 19:18:57 +00004297
4298public:
Hal Finkel415c2a32016-10-02 02:10:45 +00004299 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX,
4300 bool SoftFloatABI)
4301 : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX),
4302 IsSoftFloatABI(SoftFloatABI) {}
Bill Schmidt25cb3492012-10-03 19:18:57 +00004303
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004304 bool isPromotableTypeForABI(QualType Ty) const;
John McCall7f416cc2015-09-08 08:05:57 +00004305 CharUnits getParamTypeAlignment(QualType Ty) const;
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004306
4307 ABIArgInfo classifyReturnType(QualType RetTy) const;
4308 ABIArgInfo classifyArgumentType(QualType Ty) const;
4309
Reid Klecknere9f6a712014-10-31 17:10:41 +00004310 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4311 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4312 uint64_t Members) const override;
4313
Bill Schmidt84d37792012-10-12 19:26:17 +00004314 // TODO: We can add more logic to computeInfo to improve performance.
4315 // Example: For aggregate arguments that fit in a register, we could
4316 // use getDirectInReg (as is done below for structs containing a single
4317 // floating-point value) to avoid pushing them to memory on function
4318 // entry. This would require changing the logic in PPCISelLowering
4319 // when lowering the parameters in the caller and args in the callee.
Craig Topper4f12f102014-03-12 06:41:41 +00004320 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00004321 if (!getCXXABI().classifyReturnType(FI))
4322 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004323 for (auto &I : FI.arguments()) {
Bill Schmidt84d37792012-10-12 19:26:17 +00004324 // We rely on the default argument classification for the most part.
4325 // One exception: An aggregate containing a single floating-point
Bill Schmidt179afae2013-07-23 22:15:57 +00004326 // or vector item must be passed in a register if one is available.
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004327 const Type *T = isSingleElementStruct(I.type, getContext());
Bill Schmidt84d37792012-10-12 19:26:17 +00004328 if (T) {
4329 const BuiltinType *BT = T->getAs<BuiltinType>();
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004330 if (IsQPXVectorTy(T) ||
4331 (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004332 (BT && BT->isFloatingPoint())) {
Bill Schmidt84d37792012-10-12 19:26:17 +00004333 QualType QT(T, 0);
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004334 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
Bill Schmidt84d37792012-10-12 19:26:17 +00004335 continue;
4336 }
4337 }
Aaron Ballmanec47bc22014-03-17 18:10:01 +00004338 I.info = classifyArgumentType(I.type);
Bill Schmidt84d37792012-10-12 19:26:17 +00004339 }
4340 }
Bill Schmidt25cb3492012-10-03 19:18:57 +00004341
John McCall7f416cc2015-09-08 08:05:57 +00004342 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4343 QualType Ty) const override;
Bill Schmidt25cb3492012-10-03 19:18:57 +00004344};
4345
4346class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004347
Bill Schmidt25cb3492012-10-03 19:18:57 +00004348public:
Ulrich Weigandb7122372014-07-21 00:48:09 +00004349 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
Hal Finkel415c2a32016-10-02 02:10:45 +00004350 PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX,
4351 bool SoftFloatABI)
4352 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX,
4353 SoftFloatABI)) {}
Bill Schmidt25cb3492012-10-03 19:18:57 +00004354
Craig Topper4f12f102014-03-12 06:41:41 +00004355 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Bill Schmidt25cb3492012-10-03 19:18:57 +00004356 // This is recovered from gcc output.
4357 return 1; // r1 is the dedicated stack pointer
4358 }
4359
4360 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00004361 llvm::Value *Address) const override;
Bill Schmidt25cb3492012-10-03 19:18:57 +00004362};
4363
Roman Divackyd966e722012-05-09 18:22:46 +00004364class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4365public:
4366 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
4367
Craig Topper4f12f102014-03-12 06:41:41 +00004368 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divackyd966e722012-05-09 18:22:46 +00004369 // This is recovered from gcc output.
4370 return 1; // r1 is the dedicated stack pointer
4371 }
4372
4373 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00004374 llvm::Value *Address) const override;
Roman Divackyd966e722012-05-09 18:22:46 +00004375};
4376
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004377}
Roman Divackyd966e722012-05-09 18:22:46 +00004378
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004379// Return true if the ABI requires Ty to be passed sign- or zero-
4380// extended to 64 bits.
4381bool
4382PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
4383 // Treat an enum type as its underlying type.
4384 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4385 Ty = EnumTy->getDecl()->getIntegerType();
4386
4387 // Promotable integer types are required to be promoted by the ABI.
4388 if (Ty->isPromotableIntegerType())
4389 return true;
4390
4391 // In addition to the usual promotable integer types, we also need to
4392 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
4393 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4394 switch (BT->getKind()) {
4395 case BuiltinType::Int:
4396 case BuiltinType::UInt:
4397 return true;
4398 default:
4399 break;
4400 }
4401
4402 return false;
4403}
4404
John McCall7f416cc2015-09-08 08:05:57 +00004405/// isAlignedParamType - Determine whether a type requires 16-byte or
4406/// higher alignment in the parameter area. Always returns at least 8.
4407CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
Ulrich Weigand581badc2014-07-10 17:20:07 +00004408 // Complex types are passed just like their elements.
4409 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4410 Ty = CTy->getElementType();
4411
4412 // Only vector types of size 16 bytes need alignment (larger types are
4413 // passed via reference, smaller types are not aligned).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004414 if (IsQPXVectorTy(Ty)) {
4415 if (getContext().getTypeSize(Ty) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004416 return CharUnits::fromQuantity(32);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004417
John McCall7f416cc2015-09-08 08:05:57 +00004418 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004419 } else if (Ty->isVectorType()) {
John McCall7f416cc2015-09-08 08:05:57 +00004420 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004421 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00004422
4423 // For single-element float/vector structs, we consider the whole type
4424 // to have the same alignment requirements as its single element.
4425 const Type *AlignAsType = nullptr;
4426 const Type *EltType = isSingleElementStruct(Ty, getContext());
4427 if (EltType) {
4428 const BuiltinType *BT = EltType->getAs<BuiltinType>();
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004429 if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
Ulrich Weigand581badc2014-07-10 17:20:07 +00004430 getContext().getTypeSize(EltType) == 128) ||
4431 (BT && BT->isFloatingPoint()))
4432 AlignAsType = EltType;
4433 }
4434
Ulrich Weigandb7122372014-07-21 00:48:09 +00004435 // Likewise for ELFv2 homogeneous aggregates.
4436 const Type *Base = nullptr;
4437 uint64_t Members = 0;
4438 if (!AlignAsType && Kind == ELFv2 &&
4439 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
4440 AlignAsType = Base;
4441
Ulrich Weigand581badc2014-07-10 17:20:07 +00004442 // With special case aggregates, only vector base types need alignment.
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004443 if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
4444 if (getContext().getTypeSize(AlignAsType) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004445 return CharUnits::fromQuantity(32);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004446
John McCall7f416cc2015-09-08 08:05:57 +00004447 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004448 } else if (AlignAsType) {
John McCall7f416cc2015-09-08 08:05:57 +00004449 return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004450 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00004451
4452 // Otherwise, we only need alignment for any aggregate type that
4453 // has an alignment requirement of >= 16 bytes.
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004454 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
4455 if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
John McCall7f416cc2015-09-08 08:05:57 +00004456 return CharUnits::fromQuantity(32);
4457 return CharUnits::fromQuantity(16);
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004458 }
Ulrich Weigand581badc2014-07-10 17:20:07 +00004459
John McCall7f416cc2015-09-08 08:05:57 +00004460 return CharUnits::fromQuantity(8);
Ulrich Weigand581badc2014-07-10 17:20:07 +00004461}
4462
Ulrich Weigandb7122372014-07-21 00:48:09 +00004463/// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
4464/// aggregate. Base is set to the base element type, and Members is set
4465/// to the number of base elements.
Reid Klecknere9f6a712014-10-31 17:10:41 +00004466bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
4467 uint64_t &Members) const {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004468 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
4469 uint64_t NElements = AT->getSize().getZExtValue();
4470 if (NElements == 0)
4471 return false;
4472 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
4473 return false;
4474 Members *= NElements;
4475 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
4476 const RecordDecl *RD = RT->getDecl();
4477 if (RD->hasFlexibleArrayMember())
4478 return false;
4479
4480 Members = 0;
Ulrich Weiganda094f042014-10-29 13:23:20 +00004481
4482 // If this is a C++ record, check the bases first.
4483 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
4484 for (const auto &I : CXXRD->bases()) {
4485 // Ignore empty records.
4486 if (isEmptyRecord(getContext(), I.getType(), true))
4487 continue;
4488
4489 uint64_t FldMembers;
4490 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
4491 return false;
4492
4493 Members += FldMembers;
4494 }
4495 }
4496
Ulrich Weigandb7122372014-07-21 00:48:09 +00004497 for (const auto *FD : RD->fields()) {
4498 // Ignore (non-zero arrays of) empty records.
4499 QualType FT = FD->getType();
4500 while (const ConstantArrayType *AT =
4501 getContext().getAsConstantArrayType(FT)) {
4502 if (AT->getSize().getZExtValue() == 0)
4503 return false;
4504 FT = AT->getElementType();
4505 }
4506 if (isEmptyRecord(getContext(), FT, true))
4507 continue;
4508
4509 // For compatibility with GCC, ignore empty bitfields in C++ mode.
4510 if (getContext().getLangOpts().CPlusPlus &&
4511 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4512 continue;
4513
4514 uint64_t FldMembers;
4515 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
4516 return false;
4517
4518 Members = (RD->isUnion() ?
4519 std::max(Members, FldMembers) : Members + FldMembers);
4520 }
4521
4522 if (!Base)
4523 return false;
4524
4525 // Ensure there is no padding.
4526 if (getContext().getTypeSize(Base) * Members !=
4527 getContext().getTypeSize(Ty))
4528 return false;
4529 } else {
4530 Members = 1;
4531 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
4532 Members = 2;
4533 Ty = CT->getElementType();
4534 }
4535
Reid Klecknere9f6a712014-10-31 17:10:41 +00004536 // Most ABIs only support float, double, and some vector type widths.
4537 if (!isHomogeneousAggregateBaseType(Ty))
Ulrich Weigandb7122372014-07-21 00:48:09 +00004538 return false;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004539
4540 // The base type must be the same for all members. Types that
4541 // agree in both total size and mode (float vs. vector) are
4542 // treated as being equivalent here.
4543 const Type *TyPtr = Ty.getTypePtr();
Ahmed Bougacha40a34c22016-04-19 17:54:29 +00004544 if (!Base) {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004545 Base = TyPtr;
Ahmed Bougacha40a34c22016-04-19 17:54:29 +00004546 // If it's a non-power-of-2 vector, its size is already a power-of-2,
4547 // so make sure to widen it explicitly.
4548 if (const VectorType *VT = Base->getAs<VectorType>()) {
4549 QualType EltTy = VT->getElementType();
4550 unsigned NumElements =
4551 getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
4552 Base = getContext()
4553 .getVectorType(EltTy, NumElements, VT->getVectorKind())
4554 .getTypePtr();
4555 }
4556 }
Ulrich Weigandb7122372014-07-21 00:48:09 +00004557
4558 if (Base->isVectorType() != TyPtr->isVectorType() ||
4559 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
4560 return false;
4561 }
Reid Klecknere9f6a712014-10-31 17:10:41 +00004562 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
4563}
Ulrich Weigandb7122372014-07-21 00:48:09 +00004564
Reid Klecknere9f6a712014-10-31 17:10:41 +00004565bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4566 // Homogeneous aggregates for ELFv2 must have base types of float,
4567 // double, long double, or 128-bit vectors.
4568 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4569 if (BT->getKind() == BuiltinType::Float ||
4570 BT->getKind() == BuiltinType::Double ||
Hal Finkel415c2a32016-10-02 02:10:45 +00004571 BT->getKind() == BuiltinType::LongDouble) {
4572 if (IsSoftFloatABI)
4573 return false;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004574 return true;
Hal Finkel415c2a32016-10-02 02:10:45 +00004575 }
Reid Klecknere9f6a712014-10-31 17:10:41 +00004576 }
4577 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004578 if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
Reid Klecknere9f6a712014-10-31 17:10:41 +00004579 return true;
4580 }
4581 return false;
4582}
4583
4584bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
4585 const Type *Base, uint64_t Members) const {
Ulrich Weigandb7122372014-07-21 00:48:09 +00004586 // Vector types require one register, floating point types require one
4587 // or two registers depending on their size.
Reid Klecknere9f6a712014-10-31 17:10:41 +00004588 uint32_t NumRegs =
4589 Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004590
4591 // Homogeneous Aggregates may occupy at most 8 registers.
Reid Klecknere9f6a712014-10-31 17:10:41 +00004592 return Members * NumRegs <= 8;
Ulrich Weigandb7122372014-07-21 00:48:09 +00004593}
4594
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004595ABIArgInfo
4596PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerb1be6832014-11-15 01:41:41 +00004597 Ty = useFirstFieldIfTransparentUnion(Ty);
4598
Bill Schmidt90b22c92012-11-27 02:46:43 +00004599 if (Ty->isAnyComplexType())
4600 return ABIArgInfo::getDirect();
4601
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004602 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
4603 // or via reference (larger than 16 bytes).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004604 if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004605 uint64_t Size = getContext().getTypeSize(Ty);
4606 if (Size > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004607 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004608 else if (Size < 128) {
4609 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4610 return ABIArgInfo::getDirect(CoerceTy);
4611 }
4612 }
4613
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004614 if (isAggregateTypeForABI(Ty)) {
Mark Lacey3825e832013-10-06 01:33:34 +00004615 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00004616 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004617
John McCall7f416cc2015-09-08 08:05:57 +00004618 uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
4619 uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
Ulrich Weigandb7122372014-07-21 00:48:09 +00004620
4621 // ELFv2 homogeneous aggregates are passed as array types.
4622 const Type *Base = nullptr;
4623 uint64_t Members = 0;
4624 if (Kind == ELFv2 &&
4625 isHomogeneousAggregate(Ty, Base, Members)) {
4626 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4627 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4628 return ABIArgInfo::getDirect(CoerceTy);
4629 }
4630
Ulrich Weigand601957f2014-07-21 00:56:36 +00004631 // If an aggregate may end up fully in registers, we do not
4632 // use the ByVal method, but pass the aggregate as array.
4633 // This is usually beneficial since we avoid forcing the
4634 // back-end to store the argument to memory.
4635 uint64_t Bits = getContext().getTypeSize(Ty);
4636 if (Bits > 0 && Bits <= 8 * GPRBits) {
4637 llvm::Type *CoerceTy;
4638
4639 // Types up to 8 bytes are passed as integer type (which will be
4640 // properly aligned in the argument save area doubleword).
4641 if (Bits <= GPRBits)
Rui Ueyama83aa9792016-01-14 21:00:27 +00004642 CoerceTy =
4643 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Ulrich Weigand601957f2014-07-21 00:56:36 +00004644 // Larger types are passed as arrays, with the base type selected
4645 // according to the required alignment in the save area.
4646 else {
4647 uint64_t RegBits = ABIAlign * 8;
Rui Ueyama83aa9792016-01-14 21:00:27 +00004648 uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
Ulrich Weigand601957f2014-07-21 00:56:36 +00004649 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
4650 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
4651 }
4652
4653 return ABIArgInfo::getDirect(CoerceTy);
4654 }
4655
Ulrich Weigandb7122372014-07-21 00:48:09 +00004656 // All other aggregates are passed ByVal.
John McCall7f416cc2015-09-08 08:05:57 +00004657 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
4658 /*ByVal=*/true,
Ulrich Weigand581badc2014-07-10 17:20:07 +00004659 /*Realign=*/TyAlign > ABIAlign);
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004660 }
4661
4662 return (isPromotableTypeForABI(Ty) ?
4663 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4664}
4665
4666ABIArgInfo
4667PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4668 if (RetTy->isVoidType())
4669 return ABIArgInfo::getIgnore();
4670
Bill Schmidta3d121c2012-12-17 04:20:17 +00004671 if (RetTy->isAnyComplexType())
4672 return ABIArgInfo::getDirect();
4673
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004674 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
4675 // or via reference (larger than 16 bytes).
Hal Finkel0d0a1a52015-03-11 19:14:15 +00004676 if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004677 uint64_t Size = getContext().getTypeSize(RetTy);
4678 if (Size > 128)
John McCall7f416cc2015-09-08 08:05:57 +00004679 return getNaturalAlignIndirect(RetTy);
Ulrich Weigandf4eba982014-07-10 16:39:01 +00004680 else if (Size < 128) {
4681 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4682 return ABIArgInfo::getDirect(CoerceTy);
4683 }
4684 }
4685
Ulrich Weigandb7122372014-07-21 00:48:09 +00004686 if (isAggregateTypeForABI(RetTy)) {
4687 // ELFv2 homogeneous aggregates are returned as array types.
4688 const Type *Base = nullptr;
4689 uint64_t Members = 0;
4690 if (Kind == ELFv2 &&
4691 isHomogeneousAggregate(RetTy, Base, Members)) {
4692 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4693 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4694 return ABIArgInfo::getDirect(CoerceTy);
4695 }
4696
4697 // ELFv2 small aggregates are returned in up to two registers.
4698 uint64_t Bits = getContext().getTypeSize(RetTy);
4699 if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
4700 if (Bits == 0)
4701 return ABIArgInfo::getIgnore();
4702
4703 llvm::Type *CoerceTy;
4704 if (Bits > GPRBits) {
4705 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
Serge Guelton1d993272017-05-09 19:31:30 +00004706 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
Ulrich Weigandb7122372014-07-21 00:48:09 +00004707 } else
Rui Ueyama83aa9792016-01-14 21:00:27 +00004708 CoerceTy =
4709 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
Ulrich Weigandb7122372014-07-21 00:48:09 +00004710 return ABIArgInfo::getDirect(CoerceTy);
4711 }
4712
4713 // All other aggregates are returned indirectly.
John McCall7f416cc2015-09-08 08:05:57 +00004714 return getNaturalAlignIndirect(RetTy);
Ulrich Weigandb7122372014-07-21 00:48:09 +00004715 }
Ulrich Weigand77ed89d2012-11-05 19:13:42 +00004716
4717 return (isPromotableTypeForABI(RetTy) ?
4718 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4719}
4720
Bill Schmidt25cb3492012-10-03 19:18:57 +00004721// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
John McCall7f416cc2015-09-08 08:05:57 +00004722Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4723 QualType Ty) const {
4724 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4725 TypeInfo.second = getParamTypeAlignment(Ty);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004726
John McCall7f416cc2015-09-08 08:05:57 +00004727 CharUnits SlotSize = CharUnits::fromQuantity(8);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004728
Bill Schmidt924c4782013-01-14 17:45:36 +00004729 // If we have a complex type and the base type is smaller than 8 bytes,
4730 // the ABI calls for the real and imaginary parts to be right-adjusted
4731 // in separate doublewords. However, Clang expects us to produce a
4732 // pointer to a structure with the two parts packed tightly. So generate
4733 // loads of the real and imaginary parts relative to the va_list pointer,
4734 // and store them to a temporary structure.
John McCall7f416cc2015-09-08 08:05:57 +00004735 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4736 CharUnits EltSize = TypeInfo.first / 2;
4737 if (EltSize < SlotSize) {
4738 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
4739 SlotSize * 2, SlotSize,
4740 SlotSize, /*AllowHigher*/ true);
4741
4742 Address RealAddr = Addr;
4743 Address ImagAddr = RealAddr;
4744 if (CGF.CGM.getDataLayout().isBigEndian()) {
4745 RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
4746 SlotSize - EltSize);
4747 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
4748 2 * SlotSize - EltSize);
4749 } else {
4750 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
4751 }
4752
4753 llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
4754 RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
4755 ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
4756 llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
4757 llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
4758
4759 Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
4760 CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
4761 /*init*/ true);
4762 return Temp;
Ulrich Weigandbebc55b2014-06-20 16:37:40 +00004763 }
Bill Schmidt924c4782013-01-14 17:45:36 +00004764 }
4765
John McCall7f416cc2015-09-08 08:05:57 +00004766 // Otherwise, just use the general rule.
4767 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
4768 TypeInfo, SlotSize, /*AllowHigher*/ true);
Bill Schmidt25cb3492012-10-03 19:18:57 +00004769}
4770
4771static bool
4772PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4773 llvm::Value *Address) {
Roman Divackyd966e722012-05-09 18:22:46 +00004774 // This is calculated from the LLVM and GCC tables and verified
4775 // against gcc output. AFAIK all ABIs use the same encoding.
4776
4777 CodeGen::CGBuilderTy &Builder = CGF.Builder;
4778
4779 llvm::IntegerType *i8 = CGF.Int8Ty;
4780 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4781 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4782 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4783
4784 // 0-31: r0-31, the 8-byte general-purpose registers
4785 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
4786
4787 // 32-63: fp0-31, the 8-byte floating-point registers
4788 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4789
Hal Finkel84832a72016-08-30 02:38:34 +00004790 // 64-67 are various 8-byte special-purpose registers:
Roman Divackyd966e722012-05-09 18:22:46 +00004791 // 64: mq
4792 // 65: lr
4793 // 66: ctr
4794 // 67: ap
Hal Finkel84832a72016-08-30 02:38:34 +00004795 AssignToArrayRange(Builder, Address, Eight8, 64, 67);
4796
4797 // 68-76 are various 4-byte special-purpose registers:
Roman Divackyd966e722012-05-09 18:22:46 +00004798 // 68-75 cr0-7
4799 // 76: xer
Hal Finkel84832a72016-08-30 02:38:34 +00004800 AssignToArrayRange(Builder, Address, Four8, 68, 76);
Roman Divackyd966e722012-05-09 18:22:46 +00004801
4802 // 77-108: v0-31, the 16-byte vector registers
4803 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4804
4805 // 109: vrsave
4806 // 110: vscr
4807 // 111: spe_acc
4808 // 112: spefscr
4809 // 113: sfp
Hal Finkel84832a72016-08-30 02:38:34 +00004810 // 114: tfhar
4811 // 115: tfiar
4812 // 116: texasr
4813 AssignToArrayRange(Builder, Address, Eight8, 109, 116);
Roman Divackyd966e722012-05-09 18:22:46 +00004814
4815 return false;
4816}
John McCallea8d8bb2010-03-11 00:10:12 +00004817
Bill Schmidt25cb3492012-10-03 19:18:57 +00004818bool
4819PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
4820 CodeGen::CodeGenFunction &CGF,
4821 llvm::Value *Address) const {
4822
4823 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4824}
4825
4826bool
4827PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4828 llvm::Value *Address) const {
4829
4830 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4831}
4832
Chris Lattner0cf24192010-06-28 20:05:43 +00004833//===----------------------------------------------------------------------===//
Tim Northover573cbee2014-05-24 12:52:07 +00004834// AArch64 ABI Implementation
Tim Northovera2ee4332014-03-29 15:09:45 +00004835//===----------------------------------------------------------------------===//
4836
4837namespace {
4838
John McCall12f23522016-04-04 18:33:08 +00004839class AArch64ABIInfo : public SwiftABIInfo {
Tim Northovera2ee4332014-03-29 15:09:45 +00004840public:
4841 enum ABIKind {
4842 AAPCS = 0,
Martin Storsjo502de222017-07-13 17:59:14 +00004843 DarwinPCS,
4844 Win64
Tim Northovera2ee4332014-03-29 15:09:45 +00004845 };
4846
4847private:
4848 ABIKind Kind;
4849
4850public:
John McCall12f23522016-04-04 18:33:08 +00004851 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
4852 : SwiftABIInfo(CGT), Kind(Kind) {}
Tim Northovera2ee4332014-03-29 15:09:45 +00004853
4854private:
4855 ABIKind getABIKind() const { return Kind; }
4856 bool isDarwinPCS() const { return Kind == DarwinPCS; }
4857
4858 ABIArgInfo classifyReturnType(QualType RetTy) const;
Tim Northoverb047bfa2014-11-27 21:02:49 +00004859 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004860 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4861 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4862 uint64_t Members) const override;
4863
Tim Northovera2ee4332014-03-29 15:09:45 +00004864 bool isIllegalVectorType(QualType Ty) const;
4865
David Blaikie1cbb9712014-11-14 19:09:44 +00004866 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00004867 if (!getCXXABI().classifyReturnType(FI))
4868 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Tim Northover5ffc0922014-04-17 10:20:38 +00004869
Tim Northoverb047bfa2014-11-27 21:02:49 +00004870 for (auto &it : FI.arguments())
4871 it.info = classifyArgumentType(it.type);
Tim Northovera2ee4332014-03-29 15:09:45 +00004872 }
4873
John McCall7f416cc2015-09-08 08:05:57 +00004874 Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4875 CodeGenFunction &CGF) const;
Tim Northovera2ee4332014-03-29 15:09:45 +00004876
John McCall7f416cc2015-09-08 08:05:57 +00004877 Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
4878 CodeGenFunction &CGF) const;
Tim Northovera2ee4332014-03-29 15:09:45 +00004879
John McCall7f416cc2015-09-08 08:05:57 +00004880 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4881 QualType Ty) const override {
Martin Storsjo502de222017-07-13 17:59:14 +00004882 return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
4883 : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
4884 : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
Tim Northovera2ee4332014-03-29 15:09:45 +00004885 }
John McCall12f23522016-04-04 18:33:08 +00004886
Martin Storsjo502de222017-07-13 17:59:14 +00004887 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
4888 QualType Ty) const override;
4889
John McCall12f23522016-04-04 18:33:08 +00004890 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
4891 ArrayRef<llvm::Type*> scalars,
4892 bool asReturnValue) const override {
4893 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
4894 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00004895 bool isSwiftErrorInRegister() const override {
4896 return true;
4897 }
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00004898
4899 bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
4900 unsigned elts) const override;
Tim Northovera2ee4332014-03-29 15:09:45 +00004901};
4902
Tim Northover573cbee2014-05-24 12:52:07 +00004903class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
Tim Northovera2ee4332014-03-29 15:09:45 +00004904public:
Tim Northover573cbee2014-05-24 12:52:07 +00004905 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
4906 : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
Tim Northovera2ee4332014-03-29 15:09:45 +00004907
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004908 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
Oliver Stannard7f188642017-08-21 09:54:46 +00004909 return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
Tim Northovera2ee4332014-03-29 15:09:45 +00004910 }
4911
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004912 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4913 return 31;
4914 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004915
Alexander Kornienko34eb2072015-04-11 02:00:23 +00004916 bool doesReturnSlotInterfereWithArgs() const override { return false; }
Tim Northovera2ee4332014-03-29 15:09:45 +00004917};
Martin Storsjo1c8af272017-07-20 05:47:06 +00004918
4919class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
4920public:
4921 WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
4922 : AArch64TargetCodeGenInfo(CGT, K) {}
4923
4924 void getDependentLibraryOption(llvm::StringRef Lib,
4925 llvm::SmallString<24> &Opt) const override {
4926 Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
4927 }
4928
4929 void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
4930 llvm::SmallString<32> &Opt) const override {
4931 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
4932 }
4933};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004934}
Tim Northovera2ee4332014-03-29 15:09:45 +00004935
Tim Northoverb047bfa2014-11-27 21:02:49 +00004936ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
Reid Klecknerb1be6832014-11-15 01:41:41 +00004937 Ty = useFirstFieldIfTransparentUnion(Ty);
4938
Tim Northovera2ee4332014-03-29 15:09:45 +00004939 // Handle illegal vector types here.
4940 if (isIllegalVectorType(Ty)) {
4941 uint64_t Size = getContext().getTypeSize(Ty);
Nirav Dave9a8f97e2016-02-22 16:48:42 +00004942 // Android promotes <2 x i8> to i16, not i32
Ahmed Bougacha8862cae2016-04-19 17:54:24 +00004943 if (isAndroid() && (Size <= 16)) {
Nirav Dave9a8f97e2016-02-22 16:48:42 +00004944 llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
4945 return ABIArgInfo::getDirect(ResType);
4946 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004947 if (Size <= 32) {
4948 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
Tim Northovera2ee4332014-03-29 15:09:45 +00004949 return ABIArgInfo::getDirect(ResType);
4950 }
4951 if (Size == 64) {
4952 llvm::Type *ResType =
4953 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
Tim Northovera2ee4332014-03-29 15:09:45 +00004954 return ABIArgInfo::getDirect(ResType);
4955 }
4956 if (Size == 128) {
4957 llvm::Type *ResType =
4958 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
Tim Northovera2ee4332014-03-29 15:09:45 +00004959 return ABIArgInfo::getDirect(ResType);
4960 }
John McCall7f416cc2015-09-08 08:05:57 +00004961 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Tim Northovera2ee4332014-03-29 15:09:45 +00004962 }
Tim Northovera2ee4332014-03-29 15:09:45 +00004963
4964 if (!isAggregateTypeForABI(Ty)) {
4965 // Treat an enum type as its underlying type.
4966 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4967 Ty = EnumTy->getDecl()->getIntegerType();
4968
Tim Northovera2ee4332014-03-29 15:09:45 +00004969 return (Ty->isPromotableIntegerType() && isDarwinPCS()
4970 ? ABIArgInfo::getExtend()
4971 : ABIArgInfo::getDirect());
4972 }
4973
4974 // Structures with either a non-trivial destructor or a non-trivial
4975 // copy constructor are always indirect.
Reid Kleckner40ca9132014-05-13 22:05:45 +00004976 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
John McCall7f416cc2015-09-08 08:05:57 +00004977 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
4978 CGCXXABI::RAA_DirectInMemory);
Tim Northovera2ee4332014-03-29 15:09:45 +00004979 }
4980
4981 // Empty records are always ignored on Darwin, but actually passed in C++ mode
4982 // elsewhere for GNU compatibility.
Tim Northover23bcad22017-05-05 22:36:06 +00004983 uint64_t Size = getContext().getTypeSize(Ty);
4984 bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
4985 if (IsEmpty || Size == 0) {
Tim Northovera2ee4332014-03-29 15:09:45 +00004986 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
4987 return ABIArgInfo::getIgnore();
4988
Tim Northover23bcad22017-05-05 22:36:06 +00004989 // GNU C mode. The only argument that gets ignored is an empty one with size
4990 // 0.
4991 if (IsEmpty && Size == 0)
4992 return ABIArgInfo::getIgnore();
Tim Northovera2ee4332014-03-29 15:09:45 +00004993 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4994 }
4995
4996 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
Craig Topper8a13c412014-05-21 05:09:00 +00004997 const Type *Base = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00004998 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00004999 if (isHomogeneousAggregate(Ty, Base, Members)) {
Tim Northoverb047bfa2014-11-27 21:02:49 +00005000 return ABIArgInfo::getDirect(
5001 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
Tim Northovera2ee4332014-03-29 15:09:45 +00005002 }
5003
5004 // Aggregates <= 16 bytes are passed directly in registers or on the stack.
Tim Northovera2ee4332014-03-29 15:09:45 +00005005 if (Size <= 128) {
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00005006 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5007 // same size and alignment.
5008 if (getTarget().isRenderScriptTarget()) {
5009 return coerceToIntArray(Ty, getContext(), getVMContext());
5010 }
Tim Northoverc801b4a2014-04-15 14:55:11 +00005011 unsigned Alignment = getContext().getTypeAlign(Ty);
Davide Italiano7a3b69d2017-04-03 16:51:39 +00005012 Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
Tim Northoverb047bfa2014-11-27 21:02:49 +00005013
Tim Northovera2ee4332014-03-29 15:09:45 +00005014 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5015 // For aggregates with 16-byte alignment, we use i128.
Tim Northoverc801b4a2014-04-15 14:55:11 +00005016 if (Alignment < 128 && Size == 128) {
Tim Northovera2ee4332014-03-29 15:09:45 +00005017 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5018 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5019 }
5020 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5021 }
5022
John McCall7f416cc2015-09-08 08:05:57 +00005023 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Tim Northovera2ee4332014-03-29 15:09:45 +00005024}
5025
Tim Northover573cbee2014-05-24 12:52:07 +00005026ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
Tim Northovera2ee4332014-03-29 15:09:45 +00005027 if (RetTy->isVoidType())
5028 return ABIArgInfo::getIgnore();
5029
5030 // Large vector types should be returned via memory.
5031 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
John McCall7f416cc2015-09-08 08:05:57 +00005032 return getNaturalAlignIndirect(RetTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005033
5034 if (!isAggregateTypeForABI(RetTy)) {
5035 // Treat an enum type as its underlying type.
5036 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5037 RetTy = EnumTy->getDecl()->getIntegerType();
5038
Tim Northover4dab6982014-04-18 13:46:08 +00005039 return (RetTy->isPromotableIntegerType() && isDarwinPCS()
5040 ? ABIArgInfo::getExtend()
5041 : ABIArgInfo::getDirect());
Tim Northovera2ee4332014-03-29 15:09:45 +00005042 }
5043
Tim Northover23bcad22017-05-05 22:36:06 +00005044 uint64_t Size = getContext().getTypeSize(RetTy);
5045 if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
Tim Northovera2ee4332014-03-29 15:09:45 +00005046 return ABIArgInfo::getIgnore();
5047
Craig Topper8a13c412014-05-21 05:09:00 +00005048 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005049 uint64_t Members = 0;
5050 if (isHomogeneousAggregate(RetTy, Base, Members))
Tim Northovera2ee4332014-03-29 15:09:45 +00005051 // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
5052 return ABIArgInfo::getDirect();
5053
5054 // Aggregates <= 16 bytes are returned directly in registers or on the stack.
Tim Northovera2ee4332014-03-29 15:09:45 +00005055 if (Size <= 128) {
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00005056 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5057 // same size and alignment.
5058 if (getTarget().isRenderScriptTarget()) {
5059 return coerceToIntArray(RetTy, getContext(), getVMContext());
5060 }
Pete Cooper635b5092015-04-17 22:16:24 +00005061 unsigned Alignment = getContext().getTypeAlign(RetTy);
Davide Italiano7a3b69d2017-04-03 16:51:39 +00005062 Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
Pete Cooper635b5092015-04-17 22:16:24 +00005063
5064 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5065 // For aggregates with 16-byte alignment, we use i128.
5066 if (Alignment < 128 && Size == 128) {
5067 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5068 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5069 }
Tim Northovera2ee4332014-03-29 15:09:45 +00005070 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5071 }
5072
John McCall7f416cc2015-09-08 08:05:57 +00005073 return getNaturalAlignIndirect(RetTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005074}
5075
Tim Northover573cbee2014-05-24 12:52:07 +00005076/// isIllegalVectorType - check whether the vector type is legal for AArch64.
5077bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
Tim Northovera2ee4332014-03-29 15:09:45 +00005078 if (const VectorType *VT = Ty->getAs<VectorType>()) {
5079 // Check whether VT is legal.
5080 unsigned NumElements = VT->getNumElements();
5081 uint64_t Size = getContext().getTypeSize(VT);
Tim Northover34fd4fb2016-05-03 19:24:47 +00005082 // NumElements should be power of 2.
Tim Northover360d2b32016-05-03 19:22:41 +00005083 if (!llvm::isPowerOf2_32(NumElements))
Tim Northovera2ee4332014-03-29 15:09:45 +00005084 return true;
5085 return Size != 64 && (Size != 128 || NumElements == 1);
5086 }
5087 return false;
5088}
5089
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00005090bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize,
5091 llvm::Type *eltTy,
5092 unsigned elts) const {
5093 if (!llvm::isPowerOf2_32(elts))
5094 return false;
5095 if (totalSize.getQuantity() != 8 &&
5096 (totalSize.getQuantity() != 16 || elts == 1))
5097 return false;
5098 return true;
5099}
5100
Reid Klecknere9f6a712014-10-31 17:10:41 +00005101bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5102 // Homogeneous aggregates for AAPCS64 must have base types of a floating
5103 // point type or a short-vector type. This is the same as the 32-bit ABI,
5104 // but with the difference that any floating-point type is allowed,
5105 // including __fp16.
5106 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5107 if (BT->isFloatingPoint())
5108 return true;
5109 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5110 unsigned VecSize = getContext().getTypeSize(VT);
5111 if (VecSize == 64 || VecSize == 128)
5112 return true;
5113 }
5114 return false;
5115}
5116
5117bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5118 uint64_t Members) const {
5119 return Members <= 4;
5120}
5121
John McCall7f416cc2015-09-08 08:05:57 +00005122Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
Tim Northoverb047bfa2014-11-27 21:02:49 +00005123 QualType Ty,
5124 CodeGenFunction &CGF) const {
5125 ABIArgInfo AI = classifyArgumentType(Ty);
Reid Klecknere9f6a712014-10-31 17:10:41 +00005126 bool IsIndirect = AI.isIndirect();
5127
Tim Northoverb047bfa2014-11-27 21:02:49 +00005128 llvm::Type *BaseTy = CGF.ConvertType(Ty);
5129 if (IsIndirect)
5130 BaseTy = llvm::PointerType::getUnqual(BaseTy);
5131 else if (AI.getCoerceToType())
5132 BaseTy = AI.getCoerceToType();
5133
5134 unsigned NumRegs = 1;
5135 if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
5136 BaseTy = ArrTy->getElementType();
5137 NumRegs = ArrTy->getNumElements();
5138 }
5139 bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
5140
Tim Northovera2ee4332014-03-29 15:09:45 +00005141 // The AArch64 va_list type and handling is specified in the Procedure Call
5142 // Standard, section B.4:
5143 //
5144 // struct {
5145 // void *__stack;
5146 // void *__gr_top;
5147 // void *__vr_top;
5148 // int __gr_offs;
5149 // int __vr_offs;
5150 // };
5151
5152 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
5153 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5154 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
5155 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
Tim Northovera2ee4332014-03-29 15:09:45 +00005156
John McCall7f416cc2015-09-08 08:05:57 +00005157 auto TyInfo = getContext().getTypeInfoInChars(Ty);
5158 CharUnits TyAlign = TyInfo.second;
5159
5160 Address reg_offs_p = Address::invalid();
5161 llvm::Value *reg_offs = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005162 int reg_top_index;
John McCall7f416cc2015-09-08 08:05:57 +00005163 CharUnits reg_top_offset;
5164 int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity();
Tim Northoverb047bfa2014-11-27 21:02:49 +00005165 if (!IsFPR) {
Tim Northovera2ee4332014-03-29 15:09:45 +00005166 // 3 is the field number of __gr_offs
David Blaikie2e804282015-04-05 22:47:07 +00005167 reg_offs_p =
John McCall7f416cc2015-09-08 08:05:57 +00005168 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
5169 "gr_offs_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00005170 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
5171 reg_top_index = 1; // field number for __gr_top
John McCall7f416cc2015-09-08 08:05:57 +00005172 reg_top_offset = CharUnits::fromQuantity(8);
Rui Ueyama83aa9792016-01-14 21:00:27 +00005173 RegSize = llvm::alignTo(RegSize, 8);
Tim Northovera2ee4332014-03-29 15:09:45 +00005174 } else {
Tim Northovera2ee4332014-03-29 15:09:45 +00005175 // 4 is the field number of __vr_offs.
David Blaikie2e804282015-04-05 22:47:07 +00005176 reg_offs_p =
John McCall7f416cc2015-09-08 08:05:57 +00005177 CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28),
5178 "vr_offs_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00005179 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
5180 reg_top_index = 2; // field number for __vr_top
John McCall7f416cc2015-09-08 08:05:57 +00005181 reg_top_offset = CharUnits::fromQuantity(16);
Tim Northoverb047bfa2014-11-27 21:02:49 +00005182 RegSize = 16 * NumRegs;
Tim Northovera2ee4332014-03-29 15:09:45 +00005183 }
5184
5185 //=======================================
5186 // Find out where argument was passed
5187 //=======================================
5188
5189 // If reg_offs >= 0 we're already using the stack for this type of
5190 // argument. We don't want to keep updating reg_offs (in case it overflows,
5191 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
5192 // whatever they get).
Craig Topper8a13c412014-05-21 05:09:00 +00005193 llvm::Value *UsingStack = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005194 UsingStack = CGF.Builder.CreateICmpSGE(
5195 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
5196
5197 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
5198
5199 // Otherwise, at least some kind of argument could go in these registers, the
Bob Wilson3abf1692014-04-21 01:23:36 +00005200 // question is whether this particular type is too big.
Tim Northovera2ee4332014-03-29 15:09:45 +00005201 CGF.EmitBlock(MaybeRegBlock);
5202
5203 // Integer arguments may need to correct register alignment (for example a
5204 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
5205 // align __gr_offs to calculate the potential address.
John McCall7f416cc2015-09-08 08:05:57 +00005206 if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
5207 int Align = TyAlign.getQuantity();
Tim Northovera2ee4332014-03-29 15:09:45 +00005208
5209 reg_offs = CGF.Builder.CreateAdd(
5210 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
5211 "align_regoffs");
5212 reg_offs = CGF.Builder.CreateAnd(
5213 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
5214 "aligned_regoffs");
5215 }
5216
5217 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
John McCall7f416cc2015-09-08 08:05:57 +00005218 // The fact that this is done unconditionally reflects the fact that
5219 // allocating an argument to the stack also uses up all the remaining
5220 // registers of the appropriate kind.
Craig Topper8a13c412014-05-21 05:09:00 +00005221 llvm::Value *NewOffset = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005222 NewOffset = CGF.Builder.CreateAdd(
5223 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
5224 CGF.Builder.CreateStore(NewOffset, reg_offs_p);
5225
5226 // Now we're in a position to decide whether this argument really was in
5227 // registers or not.
Craig Topper8a13c412014-05-21 05:09:00 +00005228 llvm::Value *InRegs = nullptr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005229 InRegs = CGF.Builder.CreateICmpSLE(
5230 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
5231
5232 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
5233
5234 //=======================================
5235 // Argument was in registers
5236 //=======================================
5237
5238 // Now we emit the code for if the argument was originally passed in
5239 // registers. First start the appropriate block:
5240 CGF.EmitBlock(InRegBlock);
5241
John McCall7f416cc2015-09-08 08:05:57 +00005242 llvm::Value *reg_top = nullptr;
5243 Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index,
5244 reg_top_offset, "reg_top_p");
Tim Northovera2ee4332014-03-29 15:09:45 +00005245 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
John McCall7f416cc2015-09-08 08:05:57 +00005246 Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
5247 CharUnits::fromQuantity(IsFPR ? 16 : 8));
5248 Address RegAddr = Address::invalid();
5249 llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
Tim Northovera2ee4332014-03-29 15:09:45 +00005250
5251 if (IsIndirect) {
5252 // If it's been passed indirectly (actually a struct), whatever we find from
5253 // stored registers or on the stack will actually be a struct **.
5254 MemTy = llvm::PointerType::getUnqual(MemTy);
5255 }
5256
Craig Topper8a13c412014-05-21 05:09:00 +00005257 const Type *Base = nullptr;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005258 uint64_t NumMembers = 0;
5259 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
James Molloy467be602014-05-07 14:45:55 +00005260 if (IsHFA && NumMembers > 1) {
Tim Northovera2ee4332014-03-29 15:09:45 +00005261 // Homogeneous aggregates passed in registers will have their elements split
5262 // and stored 16-bytes apart regardless of size (they're notionally in qN,
5263 // qN+1, ...). We reload and store into a temporary local variable
5264 // contiguously.
5265 assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
John McCall7f416cc2015-09-08 08:05:57 +00005266 auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
Tim Northovera2ee4332014-03-29 15:09:45 +00005267 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
5268 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
John McCall7f416cc2015-09-08 08:05:57 +00005269 Address Tmp = CGF.CreateTempAlloca(HFATy,
5270 std::max(TyAlign, BaseTyInfo.second));
Tim Northovera2ee4332014-03-29 15:09:45 +00005271
John McCall7f416cc2015-09-08 08:05:57 +00005272 // On big-endian platforms, the value will be right-aligned in its slot.
5273 int Offset = 0;
5274 if (CGF.CGM.getDataLayout().isBigEndian() &&
5275 BaseTyInfo.first.getQuantity() < 16)
5276 Offset = 16 - BaseTyInfo.first.getQuantity();
5277
Tim Northovera2ee4332014-03-29 15:09:45 +00005278 for (unsigned i = 0; i < NumMembers; ++i) {
John McCall7f416cc2015-09-08 08:05:57 +00005279 CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
5280 Address LoadAddr =
5281 CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
5282 LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
5283
5284 Address StoreAddr =
5285 CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first);
Tim Northovera2ee4332014-03-29 15:09:45 +00005286
5287 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
5288 CGF.Builder.CreateStore(Elem, StoreAddr);
5289 }
5290
John McCall7f416cc2015-09-08 08:05:57 +00005291 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005292 } else {
John McCall7f416cc2015-09-08 08:05:57 +00005293 // Otherwise the object is contiguous in memory.
5294
5295 // It might be right-aligned in its slot.
5296 CharUnits SlotSize = BaseAddr.getAlignment();
5297 if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
James Molloy467be602014-05-07 14:45:55 +00005298 (IsHFA || !isAggregateTypeForABI(Ty)) &&
John McCall7f416cc2015-09-08 08:05:57 +00005299 TyInfo.first < SlotSize) {
5300 CharUnits Offset = SlotSize - TyInfo.first;
5301 BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
Tim Northovera2ee4332014-03-29 15:09:45 +00005302 }
5303
John McCall7f416cc2015-09-08 08:05:57 +00005304 RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005305 }
5306
5307 CGF.EmitBranch(ContBlock);
5308
5309 //=======================================
5310 // Argument was on the stack
5311 //=======================================
5312 CGF.EmitBlock(OnStackBlock);
5313
John McCall7f416cc2015-09-08 08:05:57 +00005314 Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0,
5315 CharUnits::Zero(), "stack_p");
5316 llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
Tim Northovera2ee4332014-03-29 15:09:45 +00005317
John McCall7f416cc2015-09-08 08:05:57 +00005318 // Again, stack arguments may need realignment. In this case both integer and
Tim Northovera2ee4332014-03-29 15:09:45 +00005319 // floating-point ones might be affected.
John McCall7f416cc2015-09-08 08:05:57 +00005320 if (!IsIndirect && TyAlign.getQuantity() > 8) {
5321 int Align = TyAlign.getQuantity();
Tim Northovera2ee4332014-03-29 15:09:45 +00005322
John McCall7f416cc2015-09-08 08:05:57 +00005323 OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
Tim Northovera2ee4332014-03-29 15:09:45 +00005324
John McCall7f416cc2015-09-08 08:05:57 +00005325 OnStackPtr = CGF.Builder.CreateAdd(
5326 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
Tim Northovera2ee4332014-03-29 15:09:45 +00005327 "align_stack");
John McCall7f416cc2015-09-08 08:05:57 +00005328 OnStackPtr = CGF.Builder.CreateAnd(
5329 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
Tim Northovera2ee4332014-03-29 15:09:45 +00005330 "align_stack");
5331
John McCall7f416cc2015-09-08 08:05:57 +00005332 OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005333 }
John McCall7f416cc2015-09-08 08:05:57 +00005334 Address OnStackAddr(OnStackPtr,
5335 std::max(CharUnits::fromQuantity(8), TyAlign));
Tim Northovera2ee4332014-03-29 15:09:45 +00005336
John McCall7f416cc2015-09-08 08:05:57 +00005337 // All stack slots are multiples of 8 bytes.
5338 CharUnits StackSlotSize = CharUnits::fromQuantity(8);
5339 CharUnits StackSize;
Tim Northovera2ee4332014-03-29 15:09:45 +00005340 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00005341 StackSize = StackSlotSize;
Tim Northovera2ee4332014-03-29 15:09:45 +00005342 else
Rui Ueyama83aa9792016-01-14 21:00:27 +00005343 StackSize = TyInfo.first.alignTo(StackSlotSize);
Tim Northovera2ee4332014-03-29 15:09:45 +00005344
John McCall7f416cc2015-09-08 08:05:57 +00005345 llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
Tim Northovera2ee4332014-03-29 15:09:45 +00005346 llvm::Value *NewStack =
John McCall7f416cc2015-09-08 08:05:57 +00005347 CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
Tim Northovera2ee4332014-03-29 15:09:45 +00005348
5349 // Write the new value of __stack for the next call to va_arg
5350 CGF.Builder.CreateStore(NewStack, stack_p);
5351
5352 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
John McCall7f416cc2015-09-08 08:05:57 +00005353 TyInfo.first < StackSlotSize) {
5354 CharUnits Offset = StackSlotSize - TyInfo.first;
5355 OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
Tim Northovera2ee4332014-03-29 15:09:45 +00005356 }
5357
John McCall7f416cc2015-09-08 08:05:57 +00005358 OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
Tim Northovera2ee4332014-03-29 15:09:45 +00005359
5360 CGF.EmitBranch(ContBlock);
5361
5362 //=======================================
5363 // Tidy up
5364 //=======================================
5365 CGF.EmitBlock(ContBlock);
5366
John McCall7f416cc2015-09-08 08:05:57 +00005367 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
5368 OnStackAddr, OnStackBlock, "vaargs.addr");
Tim Northovera2ee4332014-03-29 15:09:45 +00005369
5370 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00005371 return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
5372 TyInfo.second);
Tim Northovera2ee4332014-03-29 15:09:45 +00005373
5374 return ResAddr;
5375}
5376
John McCall7f416cc2015-09-08 08:05:57 +00005377Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
5378 CodeGenFunction &CGF) const {
5379 // The backend's lowering doesn't support va_arg for aggregates or
5380 // illegal vector types. Lower VAArg here for these cases and use
5381 // the LLVM va_arg instruction for everything else.
Tim Northovera2ee4332014-03-29 15:09:45 +00005382 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
James Y Knight29b5f082016-02-24 02:59:33 +00005383 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
Tim Northovera2ee4332014-03-29 15:09:45 +00005384
John McCall7f416cc2015-09-08 08:05:57 +00005385 CharUnits SlotSize = CharUnits::fromQuantity(8);
Tim Northovera2ee4332014-03-29 15:09:45 +00005386
John McCall7f416cc2015-09-08 08:05:57 +00005387 // Empty records are ignored for parameter passing purposes.
Tim Northovera2ee4332014-03-29 15:09:45 +00005388 if (isEmptyRecord(getContext(), Ty, true)) {
John McCall7f416cc2015-09-08 08:05:57 +00005389 Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
5390 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
5391 return Addr;
Tim Northovera2ee4332014-03-29 15:09:45 +00005392 }
5393
John McCall7f416cc2015-09-08 08:05:57 +00005394 // The size of the actual thing passed, which might end up just
5395 // being a pointer for indirect types.
5396 auto TyInfo = getContext().getTypeInfoInChars(Ty);
5397
5398 // Arguments bigger than 16 bytes which aren't homogeneous
5399 // aggregates should be passed indirectly.
5400 bool IsIndirect = false;
5401 if (TyInfo.first.getQuantity() > 16) {
5402 const Type *Base = nullptr;
5403 uint64_t Members = 0;
5404 IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
Tim Northovera2ee4332014-03-29 15:09:45 +00005405 }
5406
John McCall7f416cc2015-09-08 08:05:57 +00005407 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
5408 TyInfo, SlotSize, /*AllowHigherAlign*/ true);
Tim Northovera2ee4332014-03-29 15:09:45 +00005409}
5410
Martin Storsjo502de222017-07-13 17:59:14 +00005411Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
5412 QualType Ty) const {
5413 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
5414 CGF.getContext().getTypeInfoInChars(Ty),
5415 CharUnits::fromQuantity(8),
5416 /*allowHigherAlign*/ false);
5417}
5418
Tim Northovera2ee4332014-03-29 15:09:45 +00005419//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00005420// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00005421//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00005422
5423namespace {
5424
John McCall12f23522016-04-04 18:33:08 +00005425class ARMABIInfo : public SwiftABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00005426public:
5427 enum ABIKind {
5428 APCS = 0,
5429 AAPCS = 1,
Tim Northover5627d392015-10-30 16:30:45 +00005430 AAPCS_VFP = 2,
5431 AAPCS16_VFP = 3,
Daniel Dunbar020daa92009-09-12 01:00:39 +00005432 };
5433
5434private:
5435 ABIKind Kind;
5436
5437public:
John McCall12f23522016-04-04 18:33:08 +00005438 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
5439 : SwiftABIInfo(CGT), Kind(_Kind) {
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005440 setCCs();
John McCall882987f2013-02-28 19:01:20 +00005441 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00005442
John McCall3480ef22011-08-30 01:42:09 +00005443 bool isEABI() const {
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00005444 switch (getTarget().getTriple().getEnvironment()) {
5445 case llvm::Triple::Android:
5446 case llvm::Triple::EABI:
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00005447 case llvm::Triple::EABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00005448 case llvm::Triple::GNUEABI:
Joerg Sonnenberger0c1652d2013-12-16 18:30:28 +00005449 case llvm::Triple::GNUEABIHF:
Rafael Espindola0fa66802016-06-24 21:35:06 +00005450 case llvm::Triple::MuslEABI:
5451 case llvm::Triple::MuslEABIHF:
Joerg Sonnenberger782e6aa2013-12-12 21:29:27 +00005452 return true;
5453 default:
5454 return false;
5455 }
John McCall3480ef22011-08-30 01:42:09 +00005456 }
5457
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00005458 bool isEABIHF() const {
5459 switch (getTarget().getTriple().getEnvironment()) {
5460 case llvm::Triple::EABIHF:
5461 case llvm::Triple::GNUEABIHF:
Rafael Espindola0fa66802016-06-24 21:35:06 +00005462 case llvm::Triple::MuslEABIHF:
Joerg Sonnenbergerd75a1f82013-12-16 19:16:04 +00005463 return true;
5464 default:
5465 return false;
5466 }
5467 }
5468
Daniel Dunbar020daa92009-09-12 01:00:39 +00005469 ABIKind getABIKind() const { return Kind; }
5470
Tim Northovera484bc02013-10-01 14:34:25 +00005471private:
Amara Emerson9dc78782014-01-28 10:56:36 +00005472 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
Tim Northoverbc784d12015-02-24 17:22:40 +00005473 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const;
Manman Renfef9e312012-10-16 19:18:39 +00005474 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005475
Reid Klecknere9f6a712014-10-31 17:10:41 +00005476 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5477 bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5478 uint64_t Members) const override;
5479
Craig Topper4f12f102014-03-12 06:41:41 +00005480 void computeInfo(CGFunctionInfo &FI) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005481
John McCall7f416cc2015-09-08 08:05:57 +00005482 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5483 QualType Ty) const override;
John McCall882987f2013-02-28 19:01:20 +00005484
5485 llvm::CallingConv::ID getLLVMDefaultCC() const;
5486 llvm::CallingConv::ID getABIDefaultCC() const;
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005487 void setCCs();
John McCall12f23522016-04-04 18:33:08 +00005488
5489 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
5490 ArrayRef<llvm::Type*> scalars,
5491 bool asReturnValue) const override {
5492 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5493 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00005494 bool isSwiftErrorInRegister() const override {
5495 return true;
5496 }
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00005497 bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
5498 unsigned elts) const override;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005499};
5500
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005501class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
5502public:
Chris Lattner2b037972010-07-29 02:01:43 +00005503 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5504 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00005505
John McCall3480ef22011-08-30 01:42:09 +00005506 const ARMABIInfo &getABIInfo() const {
5507 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
5508 }
5509
Craig Topper4f12f102014-03-12 06:41:41 +00005510 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
John McCallbeec5a02010-03-06 00:35:14 +00005511 return 13;
5512 }
Roman Divackyc1617352011-05-18 19:36:54 +00005513
Craig Topper4f12f102014-03-12 06:41:41 +00005514 StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
Oliver Stannard7f188642017-08-21 09:54:46 +00005515 return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
John McCall31168b02011-06-15 23:02:42 +00005516 }
5517
Roman Divackyc1617352011-05-18 19:36:54 +00005518 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00005519 llvm::Value *Address) const override {
Chris Lattnerece04092012-02-07 00:39:47 +00005520 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divackyc1617352011-05-18 19:36:54 +00005521
5522 // 0-15 are the 16 integer registers.
Chris Lattnerece04092012-02-07 00:39:47 +00005523 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divackyc1617352011-05-18 19:36:54 +00005524 return false;
5525 }
John McCall3480ef22011-08-30 01:42:09 +00005526
Craig Topper4f12f102014-03-12 06:41:41 +00005527 unsigned getSizeOfUnwindException() const override {
John McCall3480ef22011-08-30 01:42:09 +00005528 if (getABIInfo().isEABI()) return 88;
5529 return TargetCodeGenInfo::getSizeOfUnwindException();
5530 }
Tim Northovera484bc02013-10-01 14:34:25 +00005531
Eric Christopher162c91c2015-06-05 22:03:00 +00005532 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005533 CodeGen::CodeGenModule &CGM,
5534 ForDefinition_t IsForDefinition) const override {
5535 if (!IsForDefinition)
5536 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00005537 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Tim Northovera484bc02013-10-01 14:34:25 +00005538 if (!FD)
5539 return;
5540
5541 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
5542 if (!Attr)
5543 return;
5544
5545 const char *Kind;
5546 switch (Attr->getInterrupt()) {
5547 case ARMInterruptAttr::Generic: Kind = ""; break;
5548 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break;
5549 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break;
5550 case ARMInterruptAttr::SWI: Kind = "SWI"; break;
5551 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break;
5552 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break;
5553 }
5554
5555 llvm::Function *Fn = cast<llvm::Function>(GV);
5556
5557 Fn->addFnAttr("interrupt", Kind);
5558
Tim Northover5627d392015-10-30 16:30:45 +00005559 ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
5560 if (ABI == ARMABIInfo::APCS)
Tim Northovera484bc02013-10-01 14:34:25 +00005561 return;
5562
5563 // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
5564 // however this is not necessarily true on taking any interrupt. Instruct
5565 // the backend to perform a realignment as part of the function prologue.
5566 llvm::AttrBuilder B;
5567 B.addStackAlignmentAttr(8);
Reid Kleckneree4930b2017-05-02 22:07:37 +00005568 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
Tim Northovera484bc02013-10-01 14:34:25 +00005569 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00005570};
5571
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005572class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005573public:
5574 WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5575 : ARMTargetCodeGenInfo(CGT, K) {}
5576
Eric Christopher162c91c2015-06-05 22:03:00 +00005577 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005578 CodeGen::CodeGenModule &CGM,
5579 ForDefinition_t IsForDefinition) const override;
Saleem Abdulrasool6e9e88b2016-06-23 13:45:33 +00005580
5581 void getDependentLibraryOption(llvm::StringRef Lib,
5582 llvm::SmallString<24> &Opt) const override {
5583 Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5584 }
5585
5586 void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5587 llvm::SmallString<32> &Opt) const override {
5588 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5589 }
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005590};
5591
Eric Christopher162c91c2015-06-05 22:03:00 +00005592void WindowsARMTargetCodeGenInfo::setTargetAttributes(
Simon Atanasyan1a116db2017-07-20 20:34:18 +00005593 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM,
5594 ForDefinition_t IsForDefinition) const {
5595 ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM, IsForDefinition);
5596 if (!IsForDefinition)
5597 return;
Saleem Abdulrasool71d1dd12015-01-30 23:29:19 +00005598 addStackProbeSizeTargetAttribute(D, GV, CGM);
5599}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005600}
Daniel Dunbard59655c2009-09-12 00:59:49 +00005601
Chris Lattner22326a12010-07-29 02:31:05 +00005602void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Tim Northoverbc784d12015-02-24 17:22:40 +00005603 if (!getCXXABI().classifyReturnType(FI))
Eric Christopher7565e0d2015-05-29 23:09:49 +00005604 FI.getReturnInfo() =
5605 classifyReturnType(FI.getReturnType(), FI.isVariadic());
Oliver Stannard405bded2014-02-11 09:25:50 +00005606
Tim Northoverbc784d12015-02-24 17:22:40 +00005607 for (auto &I : FI.arguments())
5608 I.info = classifyArgumentType(I.type, FI.isVariadic());
Daniel Dunbar020daa92009-09-12 01:00:39 +00005609
Anton Korobeynikov231e8752011-04-14 20:06:49 +00005610 // Always honor user-specified calling convention.
5611 if (FI.getCallingConvention() != llvm::CallingConv::C)
5612 return;
5613
John McCall882987f2013-02-28 19:01:20 +00005614 llvm::CallingConv::ID cc = getRuntimeCC();
5615 if (cc != llvm::CallingConv::C)
Tim Northoverbc784d12015-02-24 17:22:40 +00005616 FI.setEffectiveCallingConvention(cc);
John McCall882987f2013-02-28 19:01:20 +00005617}
Rafael Espindolaa92c4422010-06-16 16:13:39 +00005618
John McCall882987f2013-02-28 19:01:20 +00005619/// Return the default calling convention that LLVM will use.
5620llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
5621 // The default calling convention that LLVM will infer.
Tim Northoverd88ecb32016-01-27 19:32:40 +00005622 if (isEABIHF() || getTarget().getTriple().isWatchABI())
John McCall882987f2013-02-28 19:01:20 +00005623 return llvm::CallingConv::ARM_AAPCS_VFP;
5624 else if (isEABI())
5625 return llvm::CallingConv::ARM_AAPCS;
5626 else
5627 return llvm::CallingConv::ARM_APCS;
5628}
5629
5630/// Return the calling convention that our ABI would like us to use
5631/// as the C calling convention.
5632llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
Daniel Dunbar020daa92009-09-12 01:00:39 +00005633 switch (getABIKind()) {
John McCall882987f2013-02-28 19:01:20 +00005634 case APCS: return llvm::CallingConv::ARM_APCS;
5635 case AAPCS: return llvm::CallingConv::ARM_AAPCS;
5636 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Tim Northover5627d392015-10-30 16:30:45 +00005637 case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Daniel Dunbar020daa92009-09-12 01:00:39 +00005638 }
John McCall882987f2013-02-28 19:01:20 +00005639 llvm_unreachable("bad ABI kind");
5640}
5641
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005642void ARMABIInfo::setCCs() {
John McCall882987f2013-02-28 19:01:20 +00005643 assert(getRuntimeCC() == llvm::CallingConv::C);
5644
5645 // Don't muddy up the IR with a ton of explicit annotations if
5646 // they'd just match what LLVM will infer from the triple.
5647 llvm::CallingConv::ID abiCC = getABIDefaultCC();
5648 if (abiCC != getLLVMDefaultCC())
5649 RuntimeCC = abiCC;
Anton Korobeynikovd90dd792014-12-02 16:04:58 +00005650
Tim Northover5627d392015-10-30 16:30:45 +00005651 // AAPCS apparently requires runtime support functions to be soft-float, but
5652 // that's almost certainly for historic reasons (Thumb1 not supporting VFP
5653 // most likely). It's more convenient for AAPCS16_VFP to be hard-float.
Peter Smith32e26752017-07-27 10:43:53 +00005654
5655 // The Run-time ABI for the ARM Architecture section 4.1.2 requires
5656 // AEABI-complying FP helper functions to use the base AAPCS.
5657 // These AEABI functions are expanded in the ARM llvm backend, all the builtin
5658 // support functions emitted by clang such as the _Complex helpers follow the
5659 // abiCC.
5660 if (abiCC != getLLVMDefaultCC())
Tim Northover5627d392015-10-30 16:30:45 +00005661 BuiltinCC = abiCC;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005662}
5663
Tim Northoverbc784d12015-02-24 17:22:40 +00005664ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
5665 bool isVariadic) const {
Manman Ren2a523d82012-10-30 23:21:41 +00005666 // 6.1.2.1 The following argument types are VFP CPRCs:
5667 // A single-precision floating-point type (including promoted
5668 // half-precision types); A double-precision floating-point type;
5669 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
5670 // with a Base Type of a single- or double-precision floating-point type,
5671 // 64-bit containerized vectors or 128-bit containerized vectors with one
5672 // to four Elements.
Tim Northover5a1558e2014-11-07 22:30:50 +00005673 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005674
Reid Klecknerb1be6832014-11-15 01:41:41 +00005675 Ty = useFirstFieldIfTransparentUnion(Ty);
5676
Manman Renfef9e312012-10-16 19:18:39 +00005677 // Handle illegal vector types here.
5678 if (isIllegalVectorType(Ty)) {
5679 uint64_t Size = getContext().getTypeSize(Ty);
5680 if (Size <= 32) {
5681 llvm::Type *ResType =
5682 llvm::Type::getInt32Ty(getVMContext());
Tim Northover5a1558e2014-11-07 22:30:50 +00005683 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005684 }
5685 if (Size == 64) {
5686 llvm::Type *ResType = llvm::VectorType::get(
5687 llvm::Type::getInt32Ty(getVMContext()), 2);
Tim Northover5a1558e2014-11-07 22:30:50 +00005688 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005689 }
5690 if (Size == 128) {
5691 llvm::Type *ResType = llvm::VectorType::get(
5692 llvm::Type::getInt32Ty(getVMContext()), 4);
Tim Northover5a1558e2014-11-07 22:30:50 +00005693 return ABIArgInfo::getDirect(ResType);
Manman Renfef9e312012-10-16 19:18:39 +00005694 }
John McCall7f416cc2015-09-08 08:05:57 +00005695 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Manman Renfef9e312012-10-16 19:18:39 +00005696 }
5697
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005698 // __fp16 gets passed as if it were an int or float, but with the top 16 bits
5699 // unspecified. This is not done for OpenCL as it handles the half type
5700 // natively, and does not need to interwork with AAPCS code.
Pirama Arumuga Nainar8e2e9d62016-03-18 16:58:36 +00005701 if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005702 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5703 llvm::Type::getFloatTy(getVMContext()) :
5704 llvm::Type::getInt32Ty(getVMContext());
5705 return ABIArgInfo::getDirect(ResType);
5706 }
5707
John McCalla1dee5302010-08-22 10:59:02 +00005708 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005709 // Treat an enum type as its underlying type.
Oliver Stannard405bded2014-02-11 09:25:50 +00005710 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005711 Ty = EnumTy->getDecl()->getIntegerType();
Oliver Stannard405bded2014-02-11 09:25:50 +00005712 }
Douglas Gregora71cc152010-02-02 20:10:50 +00005713
Tim Northover5a1558e2014-11-07 22:30:50 +00005714 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5715 : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00005716 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005717
Oliver Stannard405bded2014-02-11 09:25:50 +00005718 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
John McCall7f416cc2015-09-08 08:05:57 +00005719 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Oliver Stannard405bded2014-02-11 09:25:50 +00005720 }
Tim Northover1060eae2013-06-21 22:49:34 +00005721
Daniel Dunbar09d33622009-09-14 21:54:03 +00005722 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005723 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00005724 return ABIArgInfo::getIgnore();
5725
Tim Northover5a1558e2014-11-07 22:30:50 +00005726 if (IsEffectivelyAAPCS_VFP) {
Manman Ren2a523d82012-10-30 23:21:41 +00005727 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
5728 // into VFP registers.
Craig Topper8a13c412014-05-21 05:09:00 +00005729 const Type *Base = nullptr;
Manman Ren2a523d82012-10-30 23:21:41 +00005730 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005731 if (isHomogeneousAggregate(Ty, Base, Members)) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005732 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Ren2a523d82012-10-30 23:21:41 +00005733 // Base can be a floating-point or a vector.
Tim Northover5a1558e2014-11-07 22:30:50 +00005734 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005735 }
Tim Northover5627d392015-10-30 16:30:45 +00005736 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5737 // WatchOS does have homogeneous aggregates. Note that we intentionally use
5738 // this convention even for a variadic function: the backend will use GPRs
5739 // if needed.
5740 const Type *Base = nullptr;
5741 uint64_t Members = 0;
5742 if (isHomogeneousAggregate(Ty, Base, Members)) {
5743 assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
5744 llvm::Type *Ty =
5745 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
5746 return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
5747 }
5748 }
5749
5750 if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5751 getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
5752 // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
5753 // bigger than 128-bits, they get placed in space allocated by the caller,
5754 // and a pointer is passed.
5755 return ABIArgInfo::getIndirect(
5756 CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
Bob Wilsone826a2a2011-08-03 05:58:22 +00005757 }
5758
Manman Ren6c30e132012-08-13 21:23:55 +00005759 // Support byval for ARM.
Manman Ren77b02382012-11-06 19:05:29 +00005760 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
5761 // most 8-byte. We realign the indirect argument if type alignment is bigger
5762 // than ABI alignment.
Manman Ren505d68f2012-11-05 22:42:46 +00005763 uint64_t ABIAlign = 4;
5764 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
5765 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
Tim Northoverd157e192015-03-09 21:40:42 +00005766 getABIKind() == ARMABIInfo::AAPCS)
Manman Ren505d68f2012-11-05 22:42:46 +00005767 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Tim Northoverd157e192015-03-09 21:40:42 +00005768
Manman Ren8cd99812012-11-06 04:58:01 +00005769 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
Tim Northover5627d392015-10-30 16:30:45 +00005770 assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
John McCall7f416cc2015-09-08 08:05:57 +00005771 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5772 /*ByVal=*/true,
5773 /*Realign=*/TyAlign > ABIAlign);
Eli Friedmane66abda2012-08-09 00:31:40 +00005774 }
5775
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00005776 // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
5777 // same size and alignment.
5778 if (getTarget().isRenderScriptTarget()) {
5779 return coerceToIntArray(Ty, getContext(), getVMContext());
5780 }
5781
Daniel Dunbarb34b0802010-09-23 01:54:28 +00005782 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2192fe52011-07-18 04:24:23 +00005783 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005784 unsigned SizeRegs;
Eli Friedmane66abda2012-08-09 00:31:40 +00005785 // FIXME: Try to match the types of the arguments more accurately where
5786 // we can.
5787 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson8e2b75d2011-08-01 23:39:04 +00005788 ElemTy = llvm::Type::getInt32Ty(getVMContext());
5789 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren6fdb1582012-06-25 22:04:00 +00005790 } else {
Manman Ren6fdb1582012-06-25 22:04:00 +00005791 ElemTy = llvm::Type::getInt64Ty(getVMContext());
5792 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00005793 }
Stuart Hastings4b214952011-04-28 18:16:06 +00005794
Tim Northover5a1558e2014-11-07 22:30:50 +00005795 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005796}
5797
Chris Lattner458b2aa2010-07-29 02:16:43 +00005798static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005799 llvm::LLVMContext &VMContext) {
5800 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
5801 // is called integer-like if its size is less than or equal to one word, and
5802 // the offset of each of its addressable sub-fields is zero.
5803
5804 uint64_t Size = Context.getTypeSize(Ty);
5805
5806 // Check that the type fits in a word.
5807 if (Size > 32)
5808 return false;
5809
5810 // FIXME: Handle vector types!
5811 if (Ty->isVectorType())
5812 return false;
5813
Daniel Dunbard53bac72009-09-14 02:20:34 +00005814 // Float types are never treated as "integer like".
5815 if (Ty->isRealFloatingType())
5816 return false;
5817
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005818 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00005819 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005820 return true;
5821
Daniel Dunbar96ebba52010-02-01 23:31:26 +00005822 // Small complex integer types are "integer like".
5823 if (const ComplexType *CT = Ty->getAs<ComplexType>())
5824 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005825
5826 // Single element and zero sized arrays should be allowed, by the definition
5827 // above, but they are not.
5828
5829 // Otherwise, it must be a record type.
5830 const RecordType *RT = Ty->getAs<RecordType>();
5831 if (!RT) return false;
5832
5833 // Ignore records with flexible arrays.
5834 const RecordDecl *RD = RT->getDecl();
5835 if (RD->hasFlexibleArrayMember())
5836 return false;
5837
5838 // Check that all sub-fields are at offset 0, and are themselves "integer
5839 // like".
5840 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
5841
5842 bool HadField = false;
5843 unsigned idx = 0;
5844 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
5845 i != e; ++i, ++idx) {
David Blaikie40ed2972012-06-06 20:45:41 +00005846 const FieldDecl *FD = *i;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005847
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005848 // Bit-fields are not addressable, we only need to verify they are "integer
5849 // like". We still have to disallow a subsequent non-bitfield, for example:
5850 // struct { int : 0; int x }
5851 // is non-integer like according to gcc.
5852 if (FD->isBitField()) {
5853 if (!RD->isUnion())
5854 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005855
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005856 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5857 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005858
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005859 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005860 }
5861
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005862 // Check if this field is at offset 0.
5863 if (Layout.getFieldOffset(idx) != 0)
5864 return false;
5865
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005866 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5867 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00005868
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00005869 // Only allow at most one field in a structure. This doesn't match the
5870 // wording above, but follows gcc in situations with a field following an
5871 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005872 if (!RD->isUnion()) {
5873 if (HadField)
5874 return false;
5875
5876 HadField = true;
5877 }
5878 }
5879
5880 return true;
5881}
5882
Oliver Stannard405bded2014-02-11 09:25:50 +00005883ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
5884 bool isVariadic) const {
Tim Northover5627d392015-10-30 16:30:45 +00005885 bool IsEffectivelyAAPCS_VFP =
5886 (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic;
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005887
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005888 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005889 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005890
Daniel Dunbar19964db2010-09-23 01:54:32 +00005891 // Large vector types should be returned via memory.
Oliver Stannard405bded2014-02-11 09:25:50 +00005892 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) {
John McCall7f416cc2015-09-08 08:05:57 +00005893 return getNaturalAlignIndirect(RetTy);
Oliver Stannard405bded2014-02-11 09:25:50 +00005894 }
Daniel Dunbar19964db2010-09-23 01:54:32 +00005895
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005896 // __fp16 gets returned as if it were an int or float, but with the top 16
5897 // bits unspecified. This is not done for OpenCL as it handles the half type
5898 // natively, and does not need to interwork with AAPCS code.
Pirama Arumuga Nainar8e2e9d62016-03-18 16:58:36 +00005899 if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) {
Oliver Stannarddc2854c2015-09-03 12:40:58 +00005900 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5901 llvm::Type::getFloatTy(getVMContext()) :
5902 llvm::Type::getInt32Ty(getVMContext());
5903 return ABIArgInfo::getDirect(ResType);
5904 }
5905
John McCalla1dee5302010-08-22 10:59:02 +00005906 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00005907 // Treat an enum type as its underlying type.
5908 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5909 RetTy = EnumTy->getDecl()->getIntegerType();
5910
Tim Northover5a1558e2014-11-07 22:30:50 +00005911 return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5912 : ABIArgInfo::getDirect();
Douglas Gregora71cc152010-02-02 20:10:50 +00005913 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005914
5915 // Are we following APCS?
5916 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00005917 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005918 return ABIArgInfo::getIgnore();
5919
Daniel Dunbareedf1512010-02-01 23:31:19 +00005920 // Complex types are all returned as packed integers.
5921 //
5922 // FIXME: Consider using 2 x vector types if the back end handles them
5923 // correctly.
5924 if (RetTy->isAnyComplexType())
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00005925 return ABIArgInfo::getDirect(llvm::IntegerType::get(
5926 getVMContext(), getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00005927
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005928 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005929 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005930 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005931 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005932 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00005933 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005934 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00005935 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5936 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005937 }
5938
5939 // Otherwise return in memory.
John McCall7f416cc2015-09-08 08:05:57 +00005940 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005941 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005942
5943 // Otherwise this is an AAPCS variant.
5944
Chris Lattner458b2aa2010-07-29 02:16:43 +00005945 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005946 return ABIArgInfo::getIgnore();
5947
Bob Wilson1d9269a2011-11-02 04:51:36 +00005948 // Check for homogeneous aggregates with AAPCS-VFP.
Tim Northover5a1558e2014-11-07 22:30:50 +00005949 if (IsEffectivelyAAPCS_VFP) {
Craig Topper8a13c412014-05-21 05:09:00 +00005950 const Type *Base = nullptr;
Tim Northover5627d392015-10-30 16:30:45 +00005951 uint64_t Members = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +00005952 if (isHomogeneousAggregate(RetTy, Base, Members)) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005953 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson1d9269a2011-11-02 04:51:36 +00005954 // Homogeneous Aggregates are returned directly.
Tim Northover5a1558e2014-11-07 22:30:50 +00005955 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00005956 }
Bob Wilson1d9269a2011-11-02 04:51:36 +00005957 }
5958
Daniel Dunbar626f1d82009-09-13 08:03:58 +00005959 // Aggregates <= 4 bytes are returned in r0; other aggregates
5960 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00005961 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005962 if (Size <= 32) {
Pirama Arumuga Nainarbb846a32016-07-27 19:01:51 +00005963 // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
5964 // same size and alignment.
5965 if (getTarget().isRenderScriptTarget()) {
5966 return coerceToIntArray(RetTy, getContext(), getVMContext());
5967 }
Christian Pirkerc3d32172014-07-03 09:28:12 +00005968 if (getDataLayout().isBigEndian())
5969 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
Tim Northover5a1558e2014-11-07 22:30:50 +00005970 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Christian Pirkerc3d32172014-07-03 09:28:12 +00005971
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005972 // Return in the smallest viable integer type.
5973 if (Size <= 8)
Tim Northover5a1558e2014-11-07 22:30:50 +00005974 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005975 if (Size <= 16)
Tim Northover5a1558e2014-11-07 22:30:50 +00005976 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5977 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Tim Northover5627d392015-10-30 16:30:45 +00005978 } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
5979 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
5980 llvm::Type *CoerceTy =
Rui Ueyama83aa9792016-01-14 21:00:27 +00005981 llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
Tim Northover5627d392015-10-30 16:30:45 +00005982 return ABIArgInfo::getDirect(CoerceTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00005983 }
5984
John McCall7f416cc2015-09-08 08:05:57 +00005985 return getNaturalAlignIndirect(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00005986}
5987
Manman Renfef9e312012-10-16 19:18:39 +00005988/// isIllegalVector - check whether Ty is an illegal vector type.
5989bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
Stephen Hines8267e7d2015-12-04 01:39:30 +00005990 if (const VectorType *VT = Ty->getAs<VectorType> ()) {
5991 if (isAndroid()) {
5992 // Android shipped using Clang 3.1, which supported a slightly different
5993 // vector ABI. The primary differences were that 3-element vector types
5994 // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
5995 // accepts that legacy behavior for Android only.
5996 // Check whether VT is legal.
5997 unsigned NumElements = VT->getNumElements();
5998 // NumElements should be power of 2 or equal to 3.
5999 if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
6000 return true;
6001 } else {
6002 // Check whether VT is legal.
6003 unsigned NumElements = VT->getNumElements();
6004 uint64_t Size = getContext().getTypeSize(VT);
6005 // NumElements should be power of 2.
6006 if (!llvm::isPowerOf2_32(NumElements))
6007 return true;
6008 // Size should be greater than 32 bits.
6009 return Size <= 32;
6010 }
Manman Renfef9e312012-10-16 19:18:39 +00006011 }
6012 return false;
6013}
6014
Arnold Schwaighofer634e3202017-05-26 18:11:54 +00006015bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
6016 llvm::Type *eltTy,
6017 unsigned numElts) const {
6018 if (!llvm::isPowerOf2_32(numElts))
6019 return false;
6020 unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy);
6021 if (size > 64)
6022 return false;
6023 if (vectorSize.getQuantity() != 8 &&
6024 (vectorSize.getQuantity() != 16 || numElts == 1))
6025 return false;
6026 return true;
6027}
6028
Reid Klecknere9f6a712014-10-31 17:10:41 +00006029bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
6030 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
6031 // double, or 64-bit or 128-bit vectors.
6032 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
6033 if (BT->getKind() == BuiltinType::Float ||
6034 BT->getKind() == BuiltinType::Double ||
6035 BT->getKind() == BuiltinType::LongDouble)
6036 return true;
6037 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
6038 unsigned VecSize = getContext().getTypeSize(VT);
6039 if (VecSize == 64 || VecSize == 128)
6040 return true;
6041 }
6042 return false;
6043}
6044
6045bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
6046 uint64_t Members) const {
6047 return Members <= 4;
6048}
6049
John McCall7f416cc2015-09-08 08:05:57 +00006050Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6051 QualType Ty) const {
6052 CharUnits SlotSize = CharUnits::fromQuantity(4);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00006053
John McCall7f416cc2015-09-08 08:05:57 +00006054 // Empty records are ignored for parameter passing purposes.
Tim Northover1711cc92013-06-21 23:05:33 +00006055 if (isEmptyRecord(getContext(), Ty, true)) {
John McCall7f416cc2015-09-08 08:05:57 +00006056 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
6057 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6058 return Addr;
Tim Northover1711cc92013-06-21 23:05:33 +00006059 }
6060
John McCall7f416cc2015-09-08 08:05:57 +00006061 auto TyInfo = getContext().getTypeInfoInChars(Ty);
6062 CharUnits TyAlignForABI = TyInfo.second;
Manman Rencca54d02012-10-16 19:01:37 +00006063
John McCall7f416cc2015-09-08 08:05:57 +00006064 // Use indirect if size of the illegal vector is bigger than 16 bytes.
6065 bool IsIndirect = false;
Tim Northover5627d392015-10-30 16:30:45 +00006066 const Type *Base = nullptr;
6067 uint64_t Members = 0;
John McCall7f416cc2015-09-08 08:05:57 +00006068 if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
6069 IsIndirect = true;
6070
Tim Northover5627d392015-10-30 16:30:45 +00006071 // ARMv7k passes structs bigger than 16 bytes indirectly, in space
6072 // allocated by the caller.
6073 } else if (TyInfo.first > CharUnits::fromQuantity(16) &&
6074 getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6075 !isHomogeneousAggregate(Ty, Base, Members)) {
6076 IsIndirect = true;
6077
John McCall7f416cc2015-09-08 08:05:57 +00006078 // Otherwise, bound the type's ABI alignment.
Manman Rencca54d02012-10-16 19:01:37 +00006079 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
6080 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
John McCall7f416cc2015-09-08 08:05:57 +00006081 // Our callers should be prepared to handle an under-aligned address.
6082 } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6083 getABIKind() == ARMABIInfo::AAPCS) {
6084 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6085 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
Tim Northover4c5cb9c2015-11-02 19:32:23 +00006086 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6087 // ARMv7k allows type alignment up to 16 bytes.
6088 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6089 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
John McCall7f416cc2015-09-08 08:05:57 +00006090 } else {
6091 TyAlignForABI = CharUnits::fromQuantity(4);
Manman Renfef9e312012-10-16 19:18:39 +00006092 }
John McCall7f416cc2015-09-08 08:05:57 +00006093 TyInfo.second = TyAlignForABI;
Manman Rencca54d02012-10-16 19:01:37 +00006094
John McCall7f416cc2015-09-08 08:05:57 +00006095 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
6096 SlotSize, /*AllowHigherAlign*/ true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00006097}
6098
Chris Lattner0cf24192010-06-28 20:05:43 +00006099//===----------------------------------------------------------------------===//
Justin Holewinski83e96682012-05-24 17:43:12 +00006100// NVPTX ABI Implementation
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006101//===----------------------------------------------------------------------===//
6102
6103namespace {
6104
Justin Holewinski83e96682012-05-24 17:43:12 +00006105class NVPTXABIInfo : public ABIInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006106public:
Justin Holewinski36837432013-03-30 14:38:24 +00006107 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006108
6109 ABIArgInfo classifyReturnType(QualType RetTy) const;
6110 ABIArgInfo classifyArgumentType(QualType Ty) const;
6111
Craig Topper4f12f102014-03-12 06:41:41 +00006112 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00006113 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6114 QualType Ty) const override;
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006115};
6116
Justin Holewinski83e96682012-05-24 17:43:12 +00006117class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006118public:
Justin Holewinski83e96682012-05-24 17:43:12 +00006119 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
6120 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Craig Topper4f12f102014-03-12 06:41:41 +00006121
Eric Christopher162c91c2015-06-05 22:03:00 +00006122 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006123 CodeGen::CodeGenModule &M,
6124 ForDefinition_t IsForDefinition) const override;
6125
Justin Holewinski36837432013-03-30 14:38:24 +00006126private:
Eli Benderskye06a2c42014-04-15 16:57:05 +00006127 // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
6128 // resulting MDNode to the nvvm.annotations MDNode.
6129 static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006130};
6131
Justin Holewinski83e96682012-05-24 17:43:12 +00006132ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006133 if (RetTy->isVoidType())
6134 return ABIArgInfo::getIgnore();
Justin Holewinskif9329ff2013-11-20 20:35:34 +00006135
6136 // note: this is different from default ABI
6137 if (!RetTy->isScalarType())
6138 return ABIArgInfo::getDirect();
6139
6140 // Treat an enum type as its underlying type.
6141 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6142 RetTy = EnumTy->getDecl()->getIntegerType();
6143
6144 return (RetTy->isPromotableIntegerType() ?
6145 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006146}
6147
Justin Holewinski83e96682012-05-24 17:43:12 +00006148ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinskif9329ff2013-11-20 20:35:34 +00006149 // Treat an enum type as its underlying type.
6150 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6151 Ty = EnumTy->getDecl()->getIntegerType();
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006152
Eli Bendersky95338a02014-10-29 13:43:21 +00006153 // Return aggregates type as indirect by value
6154 if (isAggregateTypeForABI(Ty))
John McCall7f416cc2015-09-08 08:05:57 +00006155 return getNaturalAlignIndirect(Ty, /* byval */ true);
Eli Bendersky95338a02014-10-29 13:43:21 +00006156
Justin Holewinskif9329ff2013-11-20 20:35:34 +00006157 return (Ty->isPromotableIntegerType() ?
6158 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006159}
6160
Justin Holewinski83e96682012-05-24 17:43:12 +00006161void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +00006162 if (!getCXXABI().classifyReturnType(FI))
6163 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006164 for (auto &I : FI.arguments())
6165 I.info = classifyArgumentType(I.type);
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006166
6167 // Always honor user-specified calling convention.
6168 if (FI.getCallingConvention() != llvm::CallingConv::C)
6169 return;
6170
John McCall882987f2013-02-28 19:01:20 +00006171 FI.setEffectiveCallingConvention(getRuntimeCC());
6172}
6173
John McCall7f416cc2015-09-08 08:05:57 +00006174Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6175 QualType Ty) const {
Justin Holewinski83e96682012-05-24 17:43:12 +00006176 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006177}
6178
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006179void NVPTXTargetCodeGenInfo::setTargetAttributes(
6180 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
6181 ForDefinition_t IsForDefinition) const {
6182 if (!IsForDefinition)
6183 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006184 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Justin Holewinski38031972011-10-05 17:58:44 +00006185 if (!FD) return;
6186
6187 llvm::Function *F = cast<llvm::Function>(GV);
6188
6189 // Perform special handling in OpenCL mode
David Blaikiebbafb8a2012-03-11 07:00:24 +00006190 if (M.getLangOpts().OpenCL) {
Justin Holewinski36837432013-03-30 14:38:24 +00006191 // Use OpenCL function attributes to check for kernel functions
Justin Holewinski38031972011-10-05 17:58:44 +00006192 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00006193 if (FD->hasAttr<OpenCLKernelAttr>()) {
Justin Holewinski36837432013-03-30 14:38:24 +00006194 // OpenCL __kernel functions get kernel metadata
Eli Benderskye06a2c42014-04-15 16:57:05 +00006195 // Create !{<func-ref>, metadata !"kernel", i32 1} node
6196 addNVVMMetadata(F, "kernel", 1);
Justin Holewinski38031972011-10-05 17:58:44 +00006197 // And kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00006198 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski38031972011-10-05 17:58:44 +00006199 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00006200 }
Justin Holewinski38031972011-10-05 17:58:44 +00006201
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00006202 // Perform special handling in CUDA mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00006203 if (M.getLangOpts().CUDA) {
Justin Holewinski36837432013-03-30 14:38:24 +00006204 // CUDA __global__ functions get a kernel metadata entry. Since
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00006205 // __global__ functions cannot be called from the device, we do not
6206 // need to set the noinline attribute.
Eli Benderskye06a2c42014-04-15 16:57:05 +00006207 if (FD->hasAttr<CUDAGlobalAttr>()) {
6208 // Create !{<func-ref>, metadata !"kernel", i32 1} node
6209 addNVVMMetadata(F, "kernel", 1);
6210 }
Artem Belevich7093e402015-04-21 22:55:54 +00006211 if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
Eli Benderskye06a2c42014-04-15 16:57:05 +00006212 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
Artem Belevich7093e402015-04-21 22:55:54 +00006213 llvm::APSInt MaxThreads(32);
6214 MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
6215 if (MaxThreads > 0)
6216 addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
6217
6218 // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
6219 // not specified in __launch_bounds__ or if the user specified a 0 value,
6220 // we don't have to add a PTX directive.
6221 if (Attr->getMinBlocks()) {
6222 llvm::APSInt MinBlocks(32);
6223 MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
6224 if (MinBlocks > 0)
6225 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
6226 addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
Eli Benderskye06a2c42014-04-15 16:57:05 +00006227 }
6228 }
Justin Holewinski38031972011-10-05 17:58:44 +00006229 }
6230}
6231
Eli Benderskye06a2c42014-04-15 16:57:05 +00006232void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
6233 int Operand) {
Justin Holewinski36837432013-03-30 14:38:24 +00006234 llvm::Module *M = F->getParent();
6235 llvm::LLVMContext &Ctx = M->getContext();
6236
6237 // Get "nvvm.annotations" metadata node
6238 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
6239
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00006240 llvm::Metadata *MDVals[] = {
6241 llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
6242 llvm::ConstantAsMetadata::get(
6243 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
Justin Holewinski36837432013-03-30 14:38:24 +00006244 // Append metadata to nvvm.annotations
6245 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
6246}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006247}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00006248
6249//===----------------------------------------------------------------------===//
Ulrich Weigand47445072013-05-06 16:26:41 +00006250// SystemZ ABI Implementation
6251//===----------------------------------------------------------------------===//
6252
6253namespace {
6254
Bryan Chane3f1ed52016-04-28 13:56:43 +00006255class SystemZABIInfo : public SwiftABIInfo {
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006256 bool HasVector;
6257
Ulrich Weigand47445072013-05-06 16:26:41 +00006258public:
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006259 SystemZABIInfo(CodeGenTypes &CGT, bool HV)
Bryan Chane3f1ed52016-04-28 13:56:43 +00006260 : SwiftABIInfo(CGT), HasVector(HV) {}
Ulrich Weigand47445072013-05-06 16:26:41 +00006261
6262 bool isPromotableIntegerType(QualType Ty) const;
6263 bool isCompoundType(QualType Ty) const;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006264 bool isVectorArgumentType(QualType Ty) const;
Ulrich Weigand47445072013-05-06 16:26:41 +00006265 bool isFPArgumentType(QualType Ty) const;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006266 QualType GetSingleElementType(QualType Ty) const;
Ulrich Weigand47445072013-05-06 16:26:41 +00006267
6268 ABIArgInfo classifyReturnType(QualType RetTy) const;
6269 ABIArgInfo classifyArgumentType(QualType ArgTy) const;
6270
Craig Topper4f12f102014-03-12 06:41:41 +00006271 void computeInfo(CGFunctionInfo &FI) const override {
Reid Kleckner40ca9132014-05-13 22:05:45 +00006272 if (!getCXXABI().classifyReturnType(FI))
6273 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006274 for (auto &I : FI.arguments())
6275 I.info = classifyArgumentType(I.type);
Ulrich Weigand47445072013-05-06 16:26:41 +00006276 }
6277
John McCall7f416cc2015-09-08 08:05:57 +00006278 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6279 QualType Ty) const override;
Bryan Chane3f1ed52016-04-28 13:56:43 +00006280
6281 bool shouldPassIndirectlyForSwift(CharUnits totalSize,
6282 ArrayRef<llvm::Type*> scalars,
6283 bool asReturnValue) const override {
6284 return occupiesMoreThan(CGT, scalars, /*total*/ 4);
6285 }
Arnold Schwaighoferb0f2c332016-12-01 18:07:38 +00006286 bool isSwiftErrorInRegister() const override {
6287 return true;
6288 }
Ulrich Weigand47445072013-05-06 16:26:41 +00006289};
6290
6291class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
6292public:
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006293 SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
6294 : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
Ulrich Weigand47445072013-05-06 16:26:41 +00006295};
6296
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006297}
Ulrich Weigand47445072013-05-06 16:26:41 +00006298
6299bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
6300 // Treat an enum type as its underlying type.
6301 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6302 Ty = EnumTy->getDecl()->getIntegerType();
6303
6304 // Promotable integer types are required to be promoted by the ABI.
6305 if (Ty->isPromotableIntegerType())
6306 return true;
6307
6308 // 32-bit values must also be promoted.
6309 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
6310 switch (BT->getKind()) {
6311 case BuiltinType::Int:
6312 case BuiltinType::UInt:
6313 return true;
6314 default:
6315 return false;
6316 }
6317 return false;
6318}
6319
6320bool SystemZABIInfo::isCompoundType(QualType Ty) const {
Ulrich Weigand759449c2015-03-30 13:49:01 +00006321 return (Ty->isAnyComplexType() ||
6322 Ty->isVectorType() ||
6323 isAggregateTypeForABI(Ty));
Ulrich Weigand47445072013-05-06 16:26:41 +00006324}
6325
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006326bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
6327 return (HasVector &&
6328 Ty->isVectorType() &&
6329 getContext().getTypeSize(Ty) <= 128);
6330}
6331
Ulrich Weigand47445072013-05-06 16:26:41 +00006332bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
6333 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
6334 switch (BT->getKind()) {
6335 case BuiltinType::Float:
6336 case BuiltinType::Double:
6337 return true;
6338 default:
6339 return false;
6340 }
6341
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006342 return false;
6343}
6344
6345QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
Ulrich Weigand47445072013-05-06 16:26:41 +00006346 if (const RecordType *RT = Ty->getAsStructureType()) {
6347 const RecordDecl *RD = RT->getDecl();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006348 QualType Found;
Ulrich Weigand47445072013-05-06 16:26:41 +00006349
6350 // If this is a C++ record, check the bases first.
6351 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Aaron Ballman574705e2014-03-13 15:41:46 +00006352 for (const auto &I : CXXRD->bases()) {
6353 QualType Base = I.getType();
Ulrich Weigand47445072013-05-06 16:26:41 +00006354
6355 // Empty bases don't affect things either way.
6356 if (isEmptyRecord(getContext(), Base, true))
6357 continue;
6358
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006359 if (!Found.isNull())
6360 return Ty;
6361 Found = GetSingleElementType(Base);
Ulrich Weigand47445072013-05-06 16:26:41 +00006362 }
6363
6364 // Check the fields.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00006365 for (const auto *FD : RD->fields()) {
Ulrich Weigand759449c2015-03-30 13:49:01 +00006366 // For compatibility with GCC, ignore empty bitfields in C++ mode.
Ulrich Weigand47445072013-05-06 16:26:41 +00006367 // Unlike isSingleElementStruct(), empty structure and array fields
6368 // do count. So do anonymous bitfields that aren't zero-sized.
Ulrich Weigand759449c2015-03-30 13:49:01 +00006369 if (getContext().getLangOpts().CPlusPlus &&
6370 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
6371 continue;
Ulrich Weigand47445072013-05-06 16:26:41 +00006372
6373 // Unlike isSingleElementStruct(), arrays do not count.
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006374 // Nested structures still do though.
6375 if (!Found.isNull())
6376 return Ty;
6377 Found = GetSingleElementType(FD->getType());
Ulrich Weigand47445072013-05-06 16:26:41 +00006378 }
6379
6380 // Unlike isSingleElementStruct(), trailing padding is allowed.
6381 // An 8-byte aligned struct s { float f; } is passed as a double.
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006382 if (!Found.isNull())
6383 return Found;
Ulrich Weigand47445072013-05-06 16:26:41 +00006384 }
6385
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006386 return Ty;
Ulrich Weigand47445072013-05-06 16:26:41 +00006387}
6388
John McCall7f416cc2015-09-08 08:05:57 +00006389Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6390 QualType Ty) const {
Ulrich Weigand47445072013-05-06 16:26:41 +00006391 // Assume that va_list type is correct; should be pointer to LLVM type:
6392 // struct {
6393 // i64 __gpr;
6394 // i64 __fpr;
6395 // i8 *__overflow_arg_area;
6396 // i8 *__reg_save_area;
6397 // };
6398
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006399 // Every non-vector argument occupies 8 bytes and is passed by preference
6400 // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are
6401 // always passed on the stack.
John McCall7f416cc2015-09-08 08:05:57 +00006402 Ty = getContext().getCanonicalType(Ty);
6403 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Ulrich Weigand759449c2015-03-30 13:49:01 +00006404 llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00006405 llvm::Type *DirectTy = ArgTy;
Ulrich Weigand47445072013-05-06 16:26:41 +00006406 ABIArgInfo AI = classifyArgumentType(Ty);
Ulrich Weigand47445072013-05-06 16:26:41 +00006407 bool IsIndirect = AI.isIndirect();
Ulrich Weigand759449c2015-03-30 13:49:01 +00006408 bool InFPRs = false;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006409 bool IsVector = false;
John McCall7f416cc2015-09-08 08:05:57 +00006410 CharUnits UnpaddedSize;
6411 CharUnits DirectAlign;
Ulrich Weigand47445072013-05-06 16:26:41 +00006412 if (IsIndirect) {
John McCall7f416cc2015-09-08 08:05:57 +00006413 DirectTy = llvm::PointerType::getUnqual(DirectTy);
6414 UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
Ulrich Weigand759449c2015-03-30 13:49:01 +00006415 } else {
6416 if (AI.getCoerceToType())
6417 ArgTy = AI.getCoerceToType();
6418 InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006419 IsVector = ArgTy->isVectorTy();
John McCall7f416cc2015-09-08 08:05:57 +00006420 UnpaddedSize = TyInfo.first;
6421 DirectAlign = TyInfo.second;
Ulrich Weigand759449c2015-03-30 13:49:01 +00006422 }
John McCall7f416cc2015-09-08 08:05:57 +00006423 CharUnits PaddedSize = CharUnits::fromQuantity(8);
6424 if (IsVector && UnpaddedSize > PaddedSize)
6425 PaddedSize = CharUnits::fromQuantity(16);
6426 assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
Ulrich Weigand47445072013-05-06 16:26:41 +00006427
John McCall7f416cc2015-09-08 08:05:57 +00006428 CharUnits Padding = (PaddedSize - UnpaddedSize);
Ulrich Weigand47445072013-05-06 16:26:41 +00006429
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006430 llvm::Type *IndexTy = CGF.Int64Ty;
John McCall7f416cc2015-09-08 08:05:57 +00006431 llvm::Value *PaddedSizeV =
6432 llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006433
6434 if (IsVector) {
6435 // Work out the address of a vector argument on the stack.
6436 // Vector arguments are always passed in the high bits of a
6437 // single (8 byte) or double (16 byte) stack slot.
John McCall7f416cc2015-09-08 08:05:57 +00006438 Address OverflowArgAreaPtr =
6439 CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16),
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006440 "overflow_arg_area_ptr");
John McCall7f416cc2015-09-08 08:05:57 +00006441 Address OverflowArgArea =
6442 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6443 TyInfo.second);
6444 Address MemAddr =
6445 CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006446
6447 // Update overflow_arg_area_ptr pointer
6448 llvm::Value *NewOverflowArgArea =
John McCall7f416cc2015-09-08 08:05:57 +00006449 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6450 "overflow_arg_area");
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006451 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6452
6453 return MemAddr;
6454 }
6455
John McCall7f416cc2015-09-08 08:05:57 +00006456 assert(PaddedSize.getQuantity() == 8);
6457
6458 unsigned MaxRegs, RegCountField, RegSaveIndex;
6459 CharUnits RegPadding;
Ulrich Weigand47445072013-05-06 16:26:41 +00006460 if (InFPRs) {
6461 MaxRegs = 4; // Maximum of 4 FPR arguments
6462 RegCountField = 1; // __fpr
6463 RegSaveIndex = 16; // save offset for f0
John McCall7f416cc2015-09-08 08:05:57 +00006464 RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
Ulrich Weigand47445072013-05-06 16:26:41 +00006465 } else {
6466 MaxRegs = 5; // Maximum of 5 GPR arguments
6467 RegCountField = 0; // __gpr
6468 RegSaveIndex = 2; // save offset for r2
6469 RegPadding = Padding; // values are passed in the low bits of a GPR
6470 }
6471
John McCall7f416cc2015-09-08 08:05:57 +00006472 Address RegCountPtr = CGF.Builder.CreateStructGEP(
6473 VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8),
6474 "reg_count_ptr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006475 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
Ulrich Weigand47445072013-05-06 16:26:41 +00006476 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
6477 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
Oliver Stannard405bded2014-02-11 09:25:50 +00006478 "fits_in_regs");
Ulrich Weigand47445072013-05-06 16:26:41 +00006479
6480 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
6481 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
6482 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
6483 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
6484
6485 // Emit code to load the value if it was passed in registers.
6486 CGF.EmitBlock(InRegBlock);
6487
6488 // Work out the address of an argument register.
Ulrich Weigand47445072013-05-06 16:26:41 +00006489 llvm::Value *ScaledRegCount =
6490 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
6491 llvm::Value *RegBase =
John McCall7f416cc2015-09-08 08:05:57 +00006492 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
6493 + RegPadding.getQuantity());
Ulrich Weigand47445072013-05-06 16:26:41 +00006494 llvm::Value *RegOffset =
6495 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
John McCall7f416cc2015-09-08 08:05:57 +00006496 Address RegSaveAreaPtr =
6497 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
6498 "reg_save_area_ptr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006499 llvm::Value *RegSaveArea =
6500 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
John McCall7f416cc2015-09-08 08:05:57 +00006501 Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
6502 "raw_reg_addr"),
6503 PaddedSize);
6504 Address RegAddr =
6505 CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006506
6507 // Update the register count
6508 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
6509 llvm::Value *NewRegCount =
6510 CGF.Builder.CreateAdd(RegCount, One, "reg_count");
6511 CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
6512 CGF.EmitBranch(ContBlock);
6513
6514 // Emit code to load the value if it was passed in memory.
6515 CGF.EmitBlock(InMemBlock);
6516
6517 // Work out the address of a stack argument.
John McCall7f416cc2015-09-08 08:05:57 +00006518 Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP(
6519 VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr");
6520 Address OverflowArgArea =
6521 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6522 PaddedSize);
6523 Address RawMemAddr =
6524 CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
6525 Address MemAddr =
6526 CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006527
6528 // Update overflow_arg_area_ptr pointer
6529 llvm::Value *NewOverflowArgArea =
John McCall7f416cc2015-09-08 08:05:57 +00006530 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6531 "overflow_arg_area");
Ulrich Weigand47445072013-05-06 16:26:41 +00006532 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6533 CGF.EmitBranch(ContBlock);
6534
6535 // Return the appropriate result.
6536 CGF.EmitBlock(ContBlock);
John McCall7f416cc2015-09-08 08:05:57 +00006537 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
6538 MemAddr, InMemBlock, "va_arg.addr");
Ulrich Weigand47445072013-05-06 16:26:41 +00006539
6540 if (IsIndirect)
John McCall7f416cc2015-09-08 08:05:57 +00006541 ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
6542 TyInfo.second);
Ulrich Weigand47445072013-05-06 16:26:41 +00006543
6544 return ResAddr;
6545}
6546
Ulrich Weigand47445072013-05-06 16:26:41 +00006547ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
6548 if (RetTy->isVoidType())
6549 return ABIArgInfo::getIgnore();
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006550 if (isVectorArgumentType(RetTy))
6551 return ABIArgInfo::getDirect();
Ulrich Weigand47445072013-05-06 16:26:41 +00006552 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
John McCall7f416cc2015-09-08 08:05:57 +00006553 return getNaturalAlignIndirect(RetTy);
Ulrich Weigand47445072013-05-06 16:26:41 +00006554 return (isPromotableIntegerType(RetTy) ?
6555 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6556}
6557
6558ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
6559 // Handle the generic C++ ABI.
Mark Lacey3825e832013-10-06 01:33:34 +00006560 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00006561 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Ulrich Weigand47445072013-05-06 16:26:41 +00006562
6563 // Integers and enums are extended to full register width.
6564 if (isPromotableIntegerType(Ty))
6565 return ABIArgInfo::getExtend();
6566
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006567 // Handle vector types and vector-like structure types. Note that
6568 // as opposed to float-like structure types, we do not allow any
6569 // padding for vector-like structures, so verify the sizes match.
Ulrich Weigand47445072013-05-06 16:26:41 +00006570 uint64_t Size = getContext().getTypeSize(Ty);
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006571 QualType SingleElementTy = GetSingleElementType(Ty);
6572 if (isVectorArgumentType(SingleElementTy) &&
6573 getContext().getTypeSize(SingleElementTy) == Size)
6574 return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
6575
6576 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
Ulrich Weigand47445072013-05-06 16:26:41 +00006577 if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
John McCall7f416cc2015-09-08 08:05:57 +00006578 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00006579
6580 // Handle small structures.
6581 if (const RecordType *RT = Ty->getAs<RecordType>()) {
6582 // Structures with flexible arrays have variable length, so really
6583 // fail the size test above.
6584 const RecordDecl *RD = RT->getDecl();
6585 if (RD->hasFlexibleArrayMember())
John McCall7f416cc2015-09-08 08:05:57 +00006586 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00006587
6588 // The structure is passed as an unextended integer, a float, or a double.
6589 llvm::Type *PassTy;
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00006590 if (isFPArgumentType(SingleElementTy)) {
Ulrich Weigand47445072013-05-06 16:26:41 +00006591 assert(Size == 32 || Size == 64);
6592 if (Size == 32)
6593 PassTy = llvm::Type::getFloatTy(getVMContext());
6594 else
6595 PassTy = llvm::Type::getDoubleTy(getVMContext());
6596 } else
6597 PassTy = llvm::IntegerType::get(getVMContext(), Size);
6598 return ABIArgInfo::getDirect(PassTy);
6599 }
6600
6601 // Non-structure compounds are passed indirectly.
6602 if (isCompoundType(Ty))
John McCall7f416cc2015-09-08 08:05:57 +00006603 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Ulrich Weigand47445072013-05-06 16:26:41 +00006604
Craig Topper8a13c412014-05-21 05:09:00 +00006605 return ABIArgInfo::getDirect(nullptr);
Ulrich Weigand47445072013-05-06 16:26:41 +00006606}
6607
6608//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006609// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00006610//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006611
6612namespace {
6613
6614class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
6615public:
Chris Lattner2b037972010-07-29 02:01:43 +00006616 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
6617 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Eric Christopher162c91c2015-06-05 22:03:00 +00006618 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006619 CodeGen::CodeGenModule &M,
6620 ForDefinition_t IsForDefinition) const override;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006621};
6622
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006623}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006624
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006625void MSP430TargetCodeGenInfo::setTargetAttributes(
6626 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
6627 ForDefinition_t IsForDefinition) const {
6628 if (!IsForDefinition)
6629 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006630 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006631 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
6632 // Handle 'interrupt' attribute:
6633 llvm::Function *F = cast<llvm::Function>(GV);
6634
6635 // Step 1: Set ISR calling convention.
6636 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
6637
6638 // Step 2: Add attributes goodness.
Bill Wendling207f0532012-12-20 19:27:06 +00006639 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006640
6641 // Step 3: Emit ISR vector alias.
Anton Korobeynikovc5a7f922012-11-26 18:59:10 +00006642 unsigned Num = attr->getNumber() / 2;
Rafael Espindola234405b2014-05-17 21:30:14 +00006643 llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
6644 "__isr_" + Twine(Num), F);
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006645 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00006646 }
6647}
6648
Chris Lattner0cf24192010-06-28 20:05:43 +00006649//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00006650// MIPS ABI Implementation. This works for both little-endian and
6651// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00006652//===----------------------------------------------------------------------===//
6653
John McCall943fae92010-05-27 06:19:26 +00006654namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00006655class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00006656 bool IsO32;
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006657 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
6658 void CoerceToIntArgs(uint64_t TySize,
Craig Topper5603df42013-07-05 19:34:19 +00006659 SmallVectorImpl<llvm::Type *> &ArgList) const;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006660 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006661 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006662 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006663public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006664 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006665 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006666 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00006667
6668 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006669 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Craig Topper4f12f102014-03-12 06:41:41 +00006670 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00006671 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6672 QualType Ty) const override;
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00006673 bool shouldSignExtUnsignedType(QualType Ty) const override;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006674};
6675
John McCall943fae92010-05-27 06:19:26 +00006676class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00006677 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00006678public:
Akira Hatanakac4baedd2013-11-11 22:10:46 +00006679 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
6680 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
Akira Hatanaka14378522011-11-02 23:14:57 +00006681 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00006682
Craig Topper4f12f102014-03-12 06:41:41 +00006683 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
John McCall943fae92010-05-27 06:19:26 +00006684 return 29;
6685 }
6686
Eric Christopher162c91c2015-06-05 22:03:00 +00006687 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006688 CodeGen::CodeGenModule &CGM,
6689 ForDefinition_t IsForDefinition) const override {
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00006690 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Reed Kotler3d5966f2013-03-13 20:40:30 +00006691 if (!FD) return;
Rafael Espindolaa0851a22013-03-19 14:32:23 +00006692 llvm::Function *Fn = cast<llvm::Function>(GV);
Simon Atanasyan1a116db2017-07-20 20:34:18 +00006693
6694 if (FD->hasAttr<MipsLongCallAttr>())
6695 Fn->addFnAttr("long-call");
6696 else if (FD->hasAttr<MipsShortCallAttr>())
6697 Fn->addFnAttr("short-call");
6698
6699 // Other attributes do not have a meaning for declarations.
6700 if (!IsForDefinition)
6701 return;
6702
Reed Kotler3d5966f2013-03-13 20:40:30 +00006703 if (FD->hasAttr<Mips16Attr>()) {
6704 Fn->addFnAttr("mips16");
6705 }
6706 else if (FD->hasAttr<NoMips16Attr>()) {
6707 Fn->addFnAttr("nomips16");
6708 }
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00006709
Simon Atanasyan2c87f532017-05-22 12:47:43 +00006710 if (FD->hasAttr<MicroMipsAttr>())
6711 Fn->addFnAttr("micromips");
6712 else if (FD->hasAttr<NoMicroMipsAttr>())
6713 Fn->addFnAttr("nomicromips");
6714
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00006715 const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
6716 if (!Attr)
6717 return;
6718
6719 const char *Kind;
6720 switch (Attr->getInterrupt()) {
Daniel Sandersbd3f47f2015-11-27 18:03:44 +00006721 case MipsInterruptAttr::eic: Kind = "eic"; break;
6722 case MipsInterruptAttr::sw0: Kind = "sw0"; break;
6723 case MipsInterruptAttr::sw1: Kind = "sw1"; break;
6724 case MipsInterruptAttr::hw0: Kind = "hw0"; break;
6725 case MipsInterruptAttr::hw1: Kind = "hw1"; break;
6726 case MipsInterruptAttr::hw2: Kind = "hw2"; break;
6727 case MipsInterruptAttr::hw3: Kind = "hw3"; break;
6728 case MipsInterruptAttr::hw4: Kind = "hw4"; break;
6729 case MipsInterruptAttr::hw5: Kind = "hw5"; break;
6730 }
6731
6732 Fn->addFnAttr("interrupt", Kind);
6733
Reed Kotler373feca2013-01-16 17:10:28 +00006734 }
Reed Kotler3d5966f2013-03-13 20:40:30 +00006735
John McCall943fae92010-05-27 06:19:26 +00006736 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00006737 llvm::Value *Address) const override;
John McCall3480ef22011-08-30 01:42:09 +00006738
Craig Topper4f12f102014-03-12 06:41:41 +00006739 unsigned getSizeOfUnwindException() const override {
Akira Hatanaka0486db02011-09-20 18:23:28 +00006740 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00006741 }
John McCall943fae92010-05-27 06:19:26 +00006742};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006743}
John McCall943fae92010-05-27 06:19:26 +00006744
Eric Christopher7565e0d2015-05-29 23:09:49 +00006745void MipsABIInfo::CoerceToIntArgs(
6746 uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006747 llvm::IntegerType *IntTy =
6748 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006749
6750 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
6751 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
6752 ArgList.push_back(IntTy);
6753
6754 // If necessary, add one more integer type to ArgList.
6755 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
6756
6757 if (R)
6758 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006759}
6760
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006761// In N32/64, an aligned double precision floating point field is passed in
6762// a register.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006763llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006764 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
6765
6766 if (IsO32) {
6767 CoerceToIntArgs(TySize, ArgList);
6768 return llvm::StructType::get(getVMContext(), ArgList);
6769 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006770
Akira Hatanaka02e13e52012-01-12 00:52:17 +00006771 if (Ty->isComplexType())
6772 return CGT.ConvertType(Ty);
Akira Hatanaka79f04612012-01-10 23:12:19 +00006773
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00006774 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006775
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006776 // Unions/vectors are passed in integer registers.
6777 if (!RT || !RT->isStructureOrClassType()) {
6778 CoerceToIntArgs(TySize, ArgList);
6779 return llvm::StructType::get(getVMContext(), ArgList);
6780 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006781
6782 const RecordDecl *RD = RT->getDecl();
6783 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006784 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Eric Christopher7565e0d2015-05-29 23:09:49 +00006785
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006786 uint64_t LastOffset = 0;
6787 unsigned idx = 0;
6788 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
6789
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00006790 // Iterate over fields in the struct/class and check if there are any aligned
6791 // double fields.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006792 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6793 i != e; ++i, ++idx) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00006794 const QualType Ty = i->getType();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006795 const BuiltinType *BT = Ty->getAs<BuiltinType>();
6796
6797 if (!BT || BT->getKind() != BuiltinType::Double)
6798 continue;
6799
6800 uint64_t Offset = Layout.getFieldOffset(idx);
6801 if (Offset % 64) // Ignore doubles that are not aligned.
6802 continue;
6803
6804 // Add ((Offset - LastOffset) / 64) args of type i64.
6805 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
6806 ArgList.push_back(I64);
6807
6808 // Add double type.
6809 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
6810 LastOffset = Offset + 64;
6811 }
6812
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006813 CoerceToIntArgs(TySize - LastOffset, IntArgList);
6814 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanaka101f70d2011-11-02 23:54:49 +00006815
6816 return llvm::StructType::get(getVMContext(), ArgList);
6817}
6818
Akira Hatanakaddd66342013-10-29 18:41:15 +00006819llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
6820 uint64_t Offset) const {
6821 if (OrigOffset + MinABIStackAlignInBytes > Offset)
Craig Topper8a13c412014-05-21 05:09:00 +00006822 return nullptr;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006823
Akira Hatanakaddd66342013-10-29 18:41:15 +00006824 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
Akira Hatanaka1632af62012-01-09 19:31:25 +00006825}
Akira Hatanaka21ee88c2012-01-10 22:44:52 +00006826
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006827ABIArgInfo
6828MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Daniel Sanders998c9102015-01-14 12:00:12 +00006829 Ty = useFirstFieldIfTransparentUnion(Ty);
6830
Akira Hatanaka1632af62012-01-09 19:31:25 +00006831 uint64_t OrigOffset = Offset;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006832 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanaka1632af62012-01-09 19:31:25 +00006833 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006834
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006835 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
6836 (uint64_t)StackAlignInBytes);
Rui Ueyama83aa9792016-01-14 21:00:27 +00006837 unsigned CurrOffset = llvm::alignTo(Offset, Align);
6838 Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
Akira Hatanaka1632af62012-01-09 19:31:25 +00006839
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006840 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanakab579fe52011-06-02 00:09:17 +00006841 // Ignore empty aggregates.
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006842 if (TySize == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00006843 return ABIArgInfo::getIgnore();
6844
Mark Lacey3825e832013-10-06 01:33:34 +00006845 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006846 Offset = OrigOffset + MinABIStackAlignInBytes;
John McCall7f416cc2015-09-08 08:05:57 +00006847 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00006848 }
Akira Hatanakadf425db2011-08-01 18:09:58 +00006849
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006850 // If we have reached here, aggregates are passed directly by coercing to
6851 // another structure type. Padding is inserted if the offset of the
6852 // aggregate is unaligned.
Daniel Sandersaa1b3552014-10-24 15:30:16 +00006853 ABIArgInfo ArgInfo =
6854 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
6855 getPaddingType(OrigOffset, CurrOffset));
6856 ArgInfo.setInReg(true);
6857 return ArgInfo;
Akira Hatanakab579fe52011-06-02 00:09:17 +00006858 }
6859
6860 // Treat an enum type as its underlying type.
6861 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6862 Ty = EnumTy->getDecl()->getIntegerType();
6863
Daniel Sanders5b445b32014-10-24 14:42:42 +00006864 // All integral types are promoted to the GPR width.
6865 if (Ty->isIntegralOrEnumerationType())
Akira Hatanaka1632af62012-01-09 19:31:25 +00006866 return ABIArgInfo::getExtend();
6867
Akira Hatanakaddd66342013-10-29 18:41:15 +00006868 return ABIArgInfo::getDirect(
Craig Topper8a13c412014-05-21 05:09:00 +00006869 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
Akira Hatanakab579fe52011-06-02 00:09:17 +00006870}
6871
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006872llvm::Type*
6873MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakab6f74432012-02-09 18:49:26 +00006874 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006875 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006876
Akira Hatanakab6f74432012-02-09 18:49:26 +00006877 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006878 const RecordDecl *RD = RT->getDecl();
Akira Hatanakab6f74432012-02-09 18:49:26 +00006879 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
6880 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006881
Akira Hatanakab6f74432012-02-09 18:49:26 +00006882 // N32/64 returns struct/classes in floating point registers if the
6883 // following conditions are met:
6884 // 1. The size of the struct/class is no larger than 128-bit.
6885 // 2. The struct/class has one or two fields all of which are floating
6886 // point types.
Eric Christopher7565e0d2015-05-29 23:09:49 +00006887 // 3. The offset of the first field is zero (this follows what gcc does).
Akira Hatanakab6f74432012-02-09 18:49:26 +00006888 //
6889 // Any other composite results are returned in integer registers.
6890 //
6891 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
6892 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
6893 for (; b != e; ++b) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00006894 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006895
Akira Hatanakab6f74432012-02-09 18:49:26 +00006896 if (!BT || !BT->isFloatingPoint())
6897 break;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006898
David Blaikie2d7c57e2012-04-30 02:36:29 +00006899 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakab6f74432012-02-09 18:49:26 +00006900 }
6901
6902 if (b == e)
6903 return llvm::StructType::get(getVMContext(), RTList,
6904 RD->hasAttr<PackedAttr>());
6905
6906 RTList.clear();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006907 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006908 }
6909
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00006910 CoerceToIntArgs(Size, RTList);
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006911 return llvm::StructType::get(getVMContext(), RTList);
6912}
6913
Akira Hatanakab579fe52011-06-02 00:09:17 +00006914ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanaka60f5fe62012-01-23 23:18:57 +00006915 uint64_t Size = getContext().getTypeSize(RetTy);
6916
Daniel Sandersed39f582014-09-04 13:28:14 +00006917 if (RetTy->isVoidType())
6918 return ABIArgInfo::getIgnore();
6919
6920 // O32 doesn't treat zero-sized structs differently from other structs.
6921 // However, N32/N64 ignores zero sized return values.
6922 if (!IsO32 && Size == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00006923 return ABIArgInfo::getIgnore();
6924
Akira Hatanakac37eddf2012-05-11 21:01:17 +00006925 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006926 if (Size <= 128) {
6927 if (RetTy->isAnyComplexType())
6928 return ABIArgInfo::getDirect();
6929
Daniel Sanderse5018b62014-09-04 15:05:39 +00006930 // O32 returns integer vectors in registers and N32/N64 returns all small
Daniel Sanders00a56ff2014-09-04 15:07:43 +00006931 // aggregates in registers.
Daniel Sanderse5018b62014-09-04 15:05:39 +00006932 if (!IsO32 ||
6933 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
6934 ABIArgInfo ArgInfo =
6935 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
6936 ArgInfo.setInReg(true);
6937 return ArgInfo;
6938 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00006939 }
Akira Hatanakab579fe52011-06-02 00:09:17 +00006940
John McCall7f416cc2015-09-08 08:05:57 +00006941 return getNaturalAlignIndirect(RetTy);
Akira Hatanakab579fe52011-06-02 00:09:17 +00006942 }
6943
6944 // Treat an enum type as its underlying type.
6945 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6946 RetTy = EnumTy->getDecl()->getIntegerType();
6947
6948 return (RetTy->isPromotableIntegerType() ?
6949 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6950}
6951
6952void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanaka32604a92012-01-12 01:10:09 +00006953 ABIArgInfo &RetInfo = FI.getReturnInfo();
Reid Kleckner40ca9132014-05-13 22:05:45 +00006954 if (!getCXXABI().classifyReturnType(FI))
6955 RetInfo = classifyReturnType(FI.getReturnType());
Akira Hatanaka32604a92012-01-12 01:10:09 +00006956
Eric Christopher7565e0d2015-05-29 23:09:49 +00006957 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00006958 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanaka32604a92012-01-12 01:10:09 +00006959
Aaron Ballmanec47bc22014-03-17 18:10:01 +00006960 for (auto &I : FI.arguments())
6961 I.info = classifyArgumentType(I.type, Offset);
Akira Hatanakab579fe52011-06-02 00:09:17 +00006962}
6963
John McCall7f416cc2015-09-08 08:05:57 +00006964Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6965 QualType OrigTy) const {
6966 QualType Ty = OrigTy;
Daniel Sanders59229dc2014-11-19 10:01:35 +00006967
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006968 // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
6969 // Pointers are also promoted in the same way but this only matters for N32.
Daniel Sanders59229dc2014-11-19 10:01:35 +00006970 unsigned SlotSizeInBits = IsO32 ? 32 : 64;
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006971 unsigned PtrWidth = getTarget().getPointerWidth(0);
John McCall7f416cc2015-09-08 08:05:57 +00006972 bool DidPromote = false;
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006973 if ((Ty->isIntegerType() &&
John McCall7f416cc2015-09-08 08:05:57 +00006974 getContext().getIntWidth(Ty) < SlotSizeInBits) ||
Daniel Sanderscdcb5802015-01-13 10:47:00 +00006975 (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
John McCall7f416cc2015-09-08 08:05:57 +00006976 DidPromote = true;
6977 Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
6978 Ty->isSignedIntegerType());
Daniel Sanders59229dc2014-11-19 10:01:35 +00006979 }
Eric Christopher7565e0d2015-05-29 23:09:49 +00006980
John McCall7f416cc2015-09-08 08:05:57 +00006981 auto TyInfo = getContext().getTypeInfoInChars(Ty);
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00006982
John McCall7f416cc2015-09-08 08:05:57 +00006983 // The alignment of things in the argument area is never larger than
6984 // StackAlignInBytes.
6985 TyInfo.second =
6986 std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
6987
6988 // MinABIStackAlignInBytes is the size of argument slots on the stack.
6989 CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
6990
6991 Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6992 TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
6993
6994
6995 // If there was a promotion, "unpromote" into a temporary.
6996 // TODO: can we just use a pointer into a subset of the original slot?
6997 if (DidPromote) {
6998 Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
6999 llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
7000
7001 // Truncate down to the right width.
7002 llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
7003 : CGF.IntPtrTy);
7004 llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
7005 if (OrigTy->isPointerType())
7006 V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
7007
7008 CGF.Builder.CreateStore(V, Temp);
7009 Addr = Temp;
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00007010 }
Daniel Sanders2ef3cdd32014-08-01 13:26:28 +00007011
John McCall7f416cc2015-09-08 08:05:57 +00007012 return Addr;
Akira Hatanakab579fe52011-06-02 00:09:17 +00007013}
7014
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00007015bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
7016 int TySize = getContext().getTypeSize(Ty);
Eric Christopher7565e0d2015-05-29 23:09:49 +00007017
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00007018 // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
7019 if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
7020 return true;
Eric Christopher7565e0d2015-05-29 23:09:49 +00007021
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00007022 return false;
7023}
7024
John McCall943fae92010-05-27 06:19:26 +00007025bool
7026MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7027 llvm::Value *Address) const {
7028 // This information comes from gcc's implementation, which seems to
7029 // as canonical as it gets.
7030
John McCall943fae92010-05-27 06:19:26 +00007031 // Everything on MIPS is 4 bytes. Double-precision FP registers
7032 // are aliased to pairs of single-precision FP registers.
Chris Lattnerece04092012-02-07 00:39:47 +00007033 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCall943fae92010-05-27 06:19:26 +00007034
7035 // 0-31 are the general purpose registers, $0 - $31.
7036 // 32-63 are the floating-point registers, $f0 - $f31.
7037 // 64 and 65 are the multiply/divide registers, $hi and $lo.
7038 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattnerece04092012-02-07 00:39:47 +00007039 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCall943fae92010-05-27 06:19:26 +00007040
7041 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
7042 // They are one bit wide and ignored here.
7043
7044 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
7045 // (coprocessor 1 is the FP unit)
7046 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
7047 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
7048 // 176-181 are the DSP accumulator registers.
Chris Lattnerece04092012-02-07 00:39:47 +00007049 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCall943fae92010-05-27 06:19:26 +00007050 return false;
7051}
7052
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007053//===----------------------------------------------------------------------===//
Dylan McKaye8232d72017-02-08 05:09:26 +00007054// AVR ABI Implementation.
7055//===----------------------------------------------------------------------===//
7056
7057namespace {
7058class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
7059public:
7060 AVRTargetCodeGenInfo(CodeGenTypes &CGT)
7061 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { }
7062
7063 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00007064 CodeGen::CodeGenModule &CGM,
7065 ForDefinition_t IsForDefinition) const override {
7066 if (!IsForDefinition)
7067 return;
Dylan McKaye8232d72017-02-08 05:09:26 +00007068 const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
7069 if (!FD) return;
7070 auto *Fn = cast<llvm::Function>(GV);
7071
7072 if (FD->getAttr<AVRInterruptAttr>())
7073 Fn->addFnAttr("interrupt");
7074
7075 if (FD->getAttr<AVRSignalAttr>())
7076 Fn->addFnAttr("signal");
7077 }
7078};
7079}
7080
7081//===----------------------------------------------------------------------===//
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007082// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
Eric Christopher7565e0d2015-05-29 23:09:49 +00007083// Currently subclassed only to implement custom OpenCL C function attribute
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007084// handling.
7085//===----------------------------------------------------------------------===//
7086
7087namespace {
7088
7089class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
7090public:
7091 TCETargetCodeGenInfo(CodeGenTypes &CGT)
7092 : DefaultTargetCodeGenInfo(CGT) {}
7093
Eric Christopher162c91c2015-06-05 22:03:00 +00007094 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00007095 CodeGen::CodeGenModule &M,
7096 ForDefinition_t IsForDefinition) const override;
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007097};
7098
Eric Christopher162c91c2015-06-05 22:03:00 +00007099void TCETargetCodeGenInfo::setTargetAttributes(
Simon Atanasyan1a116db2017-07-20 20:34:18 +00007100 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
7101 ForDefinition_t IsForDefinition) const {
7102 if (!IsForDefinition)
7103 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00007104 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007105 if (!FD) return;
7106
7107 llvm::Function *F = cast<llvm::Function>(GV);
Eric Christopher7565e0d2015-05-29 23:09:49 +00007108
David Blaikiebbafb8a2012-03-11 07:00:24 +00007109 if (M.getLangOpts().OpenCL) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007110 if (FD->hasAttr<OpenCLKernelAttr>()) {
7111 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling207f0532012-12-20 19:27:06 +00007112 F->addFnAttr(llvm::Attribute::NoInline);
Aaron Ballman36a18ff2013-12-19 13:16:35 +00007113 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
7114 if (Attr) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007115 // Convert the reqd_work_group_size() attributes to metadata.
7116 llvm::LLVMContext &Context = F->getContext();
Eric Christopher7565e0d2015-05-29 23:09:49 +00007117 llvm::NamedMDNode *OpenCLMetadata =
7118 M.getModule().getOrInsertNamedMetadata(
7119 "opencl.kernel_wg_size_info");
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007120
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00007121 SmallVector<llvm::Metadata *, 5> Operands;
7122 Operands.push_back(llvm::ConstantAsMetadata::get(F));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007123
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00007124 Operands.push_back(
7125 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7126 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
7127 Operands.push_back(
7128 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7129 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
7130 Operands.push_back(
7131 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7132 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007133
Eric Christopher7565e0d2015-05-29 23:09:49 +00007134 // Add a boolean constant operand for "required" (true) or "hint"
7135 // (false) for implementing the work_group_size_hint attr later.
7136 // Currently always true as the hint is not yet implemented.
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00007137 Operands.push_back(
7138 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00007139 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
7140 }
7141 }
7142 }
7143}
7144
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007145}
John McCall943fae92010-05-27 06:19:26 +00007146
Tony Linthicum76329bf2011-12-12 21:14:55 +00007147//===----------------------------------------------------------------------===//
7148// Hexagon ABI Implementation
7149//===----------------------------------------------------------------------===//
7150
7151namespace {
7152
7153class HexagonABIInfo : public ABIInfo {
7154
7155
7156public:
7157 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
7158
7159private:
7160
7161 ABIArgInfo classifyReturnType(QualType RetTy) const;
7162 ABIArgInfo classifyArgumentType(QualType RetTy) const;
7163
Craig Topper4f12f102014-03-12 06:41:41 +00007164 void computeInfo(CGFunctionInfo &FI) const override;
Tony Linthicum76329bf2011-12-12 21:14:55 +00007165
John McCall7f416cc2015-09-08 08:05:57 +00007166 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7167 QualType Ty) const override;
Tony Linthicum76329bf2011-12-12 21:14:55 +00007168};
7169
7170class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
7171public:
7172 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
7173 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
7174
Craig Topper4f12f102014-03-12 06:41:41 +00007175 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Tony Linthicum76329bf2011-12-12 21:14:55 +00007176 return 29;
7177 }
7178};
7179
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007180}
Tony Linthicum76329bf2011-12-12 21:14:55 +00007181
7182void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
Reid Kleckner40ca9132014-05-13 22:05:45 +00007183 if (!getCXXABI().classifyReturnType(FI))
7184 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Aaron Ballmanec47bc22014-03-17 18:10:01 +00007185 for (auto &I : FI.arguments())
7186 I.info = classifyArgumentType(I.type);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007187}
7188
7189ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
7190 if (!isAggregateTypeForABI(Ty)) {
7191 // Treat an enum type as its underlying type.
7192 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7193 Ty = EnumTy->getDecl()->getIntegerType();
7194
7195 return (Ty->isPromotableIntegerType() ?
7196 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
7197 }
7198
Krzysztof Parzyszek408b2722017-05-12 13:18:07 +00007199 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
7200 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7201
Tony Linthicum76329bf2011-12-12 21:14:55 +00007202 // Ignore empty records.
7203 if (isEmptyRecord(getContext(), Ty, true))
7204 return ABIArgInfo::getIgnore();
7205
Tony Linthicum76329bf2011-12-12 21:14:55 +00007206 uint64_t Size = getContext().getTypeSize(Ty);
7207 if (Size > 64)
John McCall7f416cc2015-09-08 08:05:57 +00007208 return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007209 // Pass in the smallest viable integer type.
7210 else if (Size > 32)
7211 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
7212 else if (Size > 16)
7213 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7214 else if (Size > 8)
7215 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7216 else
7217 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
7218}
7219
7220ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
7221 if (RetTy->isVoidType())
7222 return ABIArgInfo::getIgnore();
7223
7224 // Large vector types should be returned via memory.
7225 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
John McCall7f416cc2015-09-08 08:05:57 +00007226 return getNaturalAlignIndirect(RetTy);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007227
7228 if (!isAggregateTypeForABI(RetTy)) {
7229 // Treat an enum type as its underlying type.
7230 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7231 RetTy = EnumTy->getDecl()->getIntegerType();
7232
7233 return (RetTy->isPromotableIntegerType() ?
7234 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
7235 }
7236
Tony Linthicum76329bf2011-12-12 21:14:55 +00007237 if (isEmptyRecord(getContext(), RetTy, true))
7238 return ABIArgInfo::getIgnore();
7239
7240 // Aggregates <= 8 bytes are returned in r0; other aggregates
7241 // are returned indirectly.
7242 uint64_t Size = getContext().getTypeSize(RetTy);
7243 if (Size <= 64) {
7244 // Return in the smallest viable integer type.
7245 if (Size <= 8)
7246 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
7247 if (Size <= 16)
7248 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7249 if (Size <= 32)
7250 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7251 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
7252 }
7253
John McCall7f416cc2015-09-08 08:05:57 +00007254 return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007255}
7256
John McCall7f416cc2015-09-08 08:05:57 +00007257Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7258 QualType Ty) const {
7259 // FIXME: Someone needs to audit that this handle alignment correctly.
7260 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
7261 getContext().getTypeInfoInChars(Ty),
7262 CharUnits::fromQuantity(4),
7263 /*AllowHigherAlign*/ true);
Tony Linthicum76329bf2011-12-12 21:14:55 +00007264}
7265
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007266//===----------------------------------------------------------------------===//
Jacques Pienaard964cc22016-03-28 21:02:54 +00007267// Lanai ABI Implementation
7268//===----------------------------------------------------------------------===//
7269
Benjamin Kramer5d28c7f2016-04-07 10:14:54 +00007270namespace {
Jacques Pienaard964cc22016-03-28 21:02:54 +00007271class LanaiABIInfo : public DefaultABIInfo {
7272public:
7273 LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7274
7275 bool shouldUseInReg(QualType Ty, CCState &State) const;
7276
7277 void computeInfo(CGFunctionInfo &FI) const override {
7278 CCState State(FI.getCallingConvention());
7279 // Lanai uses 4 registers to pass arguments unless the function has the
7280 // regparm attribute set.
7281 if (FI.getHasRegParm()) {
7282 State.FreeRegs = FI.getRegParm();
7283 } else {
7284 State.FreeRegs = 4;
7285 }
7286
7287 if (!getCXXABI().classifyReturnType(FI))
7288 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7289 for (auto &I : FI.arguments())
7290 I.info = classifyArgumentType(I.type, State);
7291 }
7292
Jacques Pienaare74d9132016-04-26 00:09:29 +00007293 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
Jacques Pienaard964cc22016-03-28 21:02:54 +00007294 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
7295};
Benjamin Kramer5d28c7f2016-04-07 10:14:54 +00007296} // end anonymous namespace
Jacques Pienaard964cc22016-03-28 21:02:54 +00007297
7298bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
7299 unsigned Size = getContext().getTypeSize(Ty);
7300 unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
7301
7302 if (SizeInRegs == 0)
7303 return false;
7304
7305 if (SizeInRegs > State.FreeRegs) {
7306 State.FreeRegs = 0;
7307 return false;
7308 }
7309
7310 State.FreeRegs -= SizeInRegs;
7311
7312 return true;
7313}
7314
Jacques Pienaare74d9132016-04-26 00:09:29 +00007315ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
7316 CCState &State) const {
7317 if (!ByVal) {
7318 if (State.FreeRegs) {
7319 --State.FreeRegs; // Non-byval indirects just use one pointer.
7320 return getNaturalAlignIndirectInReg(Ty);
7321 }
7322 return getNaturalAlignIndirect(Ty, false);
7323 }
7324
7325 // Compute the byval alignment.
Kostya Serebryany0da44422016-04-26 01:53:49 +00007326 const unsigned MinABIStackAlignInBytes = 4;
Jacques Pienaare74d9132016-04-26 00:09:29 +00007327 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
7328 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
7329 /*Realign=*/TypeAlign >
7330 MinABIStackAlignInBytes);
7331}
7332
Jacques Pienaard964cc22016-03-28 21:02:54 +00007333ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
7334 CCState &State) const {
Jacques Pienaare74d9132016-04-26 00:09:29 +00007335 // Check with the C++ ABI first.
7336 const RecordType *RT = Ty->getAs<RecordType>();
7337 if (RT) {
7338 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
7339 if (RAA == CGCXXABI::RAA_Indirect) {
7340 return getIndirectResult(Ty, /*ByVal=*/false, State);
7341 } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
7342 return getNaturalAlignIndirect(Ty, /*ByRef=*/true);
7343 }
7344 }
7345
7346 if (isAggregateTypeForABI(Ty)) {
7347 // Structures with flexible arrays are always indirect.
7348 if (RT && RT->getDecl()->hasFlexibleArrayMember())
7349 return getIndirectResult(Ty, /*ByVal=*/true, State);
7350
7351 // Ignore empty structs/unions.
7352 if (isEmptyRecord(getContext(), Ty, true))
7353 return ABIArgInfo::getIgnore();
7354
7355 llvm::LLVMContext &LLVMContext = getVMContext();
7356 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
7357 if (SizeInRegs <= State.FreeRegs) {
7358 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
7359 SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
7360 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
7361 State.FreeRegs -= SizeInRegs;
7362 return ABIArgInfo::getDirectInReg(Result);
7363 } else {
7364 State.FreeRegs = 0;
7365 }
7366 return getIndirectResult(Ty, true, State);
7367 }
Jacques Pienaard964cc22016-03-28 21:02:54 +00007368
7369 // Treat an enum type as its underlying type.
7370 if (const auto *EnumTy = Ty->getAs<EnumType>())
7371 Ty = EnumTy->getDecl()->getIntegerType();
7372
Jacques Pienaare74d9132016-04-26 00:09:29 +00007373 bool InReg = shouldUseInReg(Ty, State);
7374 if (Ty->isPromotableIntegerType()) {
7375 if (InReg)
7376 return ABIArgInfo::getDirectInReg();
Jacques Pienaard964cc22016-03-28 21:02:54 +00007377 return ABIArgInfo::getExtend();
Jacques Pienaare74d9132016-04-26 00:09:29 +00007378 }
7379 if (InReg)
7380 return ABIArgInfo::getDirectInReg();
Jacques Pienaard964cc22016-03-28 21:02:54 +00007381 return ABIArgInfo::getDirect();
7382}
7383
7384namespace {
7385class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
7386public:
7387 LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
7388 : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {}
7389};
7390}
7391
7392//===----------------------------------------------------------------------===//
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007393// AMDGPU ABI Implementation
7394//===----------------------------------------------------------------------===//
7395
7396namespace {
7397
Matt Arsenault88d7da02016-08-22 19:25:59 +00007398class AMDGPUABIInfo final : public DefaultABIInfo {
Matt Arsenault88d7da02016-08-22 19:25:59 +00007399private:
Matt Arsenault3fe73952017-08-09 21:44:58 +00007400 static const unsigned MaxNumRegsForArgsRet = 16;
7401
Matt Arsenault3fe73952017-08-09 21:44:58 +00007402 unsigned numRegsForType(QualType Ty) const;
7403
7404 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
7405 bool isHomogeneousAggregateSmallEnough(const Type *Base,
7406 uint64_t Members) const override;
7407
7408public:
7409 explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
7410 DefaultABIInfo(CGT) {}
7411
7412 ABIArgInfo classifyReturnType(QualType RetTy) const;
7413 ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
7414 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
Matt Arsenault88d7da02016-08-22 19:25:59 +00007415
7416 void computeInfo(CGFunctionInfo &FI) const override;
7417};
7418
Matt Arsenault3fe73952017-08-09 21:44:58 +00007419bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
7420 return true;
7421}
7422
7423bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
7424 const Type *Base, uint64_t Members) const {
7425 uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
7426
7427 // Homogeneous Aggregates may occupy at most 16 registers.
7428 return Members * NumRegs <= MaxNumRegsForArgsRet;
7429}
7430
Matt Arsenault3fe73952017-08-09 21:44:58 +00007431/// Estimate number of registers the type will use when passed in registers.
7432unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
7433 unsigned NumRegs = 0;
7434
7435 if (const VectorType *VT = Ty->getAs<VectorType>()) {
7436 // Compute from the number of elements. The reported size is based on the
7437 // in-memory size, which includes the padding 4th element for 3-vectors.
7438 QualType EltTy = VT->getElementType();
7439 unsigned EltSize = getContext().getTypeSize(EltTy);
7440
7441 // 16-bit element vectors should be passed as packed.
7442 if (EltSize == 16)
7443 return (VT->getNumElements() + 1) / 2;
7444
7445 unsigned EltNumRegs = (EltSize + 31) / 32;
7446 return EltNumRegs * VT->getNumElements();
7447 }
7448
7449 if (const RecordType *RT = Ty->getAs<RecordType>()) {
7450 const RecordDecl *RD = RT->getDecl();
7451 assert(!RD->hasFlexibleArrayMember());
7452
7453 for (const FieldDecl *Field : RD->fields()) {
7454 QualType FieldTy = Field->getType();
7455 NumRegs += numRegsForType(FieldTy);
7456 }
7457
7458 return NumRegs;
7459 }
7460
7461 return (getContext().getTypeSize(Ty) + 31) / 32;
7462}
7463
Matt Arsenault88d7da02016-08-22 19:25:59 +00007464void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
Matt Arsenault3fe73952017-08-09 21:44:58 +00007465 llvm::CallingConv::ID CC = FI.getCallingConvention();
7466
Matt Arsenault88d7da02016-08-22 19:25:59 +00007467 if (!getCXXABI().classifyReturnType(FI))
7468 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7469
Matt Arsenault3fe73952017-08-09 21:44:58 +00007470 unsigned NumRegsLeft = MaxNumRegsForArgsRet;
7471 for (auto &Arg : FI.arguments()) {
7472 if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
7473 Arg.info = classifyKernelArgumentType(Arg.type);
7474 } else {
7475 Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
7476 }
7477 }
Matt Arsenault88d7da02016-08-22 19:25:59 +00007478}
7479
Matt Arsenault3fe73952017-08-09 21:44:58 +00007480ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
7481 if (isAggregateTypeForABI(RetTy)) {
7482 // Records with non-trivial destructors/copy-constructors should not be
7483 // returned by value.
7484 if (!getRecordArgABI(RetTy, getCXXABI())) {
7485 // Ignore empty structs/unions.
7486 if (isEmptyRecord(getContext(), RetTy, true))
7487 return ABIArgInfo::getIgnore();
7488
7489 // Lower single-element structs to just return a regular value.
7490 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
7491 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
7492
7493 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
7494 const RecordDecl *RD = RT->getDecl();
7495 if (RD->hasFlexibleArrayMember())
7496 return DefaultABIInfo::classifyReturnType(RetTy);
7497 }
7498
7499 // Pack aggregates <= 4 bytes into single VGPR or pair.
7500 uint64_t Size = getContext().getTypeSize(RetTy);
7501 if (Size <= 16)
7502 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7503
7504 if (Size <= 32)
7505 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7506
7507 if (Size <= 64) {
7508 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
7509 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
7510 }
7511
7512 if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
7513 return ABIArgInfo::getDirect();
7514 }
Matt Arsenault88d7da02016-08-22 19:25:59 +00007515 }
7516
Matt Arsenault3fe73952017-08-09 21:44:58 +00007517 // Otherwise just do the default thing.
7518 return DefaultABIInfo::classifyReturnType(RetTy);
7519}
7520
7521/// For kernels all parameters are really passed in a special buffer. It doesn't
7522/// make sense to pass anything byval, so everything must be direct.
7523ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
7524 Ty = useFirstFieldIfTransparentUnion(Ty);
7525
7526 // TODO: Can we omit empty structs?
7527
Matt Arsenault88d7da02016-08-22 19:25:59 +00007528 // Coerce single element structs to its element.
Matt Arsenault3fe73952017-08-09 21:44:58 +00007529 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
7530 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
Matt Arsenault88d7da02016-08-22 19:25:59 +00007531
7532 // If we set CanBeFlattened to true, CodeGen will expand the struct to its
7533 // individual elements, which confuses the Clover OpenCL backend; therefore we
7534 // have to set it to false here. Other args of getDirect() are just defaults.
7535 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
7536}
7537
Matt Arsenault3fe73952017-08-09 21:44:58 +00007538ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
7539 unsigned &NumRegsLeft) const {
7540 assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
7541
7542 Ty = useFirstFieldIfTransparentUnion(Ty);
7543
7544 if (isAggregateTypeForABI(Ty)) {
7545 // Records with non-trivial destructors/copy-constructors should not be
7546 // passed by value.
7547 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
7548 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7549
7550 // Ignore empty structs/unions.
7551 if (isEmptyRecord(getContext(), Ty, true))
7552 return ABIArgInfo::getIgnore();
7553
7554 // Lower single-element structs to just pass a regular value. TODO: We
7555 // could do reasonable-size multiple-element structs too, using getExpand(),
7556 // though watch out for things like bitfields.
7557 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
7558 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
7559
7560 if (const RecordType *RT = Ty->getAs<RecordType>()) {
7561 const RecordDecl *RD = RT->getDecl();
7562 if (RD->hasFlexibleArrayMember())
7563 return DefaultABIInfo::classifyArgumentType(Ty);
7564 }
7565
7566 // Pack aggregates <= 8 bytes into single VGPR or pair.
7567 uint64_t Size = getContext().getTypeSize(Ty);
7568 if (Size <= 64) {
7569 unsigned NumRegs = (Size + 31) / 32;
7570 NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
7571
7572 if (Size <= 16)
7573 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7574
7575 if (Size <= 32)
7576 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7577
7578 // XXX: Should this be i64 instead, and should the limit increase?
7579 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
7580 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
7581 }
7582
7583 if (NumRegsLeft > 0) {
7584 unsigned NumRegs = numRegsForType(Ty);
7585 if (NumRegsLeft >= NumRegs) {
7586 NumRegsLeft -= NumRegs;
7587 return ABIArgInfo::getDirect();
7588 }
7589 }
7590 }
7591
7592 // Otherwise just do the default thing.
7593 ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
7594 if (!ArgInfo.isIndirect()) {
7595 unsigned NumRegs = numRegsForType(Ty);
7596 NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
7597 }
7598
7599 return ArgInfo;
7600}
7601
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007602class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
7603public:
7604 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
Matt Arsenault88d7da02016-08-22 19:25:59 +00007605 : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {}
Eric Christopher162c91c2015-06-05 22:03:00 +00007606 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +00007607 CodeGen::CodeGenModule &M,
7608 ForDefinition_t IsForDefinition) const override;
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00007609 unsigned getOpenCLKernelCallingConv() const override;
Nico Weber7849eeb2016-12-14 21:38:18 +00007610
Yaxun Liu402804b2016-12-15 08:09:08 +00007611 llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
7612 llvm::PointerType *T, QualType QT) const override;
Yaxun Liu6d96f1632017-05-18 18:51:09 +00007613
7614 unsigned getASTAllocaAddressSpace() const override {
7615 return LangAS::FirstTargetAddressSpace +
7616 getABIInfo().getDataLayout().getAllocaAddrSpace();
7617 }
Yaxun Liucbf647c2017-07-08 13:24:52 +00007618 unsigned getGlobalVarAddressSpace(CodeGenModule &CGM,
7619 const VarDecl *D) const override;
Yaxun Liu39195062017-08-04 18:16:31 +00007620 llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S,
7621 llvm::LLVMContext &C) const override;
Yaxun Liuc2a87a02017-10-14 12:23:50 +00007622 llvm::Function *
7623 createEnqueuedBlockKernel(CodeGenFunction &CGF,
7624 llvm::Function *BlockInvokeFunc,
7625 llvm::Value *BlockLiteral) const override;
Yaxun Liu402804b2016-12-15 08:09:08 +00007626};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007627}
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007628
Eric Christopher162c91c2015-06-05 22:03:00 +00007629void AMDGPUTargetCodeGenInfo::setTargetAttributes(
Simon Atanasyan1a116db2017-07-20 20:34:18 +00007630 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M,
7631 ForDefinition_t IsForDefinition) const {
7632 if (!IsForDefinition)
7633 return;
Akira Hatanakaaec6b2c2015-10-08 20:26:34 +00007634 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007635 if (!FD)
7636 return;
7637
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007638 llvm::Function *F = cast<llvm::Function>(GV);
7639
Stanislav Mekhanoshin921a4232017-04-06 18:15:44 +00007640 const auto *ReqdWGS = M.getLangOpts().OpenCL ?
7641 FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
7642 const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
7643 if (ReqdWGS || FlatWGS) {
7644 unsigned Min = FlatWGS ? FlatWGS->getMin() : 0;
7645 unsigned Max = FlatWGS ? FlatWGS->getMax() : 0;
7646 if (ReqdWGS && Min == 0 && Max == 0)
7647 Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007648
7649 if (Min != 0) {
7650 assert(Min <= Max && "Min must be less than or equal Max");
7651
7652 std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
7653 F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
7654 } else
7655 assert(Max == 0 && "Max must be zero");
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007656 }
7657
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007658 if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
7659 unsigned Min = Attr->getMin();
7660 unsigned Max = Attr->getMax();
7661
7662 if (Min != 0) {
7663 assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
7664
7665 std::string AttrVal = llvm::utostr(Min);
7666 if (Max != 0)
7667 AttrVal = AttrVal + "," + llvm::utostr(Max);
7668 F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
7669 } else
7670 assert(Max == 0 && "Max must be zero");
7671 }
7672
7673 if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007674 unsigned NumSGPR = Attr->getNumSGPR();
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007675
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007676 if (NumSGPR != 0)
Konstantin Zhuravlyov5b48d722016-09-26 01:02:57 +00007677 F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
7678 }
7679
7680 if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
7681 uint32_t NumVGPR = Attr->getNumVGPR();
7682
7683 if (NumVGPR != 0)
7684 F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00007685 }
Yaxun Liuf2e8ab22016-07-19 19:39:45 +00007686}
Tony Linthicum76329bf2011-12-12 21:14:55 +00007687
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00007688unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
7689 return llvm::CallingConv::AMDGPU_KERNEL;
7690}
7691
Yaxun Liu402804b2016-12-15 08:09:08 +00007692// Currently LLVM assumes null pointers always have value 0,
7693// which results in incorrectly transformed IR. Therefore, instead of
7694// emitting null pointers in private and local address spaces, a null
7695// pointer in generic address space is emitted which is casted to a
7696// pointer in local or private address space.
7697llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
7698 const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
7699 QualType QT) const {
7700 if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
7701 return llvm::ConstantPointerNull::get(PT);
7702
7703 auto &Ctx = CGM.getContext();
7704 auto NPT = llvm::PointerType::get(PT->getElementType(),
7705 Ctx.getTargetAddressSpace(LangAS::opencl_generic));
7706 return llvm::ConstantExpr::getAddrSpaceCast(
7707 llvm::ConstantPointerNull::get(NPT), PT);
7708}
7709
Yaxun Liucbf647c2017-07-08 13:24:52 +00007710unsigned
7711AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
7712 const VarDecl *D) const {
7713 assert(!CGM.getLangOpts().OpenCL &&
7714 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
7715 "Address space agnostic languages only");
7716 unsigned DefaultGlobalAS =
7717 LangAS::FirstTargetAddressSpace +
7718 CGM.getContext().getTargetAddressSpace(LangAS::opencl_global);
7719 if (!D)
7720 return DefaultGlobalAS;
7721
7722 unsigned AddrSpace = D->getType().getAddressSpace();
7723 assert(AddrSpace == LangAS::Default ||
7724 AddrSpace >= LangAS::FirstTargetAddressSpace);
7725 if (AddrSpace != LangAS::Default)
7726 return AddrSpace;
7727
7728 if (CGM.isTypeConstant(D->getType(), false)) {
7729 if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
7730 return ConstAS.getValue();
7731 }
7732 return DefaultGlobalAS;
7733}
7734
Yaxun Liu39195062017-08-04 18:16:31 +00007735llvm::SyncScope::ID
7736AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S,
7737 llvm::LLVMContext &C) const {
7738 StringRef Name;
7739 switch (S) {
7740 case SyncScope::OpenCLWorkGroup:
7741 Name = "workgroup";
7742 break;
7743 case SyncScope::OpenCLDevice:
7744 Name = "agent";
7745 break;
7746 case SyncScope::OpenCLAllSVMDevices:
7747 Name = "";
7748 break;
7749 case SyncScope::OpenCLSubGroup:
7750 Name = "subgroup";
7751 }
7752 return C.getOrInsertSyncScopeID(Name);
7753}
7754
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007755//===----------------------------------------------------------------------===//
Chris Dewhurst7e7ee962016-06-08 14:47:25 +00007756// SPARC v8 ABI Implementation.
7757// Based on the SPARC Compliance Definition version 2.4.1.
7758//
7759// Ensures that complex values are passed in registers.
7760//
7761namespace {
7762class SparcV8ABIInfo : public DefaultABIInfo {
7763public:
7764 SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7765
7766private:
7767 ABIArgInfo classifyReturnType(QualType RetTy) const;
7768 void computeInfo(CGFunctionInfo &FI) const override;
7769};
7770} // end anonymous namespace
7771
7772
7773ABIArgInfo
7774SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
7775 if (Ty->isAnyComplexType()) {
7776 return ABIArgInfo::getDirect();
7777 }
7778 else {
7779 return DefaultABIInfo::classifyReturnType(Ty);
7780 }
7781}
7782
7783void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
7784
7785 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7786 for (auto &Arg : FI.arguments())
7787 Arg.info = classifyArgumentType(Arg.type);
7788}
7789
7790namespace {
7791class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
7792public:
7793 SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
7794 : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {}
7795};
7796} // end anonymous namespace
7797
7798//===----------------------------------------------------------------------===//
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007799// SPARC v9 ABI Implementation.
7800// Based on the SPARC Compliance Definition version 2.4.1.
7801//
7802// Function arguments a mapped to a nominal "parameter array" and promoted to
7803// registers depending on their type. Each argument occupies 8 or 16 bytes in
7804// the array, structs larger than 16 bytes are passed indirectly.
7805//
7806// One case requires special care:
7807//
7808// struct mixed {
7809// int i;
7810// float f;
7811// };
7812//
7813// When a struct mixed is passed by value, it only occupies 8 bytes in the
7814// parameter array, but the int is passed in an integer register, and the float
7815// is passed in a floating point register. This is represented as two arguments
7816// with the LLVM IR inreg attribute:
7817//
7818// declare void f(i32 inreg %i, float inreg %f)
7819//
7820// The code generator will only allocate 4 bytes from the parameter array for
7821// the inreg arguments. All other arguments are allocated a multiple of 8
7822// bytes.
7823//
7824namespace {
7825class SparcV9ABIInfo : public ABIInfo {
7826public:
7827 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
7828
7829private:
7830 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
Craig Topper4f12f102014-03-12 06:41:41 +00007831 void computeInfo(CGFunctionInfo &FI) const override;
John McCall7f416cc2015-09-08 08:05:57 +00007832 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7833 QualType Ty) const override;
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007834
7835 // Coercion type builder for structs passed in registers. The coercion type
7836 // serves two purposes:
7837 //
7838 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
7839 // in registers.
7840 // 2. Expose aligned floating point elements as first-level elements, so the
7841 // code generator knows to pass them in floating point registers.
7842 //
7843 // We also compute the InReg flag which indicates that the struct contains
7844 // aligned 32-bit floats.
7845 //
7846 struct CoerceBuilder {
7847 llvm::LLVMContext &Context;
7848 const llvm::DataLayout &DL;
7849 SmallVector<llvm::Type*, 8> Elems;
7850 uint64_t Size;
7851 bool InReg;
7852
7853 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
7854 : Context(c), DL(dl), Size(0), InReg(false) {}
7855
7856 // Pad Elems with integers until Size is ToSize.
7857 void pad(uint64_t ToSize) {
7858 assert(ToSize >= Size && "Cannot remove elements");
7859 if (ToSize == Size)
7860 return;
7861
7862 // Finish the current 64-bit word.
Rui Ueyama83aa9792016-01-14 21:00:27 +00007863 uint64_t Aligned = llvm::alignTo(Size, 64);
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007864 if (Aligned > Size && Aligned <= ToSize) {
7865 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
7866 Size = Aligned;
7867 }
7868
7869 // Add whole 64-bit words.
7870 while (Size + 64 <= ToSize) {
7871 Elems.push_back(llvm::Type::getInt64Ty(Context));
7872 Size += 64;
7873 }
7874
7875 // Final in-word padding.
7876 if (Size < ToSize) {
7877 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
7878 Size = ToSize;
7879 }
7880 }
7881
7882 // Add a floating point element at Offset.
7883 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
7884 // Unaligned floats are treated as integers.
7885 if (Offset % Bits)
7886 return;
7887 // The InReg flag is only required if there are any floats < 64 bits.
7888 if (Bits < 64)
7889 InReg = true;
7890 pad(Offset);
7891 Elems.push_back(Ty);
7892 Size = Offset + Bits;
7893 }
7894
7895 // Add a struct type to the coercion type, starting at Offset (in bits).
7896 void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
7897 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
7898 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
7899 llvm::Type *ElemTy = StrTy->getElementType(i);
7900 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
7901 switch (ElemTy->getTypeID()) {
7902 case llvm::Type::StructTyID:
7903 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
7904 break;
7905 case llvm::Type::FloatTyID:
7906 addFloat(ElemOffset, ElemTy, 32);
7907 break;
7908 case llvm::Type::DoubleTyID:
7909 addFloat(ElemOffset, ElemTy, 64);
7910 break;
7911 case llvm::Type::FP128TyID:
7912 addFloat(ElemOffset, ElemTy, 128);
7913 break;
7914 case llvm::Type::PointerTyID:
7915 if (ElemOffset % 64 == 0) {
7916 pad(ElemOffset);
7917 Elems.push_back(ElemTy);
7918 Size += 64;
7919 }
7920 break;
7921 default:
7922 break;
7923 }
7924 }
7925 }
7926
7927 // Check if Ty is a usable substitute for the coercion type.
7928 bool isUsableType(llvm::StructType *Ty) const {
Benjamin Kramer39ccabe2015-03-02 11:57:06 +00007929 return llvm::makeArrayRef(Elems) == Ty->elements();
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007930 }
7931
7932 // Get the coercion type as a literal struct type.
7933 llvm::Type *getType() const {
7934 if (Elems.size() == 1)
7935 return Elems.front();
7936 else
7937 return llvm::StructType::get(Context, Elems);
7938 }
7939 };
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007940};
7941} // end anonymous namespace
7942
7943ABIArgInfo
7944SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
7945 if (Ty->isVoidType())
7946 return ABIArgInfo::getIgnore();
7947
7948 uint64_t Size = getContext().getTypeSize(Ty);
7949
7950 // Anything too big to fit in registers is passed with an explicit indirect
7951 // pointer / sret pointer.
7952 if (Size > SizeLimit)
John McCall7f416cc2015-09-08 08:05:57 +00007953 return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007954
7955 // Treat an enum type as its underlying type.
7956 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7957 Ty = EnumTy->getDecl()->getIntegerType();
7958
7959 // Integer types smaller than a register are extended.
7960 if (Size < 64 && Ty->isIntegerType())
7961 return ABIArgInfo::getExtend();
7962
7963 // Other non-aggregates go in registers.
7964 if (!isAggregateTypeForABI(Ty))
7965 return ABIArgInfo::getDirect();
7966
Jakob Stoklund Olesenb81eb3e2014-01-12 06:54:56 +00007967 // If a C++ object has either a non-trivial copy constructor or a non-trivial
7968 // destructor, it is passed with an explicit indirect pointer / sret pointer.
7969 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
John McCall7f416cc2015-09-08 08:05:57 +00007970 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
Jakob Stoklund Olesenb81eb3e2014-01-12 06:54:56 +00007971
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007972 // This is a small aggregate type that should be passed in registers.
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007973 // Build a coercion type from the LLVM struct type.
7974 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
7975 if (!StrTy)
7976 return ABIArgInfo::getDirect();
7977
7978 CoerceBuilder CB(getVMContext(), getDataLayout());
7979 CB.addStruct(0, StrTy);
Rui Ueyama83aa9792016-01-14 21:00:27 +00007980 CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
Jakob Stoklund Olesen02dc6a12013-05-28 04:57:37 +00007981
7982 // Try to use the original type for coercion.
7983 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
7984
7985 if (CB.InReg)
7986 return ABIArgInfo::getDirectInReg(CoerceTy);
7987 else
7988 return ABIArgInfo::getDirect(CoerceTy);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00007989}
7990
John McCall7f416cc2015-09-08 08:05:57 +00007991Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7992 QualType Ty) const {
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007993 ABIArgInfo AI = classifyType(Ty, 16 * 8);
7994 llvm::Type *ArgTy = CGT.ConvertType(Ty);
7995 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
7996 AI.setCoerceToType(ArgTy);
7997
John McCall7f416cc2015-09-08 08:05:57 +00007998 CharUnits SlotSize = CharUnits::fromQuantity(8);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00007999
John McCall7f416cc2015-09-08 08:05:57 +00008000 CGBuilderTy &Builder = CGF.Builder;
8001 Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
8002 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
8003
8004 auto TypeInfo = getContext().getTypeInfoInChars(Ty);
8005
8006 Address ArgAddr = Address::invalid();
8007 CharUnits Stride;
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008008 switch (AI.getKind()) {
8009 case ABIArgInfo::Expand:
John McCallf26e73d2016-03-11 04:30:43 +00008010 case ABIArgInfo::CoerceAndExpand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00008011 case ABIArgInfo::InAlloca:
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008012 llvm_unreachable("Unsupported ABI kind for va_arg");
8013
John McCall7f416cc2015-09-08 08:05:57 +00008014 case ABIArgInfo::Extend: {
8015 Stride = SlotSize;
8016 CharUnits Offset = SlotSize - TypeInfo.first;
8017 ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008018 break;
John McCall7f416cc2015-09-08 08:05:57 +00008019 }
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008020
John McCall7f416cc2015-09-08 08:05:57 +00008021 case ABIArgInfo::Direct: {
8022 auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
Rui Ueyama83aa9792016-01-14 21:00:27 +00008023 Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008024 ArgAddr = Addr;
8025 break;
John McCall7f416cc2015-09-08 08:05:57 +00008026 }
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008027
8028 case ABIArgInfo::Indirect:
John McCall7f416cc2015-09-08 08:05:57 +00008029 Stride = SlotSize;
8030 ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
8031 ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
8032 TypeInfo.second);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008033 break;
8034
8035 case ABIArgInfo::Ignore:
John McCall7f416cc2015-09-08 08:05:57 +00008036 return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008037 }
8038
8039 // Update VAList.
John McCall7f416cc2015-09-08 08:05:57 +00008040 llvm::Value *NextPtr =
8041 Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next");
8042 Builder.CreateStore(NextPtr, VAListAddr);
Jakob Stoklund Olesen303caed2013-06-05 03:00:18 +00008043
John McCall7f416cc2015-09-08 08:05:57 +00008044 return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00008045}
8046
8047void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
8048 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
Aaron Ballmanec47bc22014-03-17 18:10:01 +00008049 for (auto &I : FI.arguments())
8050 I.info = classifyType(I.type, 16 * 8);
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00008051}
8052
8053namespace {
8054class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
8055public:
8056 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
8057 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
Roman Divackyf02c9942014-02-24 18:46:27 +00008058
Craig Topper4f12f102014-03-12 06:41:41 +00008059 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
Roman Divackyf02c9942014-02-24 18:46:27 +00008060 return 14;
8061 }
8062
8063 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Craig Topper4f12f102014-03-12 06:41:41 +00008064 llvm::Value *Address) const override;
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00008065};
8066} // end anonymous namespace
8067
Roman Divackyf02c9942014-02-24 18:46:27 +00008068bool
8069SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
8070 llvm::Value *Address) const {
8071 // This is calculated from the LLVM and GCC tables and verified
8072 // against gcc output. AFAIK all ABIs use the same encoding.
8073
8074 CodeGen::CGBuilderTy &Builder = CGF.Builder;
8075
8076 llvm::IntegerType *i8 = CGF.Int8Ty;
8077 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
8078 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
8079
8080 // 0-31: the 8-byte general-purpose registers
8081 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
8082
8083 // 32-63: f0-31, the 4-byte floating-point registers
8084 AssignToArrayRange(Builder, Address, Four8, 32, 63);
8085
8086 // Y = 64
8087 // PSR = 65
8088 // WIM = 66
8089 // TBR = 67
8090 // PC = 68
8091 // NPC = 69
8092 // FSR = 70
8093 // CSR = 71
8094 AssignToArrayRange(Builder, Address, Eight8, 64, 71);
Eric Christopher7565e0d2015-05-29 23:09:49 +00008095
Roman Divackyf02c9942014-02-24 18:46:27 +00008096 // 72-87: d0-15, the 8-byte floating-point registers
8097 AssignToArrayRange(Builder, Address, Eight8, 72, 87);
8098
8099 return false;
8100}
8101
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00008102
Robert Lytton0e076492013-08-13 09:43:10 +00008103//===----------------------------------------------------------------------===//
Robert Lyttond21e2d72014-03-03 13:45:29 +00008104// XCore ABI Implementation
Robert Lytton0e076492013-08-13 09:43:10 +00008105//===----------------------------------------------------------------------===//
Robert Lytton844aeeb2014-05-02 09:33:20 +00008106
Robert Lytton0e076492013-08-13 09:43:10 +00008107namespace {
Robert Lytton844aeeb2014-05-02 09:33:20 +00008108
8109/// A SmallStringEnc instance is used to build up the TypeString by passing
8110/// it by reference between functions that append to it.
8111typedef llvm::SmallString<128> SmallStringEnc;
8112
8113/// TypeStringCache caches the meta encodings of Types.
8114///
8115/// The reason for caching TypeStrings is two fold:
8116/// 1. To cache a type's encoding for later uses;
8117/// 2. As a means to break recursive member type inclusion.
8118///
8119/// A cache Entry can have a Status of:
8120/// NonRecursive: The type encoding is not recursive;
8121/// Recursive: The type encoding is recursive;
8122/// Incomplete: An incomplete TypeString;
8123/// IncompleteUsed: An incomplete TypeString that has been used in a
8124/// Recursive type encoding.
8125///
8126/// A NonRecursive entry will have all of its sub-members expanded as fully
8127/// as possible. Whilst it may contain types which are recursive, the type
8128/// itself is not recursive and thus its encoding may be safely used whenever
8129/// the type is encountered.
8130///
8131/// A Recursive entry will have all of its sub-members expanded as fully as
8132/// possible. The type itself is recursive and it may contain other types which
8133/// are recursive. The Recursive encoding must not be used during the expansion
8134/// of a recursive type's recursive branch. For simplicity the code uses
8135/// IncompleteCount to reject all usage of Recursive encodings for member types.
8136///
8137/// An Incomplete entry is always a RecordType and only encodes its
8138/// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
8139/// are placed into the cache during type expansion as a means to identify and
8140/// handle recursive inclusion of types as sub-members. If there is recursion
8141/// the entry becomes IncompleteUsed.
8142///
8143/// During the expansion of a RecordType's members:
8144///
8145/// If the cache contains a NonRecursive encoding for the member type, the
8146/// cached encoding is used;
8147///
8148/// If the cache contains a Recursive encoding for the member type, the
8149/// cached encoding is 'Swapped' out, as it may be incorrect, and...
8150///
8151/// If the member is a RecordType, an Incomplete encoding is placed into the
8152/// cache to break potential recursive inclusion of itself as a sub-member;
8153///
8154/// Once a member RecordType has been expanded, its temporary incomplete
8155/// entry is removed from the cache. If a Recursive encoding was swapped out
8156/// it is swapped back in;
8157///
8158/// If an incomplete entry is used to expand a sub-member, the incomplete
8159/// entry is marked as IncompleteUsed. The cache keeps count of how many
8160/// IncompleteUsed entries it currently contains in IncompleteUsedCount;
8161///
8162/// If a member's encoding is found to be a NonRecursive or Recursive viz:
8163/// IncompleteUsedCount==0, the member's encoding is added to the cache.
8164/// Else the member is part of a recursive type and thus the recursion has
8165/// been exited too soon for the encoding to be correct for the member.
8166///
8167class TypeStringCache {
8168 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
8169 struct Entry {
8170 std::string Str; // The encoded TypeString for the type.
8171 enum Status State; // Information about the encoding in 'Str'.
8172 std::string Swapped; // A temporary place holder for a Recursive encoding
8173 // during the expansion of RecordType's members.
8174 };
8175 std::map<const IdentifierInfo *, struct Entry> Map;
8176 unsigned IncompleteCount; // Number of Incomplete entries in the Map.
8177 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
8178public:
Hans Wennborg4afe5042015-07-22 20:46:26 +00008179 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
Robert Lytton844aeeb2014-05-02 09:33:20 +00008180 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
8181 bool removeIncomplete(const IdentifierInfo *ID);
8182 void addIfComplete(const IdentifierInfo *ID, StringRef Str,
8183 bool IsRecursive);
8184 StringRef lookupStr(const IdentifierInfo *ID);
8185};
8186
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008187/// TypeString encodings for enum & union fields must be order.
Robert Lytton844aeeb2014-05-02 09:33:20 +00008188/// FieldEncoding is a helper for this ordering process.
8189class FieldEncoding {
8190 bool HasName;
8191 std::string Enc;
8192public:
Hans Wennborg4afe5042015-07-22 20:46:26 +00008193 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
Malcolm Parsonsf76f6502016-11-02 10:39:27 +00008194 StringRef str() { return Enc; }
Robert Lytton844aeeb2014-05-02 09:33:20 +00008195 bool operator<(const FieldEncoding &rhs) const {
8196 if (HasName != rhs.HasName) return HasName;
8197 return Enc < rhs.Enc;
8198 }
8199};
8200
Robert Lytton7d1db152013-08-19 09:46:39 +00008201class XCoreABIInfo : public DefaultABIInfo {
8202public:
8203 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
John McCall7f416cc2015-09-08 08:05:57 +00008204 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8205 QualType Ty) const override;
Robert Lytton7d1db152013-08-19 09:46:39 +00008206};
8207
Robert Lyttond21e2d72014-03-03 13:45:29 +00008208class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
Robert Lytton844aeeb2014-05-02 09:33:20 +00008209 mutable TypeStringCache TSC;
Robert Lytton0e076492013-08-13 09:43:10 +00008210public:
Robert Lyttond21e2d72014-03-03 13:45:29 +00008211 XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
Robert Lytton7d1db152013-08-19 09:46:39 +00008212 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
Rafael Espindola8dcd6e72014-05-08 15:01:48 +00008213 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
8214 CodeGen::CodeGenModule &M) const override;
Robert Lytton0e076492013-08-13 09:43:10 +00008215};
Robert Lytton844aeeb2014-05-02 09:33:20 +00008216
Robert Lytton2d196952013-10-11 10:29:34 +00008217} // End anonymous namespace.
Robert Lytton0e076492013-08-13 09:43:10 +00008218
James Y Knight29b5f082016-02-24 02:59:33 +00008219// TODO: this implementation is likely now redundant with the default
8220// EmitVAArg.
John McCall7f416cc2015-09-08 08:05:57 +00008221Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8222 QualType Ty) const {
Robert Lytton7d1db152013-08-19 09:46:39 +00008223 CGBuilderTy &Builder = CGF.Builder;
Robert Lytton7d1db152013-08-19 09:46:39 +00008224
Robert Lytton2d196952013-10-11 10:29:34 +00008225 // Get the VAList.
John McCall7f416cc2015-09-08 08:05:57 +00008226 CharUnits SlotSize = CharUnits::fromQuantity(4);
8227 Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
Robert Lytton7d1db152013-08-19 09:46:39 +00008228
Robert Lytton2d196952013-10-11 10:29:34 +00008229 // Handle the argument.
8230 ABIArgInfo AI = classifyArgumentType(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00008231 CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
Robert Lytton2d196952013-10-11 10:29:34 +00008232 llvm::Type *ArgTy = CGT.ConvertType(Ty);
8233 if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
8234 AI.setCoerceToType(ArgTy);
Robert Lytton7d1db152013-08-19 09:46:39 +00008235 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
John McCall7f416cc2015-09-08 08:05:57 +00008236
8237 Address Val = Address::invalid();
8238 CharUnits ArgSize = CharUnits::Zero();
Robert Lytton7d1db152013-08-19 09:46:39 +00008239 switch (AI.getKind()) {
Robert Lytton7d1db152013-08-19 09:46:39 +00008240 case ABIArgInfo::Expand:
John McCallf26e73d2016-03-11 04:30:43 +00008241 case ABIArgInfo::CoerceAndExpand:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00008242 case ABIArgInfo::InAlloca:
Robert Lytton7d1db152013-08-19 09:46:39 +00008243 llvm_unreachable("Unsupported ABI kind for va_arg");
8244 case ABIArgInfo::Ignore:
John McCall7f416cc2015-09-08 08:05:57 +00008245 Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
8246 ArgSize = CharUnits::Zero();
Robert Lytton2d196952013-10-11 10:29:34 +00008247 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00008248 case ABIArgInfo::Extend:
8249 case ABIArgInfo::Direct:
John McCall7f416cc2015-09-08 08:05:57 +00008250 Val = Builder.CreateBitCast(AP, ArgPtrTy);
8251 ArgSize = CharUnits::fromQuantity(
8252 getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
Rui Ueyama83aa9792016-01-14 21:00:27 +00008253 ArgSize = ArgSize.alignTo(SlotSize);
Robert Lytton2d196952013-10-11 10:29:34 +00008254 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00008255 case ABIArgInfo::Indirect:
John McCall7f416cc2015-09-08 08:05:57 +00008256 Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
8257 Val = Address(Builder.CreateLoad(Val), TypeAlign);
8258 ArgSize = SlotSize;
Robert Lytton2d196952013-10-11 10:29:34 +00008259 break;
Robert Lytton7d1db152013-08-19 09:46:39 +00008260 }
Robert Lytton2d196952013-10-11 10:29:34 +00008261
8262 // Increment the VAList.
John McCall7f416cc2015-09-08 08:05:57 +00008263 if (!ArgSize.isZero()) {
8264 llvm::Value *APN =
8265 Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize);
8266 Builder.CreateStore(APN, VAListAddr);
Robert Lytton2d196952013-10-11 10:29:34 +00008267 }
John McCall7f416cc2015-09-08 08:05:57 +00008268
Robert Lytton2d196952013-10-11 10:29:34 +00008269 return Val;
Robert Lytton7d1db152013-08-19 09:46:39 +00008270}
Robert Lytton0e076492013-08-13 09:43:10 +00008271
Robert Lytton844aeeb2014-05-02 09:33:20 +00008272/// During the expansion of a RecordType, an incomplete TypeString is placed
8273/// into the cache as a means to identify and break recursion.
8274/// If there is a Recursive encoding in the cache, it is swapped out and will
8275/// be reinserted by removeIncomplete().
8276/// All other types of encoding should have been used rather than arriving here.
8277void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
8278 std::string StubEnc) {
8279 if (!ID)
8280 return;
8281 Entry &E = Map[ID];
8282 assert( (E.Str.empty() || E.State == Recursive) &&
8283 "Incorrectly use of addIncomplete");
8284 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
8285 E.Swapped.swap(E.Str); // swap out the Recursive
8286 E.Str.swap(StubEnc);
8287 E.State = Incomplete;
8288 ++IncompleteCount;
8289}
8290
8291/// Once the RecordType has been expanded, the temporary incomplete TypeString
8292/// must be removed from the cache.
8293/// If a Recursive was swapped out by addIncomplete(), it will be replaced.
8294/// Returns true if the RecordType was defined recursively.
8295bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
8296 if (!ID)
8297 return false;
8298 auto I = Map.find(ID);
8299 assert(I != Map.end() && "Entry not present");
8300 Entry &E = I->second;
8301 assert( (E.State == Incomplete ||
8302 E.State == IncompleteUsed) &&
8303 "Entry must be an incomplete type");
8304 bool IsRecursive = false;
8305 if (E.State == IncompleteUsed) {
8306 // We made use of our Incomplete encoding, thus we are recursive.
8307 IsRecursive = true;
8308 --IncompleteUsedCount;
8309 }
8310 if (E.Swapped.empty())
8311 Map.erase(I);
8312 else {
8313 // Swap the Recursive back.
8314 E.Swapped.swap(E.Str);
8315 E.Swapped.clear();
8316 E.State = Recursive;
8317 }
8318 --IncompleteCount;
8319 return IsRecursive;
8320}
8321
8322/// Add the encoded TypeString to the cache only if it is NonRecursive or
8323/// Recursive (viz: all sub-members were expanded as fully as possible).
8324void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
8325 bool IsRecursive) {
8326 if (!ID || IncompleteUsedCount)
8327 return; // No key or it is is an incomplete sub-type so don't add.
8328 Entry &E = Map[ID];
8329 if (IsRecursive && !E.Str.empty()) {
8330 assert(E.State==Recursive && E.Str.size() == Str.size() &&
8331 "This is not the same Recursive entry");
8332 // The parent container was not recursive after all, so we could have used
8333 // this Recursive sub-member entry after all, but we assumed the worse when
8334 // we started viz: IncompleteCount!=0.
8335 return;
8336 }
8337 assert(E.Str.empty() && "Entry already present");
8338 E.Str = Str.str();
8339 E.State = IsRecursive? Recursive : NonRecursive;
8340}
8341
8342/// Return a cached TypeString encoding for the ID. If there isn't one, or we
8343/// are recursively expanding a type (IncompleteCount != 0) and the cached
8344/// encoding is Recursive, return an empty StringRef.
8345StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
8346 if (!ID)
8347 return StringRef(); // We have no key.
8348 auto I = Map.find(ID);
8349 if (I == Map.end())
8350 return StringRef(); // We have no encoding.
8351 Entry &E = I->second;
8352 if (E.State == Recursive && IncompleteCount)
8353 return StringRef(); // We don't use Recursive encodings for member types.
8354
8355 if (E.State == Incomplete) {
8356 // The incomplete type is being used to break out of recursion.
8357 E.State = IncompleteUsed;
8358 ++IncompleteUsedCount;
8359 }
Malcolm Parsonsf76f6502016-11-02 10:39:27 +00008360 return E.Str;
Robert Lytton844aeeb2014-05-02 09:33:20 +00008361}
8362
8363/// The XCore ABI includes a type information section that communicates symbol
8364/// type information to the linker. The linker uses this information to verify
8365/// safety/correctness of things such as array bound and pointers et al.
8366/// The ABI only requires C (and XC) language modules to emit TypeStrings.
8367/// This type information (TypeString) is emitted into meta data for all global
8368/// symbols: definitions, declarations, functions & variables.
8369///
8370/// The TypeString carries type, qualifier, name, size & value details.
8371/// Please see 'Tools Development Guide' section 2.16.2 for format details:
Eric Christopher7565e0d2015-05-29 23:09:49 +00008372/// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
Robert Lytton844aeeb2014-05-02 09:33:20 +00008373/// The output is tested by test/CodeGen/xcore-stringtype.c.
8374///
8375static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
8376 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
8377
8378/// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
8379void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
8380 CodeGen::CodeGenModule &CGM) const {
8381 SmallStringEnc Enc;
8382 if (getTypeString(Enc, D, CGM, TSC)) {
8383 llvm::LLVMContext &Ctx = CGM.getModule().getContext();
Benjamin Kramer30934732016-07-02 11:41:41 +00008384 llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
8385 llvm::MDString::get(Ctx, Enc.str())};
Robert Lytton844aeeb2014-05-02 09:33:20 +00008386 llvm::NamedMDNode *MD =
8387 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
8388 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
8389 }
8390}
8391
Xiuli Pan972bea82016-03-24 03:57:17 +00008392//===----------------------------------------------------------------------===//
8393// SPIR ABI Implementation
8394//===----------------------------------------------------------------------===//
8395
8396namespace {
8397class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
8398public:
8399 SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
8400 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00008401 unsigned getOpenCLKernelCallingConv() const override;
Xiuli Pan972bea82016-03-24 03:57:17 +00008402};
Pekka Jaaskelainenfc2629a2017-06-01 07:18:49 +00008403
Xiuli Pan972bea82016-03-24 03:57:17 +00008404} // End anonymous namespace.
8405
Pekka Jaaskelainenfc2629a2017-06-01 07:18:49 +00008406namespace clang {
8407namespace CodeGen {
8408void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
8409 DefaultABIInfo SPIRABI(CGM.getTypes());
8410 SPIRABI.computeInfo(FI);
8411}
8412}
8413}
8414
Nikolay Haustov8c6538b2016-06-30 09:06:33 +00008415unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
8416 return llvm::CallingConv::SPIR_KERNEL;
8417}
8418
Robert Lytton844aeeb2014-05-02 09:33:20 +00008419static bool appendType(SmallStringEnc &Enc, QualType QType,
8420 const CodeGen::CodeGenModule &CGM,
8421 TypeStringCache &TSC);
8422
8423/// Helper function for appendRecordType().
Eric Christopher7565e0d2015-05-29 23:09:49 +00008424/// Builds a SmallVector containing the encoded field types in declaration
8425/// order.
Robert Lytton844aeeb2014-05-02 09:33:20 +00008426static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
8427 const RecordDecl *RD,
8428 const CodeGen::CodeGenModule &CGM,
8429 TypeStringCache &TSC) {
Hans Wennborga302cd92014-08-21 16:06:57 +00008430 for (const auto *Field : RD->fields()) {
Robert Lytton844aeeb2014-05-02 09:33:20 +00008431 SmallStringEnc Enc;
8432 Enc += "m(";
Hans Wennborga302cd92014-08-21 16:06:57 +00008433 Enc += Field->getName();
Robert Lytton844aeeb2014-05-02 09:33:20 +00008434 Enc += "){";
Hans Wennborga302cd92014-08-21 16:06:57 +00008435 if (Field->isBitField()) {
Robert Lytton844aeeb2014-05-02 09:33:20 +00008436 Enc += "b(";
8437 llvm::raw_svector_ostream OS(Enc);
Hans Wennborga302cd92014-08-21 16:06:57 +00008438 OS << Field->getBitWidthValue(CGM.getContext());
Robert Lytton844aeeb2014-05-02 09:33:20 +00008439 Enc += ':';
8440 }
Hans Wennborga302cd92014-08-21 16:06:57 +00008441 if (!appendType(Enc, Field->getType(), CGM, TSC))
Robert Lytton844aeeb2014-05-02 09:33:20 +00008442 return false;
Hans Wennborga302cd92014-08-21 16:06:57 +00008443 if (Field->isBitField())
Robert Lytton844aeeb2014-05-02 09:33:20 +00008444 Enc += ')';
8445 Enc += '}';
Benjamin Kramer3204b152015-05-29 19:42:19 +00008446 FE.emplace_back(!Field->getName().empty(), Enc);
Robert Lytton844aeeb2014-05-02 09:33:20 +00008447 }
8448 return true;
8449}
8450
8451/// Appends structure and union types to Enc and adds encoding to cache.
8452/// Recursively calls appendType (via extractFieldType) for each field.
8453/// Union types have their fields ordered according to the ABI.
8454static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
8455 const CodeGen::CodeGenModule &CGM,
8456 TypeStringCache &TSC, const IdentifierInfo *ID) {
8457 // Append the cached TypeString if we have one.
8458 StringRef TypeString = TSC.lookupStr(ID);
8459 if (!TypeString.empty()) {
8460 Enc += TypeString;
8461 return true;
8462 }
8463
8464 // Start to emit an incomplete TypeString.
8465 size_t Start = Enc.size();
8466 Enc += (RT->isUnionType()? 'u' : 's');
8467 Enc += '(';
8468 if (ID)
8469 Enc += ID->getName();
8470 Enc += "){";
8471
8472 // We collect all encoded fields and order as necessary.
8473 bool IsRecursive = false;
Robert Lytton844aeeb2014-05-02 09:33:20 +00008474 const RecordDecl *RD = RT->getDecl()->getDefinition();
8475 if (RD && !RD->field_empty()) {
8476 // An incomplete TypeString stub is placed in the cache for this RecordType
8477 // so that recursive calls to this RecordType will use it whilst building a
8478 // complete TypeString for this RecordType.
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008479 SmallVector<FieldEncoding, 16> FE;
Robert Lytton844aeeb2014-05-02 09:33:20 +00008480 std::string StubEnc(Enc.substr(Start).str());
8481 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString.
8482 TSC.addIncomplete(ID, std::move(StubEnc));
8483 if (!extractFieldType(FE, RD, CGM, TSC)) {
8484 (void) TSC.removeIncomplete(ID);
8485 return false;
8486 }
8487 IsRecursive = TSC.removeIncomplete(ID);
8488 // The ABI requires unions to be sorted but not structures.
8489 // See FieldEncoding::operator< for sort algorithm.
8490 if (RT->isUnionType())
8491 std::sort(FE.begin(), FE.end());
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008492 // We can now complete the TypeString.
8493 unsigned E = FE.size();
Robert Lytton844aeeb2014-05-02 09:33:20 +00008494 for (unsigned I = 0; I != E; ++I) {
8495 if (I)
8496 Enc += ',';
8497 Enc += FE[I].str();
8498 }
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008499 }
Robert Lytton844aeeb2014-05-02 09:33:20 +00008500 Enc += '}';
8501 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
8502 return true;
8503}
8504
8505/// Appends enum types to Enc and adds the encoding to the cache.
8506static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
8507 TypeStringCache &TSC,
8508 const IdentifierInfo *ID) {
8509 // Append the cached TypeString if we have one.
8510 StringRef TypeString = TSC.lookupStr(ID);
8511 if (!TypeString.empty()) {
8512 Enc += TypeString;
8513 return true;
8514 }
8515
8516 size_t Start = Enc.size();
8517 Enc += "e(";
8518 if (ID)
8519 Enc += ID->getName();
8520 Enc += "){";
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008521
8522 // We collect all encoded enumerations and order them alphanumerically.
Robert Lytton844aeeb2014-05-02 09:33:20 +00008523 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008524 SmallVector<FieldEncoding, 16> FE;
8525 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
8526 ++I) {
8527 SmallStringEnc EnumEnc;
8528 EnumEnc += "m(";
8529 EnumEnc += I->getName();
8530 EnumEnc += "){";
8531 I->getInitVal().toString(EnumEnc);
8532 EnumEnc += '}';
8533 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
8534 }
8535 std::sort(FE.begin(), FE.end());
8536 unsigned E = FE.size();
8537 for (unsigned I = 0; I != E; ++I) {
8538 if (I)
Robert Lytton844aeeb2014-05-02 09:33:20 +00008539 Enc += ',';
Robert Lyttondb8c1cb2014-05-20 07:19:33 +00008540 Enc += FE[I].str();
Robert Lytton844aeeb2014-05-02 09:33:20 +00008541 }
8542 }
8543 Enc += '}';
8544 TSC.addIfComplete(ID, Enc.substr(Start), false);
8545 return true;
8546}
8547
8548/// Appends type's qualifier to Enc.
8549/// This is done prior to appending the type's encoding.
8550static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
8551 // Qualifiers are emitted in alphabetical order.
Craig Topper273dbc62015-10-18 05:29:26 +00008552 static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
Robert Lytton844aeeb2014-05-02 09:33:20 +00008553 int Lookup = 0;
8554 if (QT.isConstQualified())
8555 Lookup += 1<<0;
8556 if (QT.isRestrictQualified())
8557 Lookup += 1<<1;
8558 if (QT.isVolatileQualified())
8559 Lookup += 1<<2;
8560 Enc += Table[Lookup];
8561}
8562
8563/// Appends built-in types to Enc.
8564static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
8565 const char *EncType;
8566 switch (BT->getKind()) {
8567 case BuiltinType::Void:
8568 EncType = "0";
8569 break;
8570 case BuiltinType::Bool:
8571 EncType = "b";
8572 break;
8573 case BuiltinType::Char_U:
8574 EncType = "uc";
8575 break;
8576 case BuiltinType::UChar:
8577 EncType = "uc";
8578 break;
8579 case BuiltinType::SChar:
8580 EncType = "sc";
8581 break;
8582 case BuiltinType::UShort:
8583 EncType = "us";
8584 break;
8585 case BuiltinType::Short:
8586 EncType = "ss";
8587 break;
8588 case BuiltinType::UInt:
8589 EncType = "ui";
8590 break;
8591 case BuiltinType::Int:
8592 EncType = "si";
8593 break;
8594 case BuiltinType::ULong:
8595 EncType = "ul";
8596 break;
8597 case BuiltinType::Long:
8598 EncType = "sl";
8599 break;
8600 case BuiltinType::ULongLong:
8601 EncType = "ull";
8602 break;
8603 case BuiltinType::LongLong:
8604 EncType = "sll";
8605 break;
8606 case BuiltinType::Float:
8607 EncType = "ft";
8608 break;
8609 case BuiltinType::Double:
8610 EncType = "d";
8611 break;
8612 case BuiltinType::LongDouble:
8613 EncType = "ld";
8614 break;
8615 default:
8616 return false;
8617 }
8618 Enc += EncType;
8619 return true;
8620}
8621
8622/// Appends a pointer encoding to Enc before calling appendType for the pointee.
8623static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
8624 const CodeGen::CodeGenModule &CGM,
8625 TypeStringCache &TSC) {
8626 Enc += "p(";
8627 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
8628 return false;
8629 Enc += ')';
8630 return true;
8631}
8632
8633/// Appends array encoding to Enc before calling appendType for the element.
Robert Lytton6adb20f2014-06-05 09:06:21 +00008634static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
8635 const ArrayType *AT,
Robert Lytton844aeeb2014-05-02 09:33:20 +00008636 const CodeGen::CodeGenModule &CGM,
8637 TypeStringCache &TSC, StringRef NoSizeEnc) {
8638 if (AT->getSizeModifier() != ArrayType::Normal)
8639 return false;
8640 Enc += "a(";
8641 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
8642 CAT->getSize().toStringUnsigned(Enc);
8643 else
8644 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
8645 Enc += ':';
Robert Lytton6adb20f2014-06-05 09:06:21 +00008646 // The Qualifiers should be attached to the type rather than the array.
8647 appendQualifier(Enc, QT);
Robert Lytton844aeeb2014-05-02 09:33:20 +00008648 if (!appendType(Enc, AT->getElementType(), CGM, TSC))
8649 return false;
8650 Enc += ')';
8651 return true;
8652}
8653
8654/// Appends a function encoding to Enc, calling appendType for the return type
8655/// and the arguments.
8656static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
8657 const CodeGen::CodeGenModule &CGM,
8658 TypeStringCache &TSC) {
8659 Enc += "f{";
8660 if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
8661 return false;
8662 Enc += "}(";
8663 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
8664 // N.B. we are only interested in the adjusted param types.
8665 auto I = FPT->param_type_begin();
8666 auto E = FPT->param_type_end();
8667 if (I != E) {
8668 do {
8669 if (!appendType(Enc, *I, CGM, TSC))
8670 return false;
8671 ++I;
8672 if (I != E)
8673 Enc += ',';
8674 } while (I != E);
8675 if (FPT->isVariadic())
8676 Enc += ",va";
8677 } else {
8678 if (FPT->isVariadic())
8679 Enc += "va";
8680 else
8681 Enc += '0';
8682 }
8683 }
8684 Enc += ')';
8685 return true;
8686}
8687
8688/// Handles the type's qualifier before dispatching a call to handle specific
8689/// type encodings.
8690static bool appendType(SmallStringEnc &Enc, QualType QType,
8691 const CodeGen::CodeGenModule &CGM,
8692 TypeStringCache &TSC) {
8693
8694 QualType QT = QType.getCanonicalType();
8695
Robert Lytton6adb20f2014-06-05 09:06:21 +00008696 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
8697 // The Qualifiers should be attached to the type rather than the array.
8698 // Thus we don't call appendQualifier() here.
8699 return appendArrayType(Enc, QT, AT, CGM, TSC, "");
8700
Robert Lytton844aeeb2014-05-02 09:33:20 +00008701 appendQualifier(Enc, QT);
8702
8703 if (const BuiltinType *BT = QT->getAs<BuiltinType>())
8704 return appendBuiltinType(Enc, BT);
8705
Robert Lytton844aeeb2014-05-02 09:33:20 +00008706 if (const PointerType *PT = QT->getAs<PointerType>())
8707 return appendPointerType(Enc, PT, CGM, TSC);
8708
8709 if (const EnumType *ET = QT->getAs<EnumType>())
8710 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
8711
8712 if (const RecordType *RT = QT->getAsStructureType())
8713 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
8714
8715 if (const RecordType *RT = QT->getAsUnionType())
8716 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
8717
8718 if (const FunctionType *FT = QT->getAs<FunctionType>())
8719 return appendFunctionType(Enc, FT, CGM, TSC);
8720
8721 return false;
8722}
8723
8724static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
8725 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
8726 if (!D)
8727 return false;
8728
8729 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
8730 if (FD->getLanguageLinkage() != CLanguageLinkage)
8731 return false;
8732 return appendType(Enc, FD->getType(), CGM, TSC);
8733 }
8734
8735 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
8736 if (VD->getLanguageLinkage() != CLanguageLinkage)
8737 return false;
8738 QualType QT = VD->getType().getCanonicalType();
8739 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
8740 // Global ArrayTypes are given a size of '*' if the size is unknown.
Robert Lytton6adb20f2014-06-05 09:06:21 +00008741 // The Qualifiers should be attached to the type rather than the array.
8742 // Thus we don't call appendQualifier() here.
8743 return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
Robert Lytton844aeeb2014-05-02 09:33:20 +00008744 }
8745 return appendType(Enc, QT, CGM, TSC);
8746 }
8747 return false;
8748}
8749
8750
Robert Lytton0e076492013-08-13 09:43:10 +00008751//===----------------------------------------------------------------------===//
8752// Driver code
8753//===----------------------------------------------------------------------===//
8754
Rafael Espindola9f834732014-09-19 01:54:22 +00008755bool CodeGenModule::supportsCOMDAT() const {
Xinliang David Li865cfdd2016-05-25 17:25:57 +00008756 return getTriple().supportsCOMDAT();
Rafael Espindola9f834732014-09-19 01:54:22 +00008757}
8758
Chris Lattner2b037972010-07-29 02:01:43 +00008759const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00008760 if (TheTargetCodeGenInfo)
8761 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00008762
Reid Kleckner9305fd12016-04-13 23:37:17 +00008763 // Helper to set the unique_ptr while still keeping the return value.
8764 auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
8765 this->TheTargetCodeGenInfo.reset(P);
8766 return *P;
8767 };
8768
John McCallc8e01702013-04-16 22:48:15 +00008769 const llvm::Triple &Triple = getTarget().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00008770 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00008771 default:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008772 return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00008773
Derek Schuff09338a22012-09-06 17:37:28 +00008774 case llvm::Triple::le32:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008775 return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00008776 case llvm::Triple::mips:
8777 case llvm::Triple::mipsel:
Petar Jovanovic26a4a402015-07-08 13:07:31 +00008778 if (Triple.getOS() == llvm::Triple::NaCl)
Reid Kleckner9305fd12016-04-13 23:37:17 +00008779 return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
8780 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
Akira Hatanakac4baedd2013-11-11 22:10:46 +00008781
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00008782 case llvm::Triple::mips64:
8783 case llvm::Triple::mips64el:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008784 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanakac4baedd2013-11-11 22:10:46 +00008785
Dylan McKaye8232d72017-02-08 05:09:26 +00008786 case llvm::Triple::avr:
8787 return SetCGInfo(new AVRTargetCodeGenInfo(Types));
8788
Tim Northover25e8a672014-05-24 12:51:25 +00008789 case llvm::Triple::aarch64:
Tim Northover40956e62014-07-23 12:32:58 +00008790 case llvm::Triple::aarch64_be: {
Tim Northover573cbee2014-05-24 12:52:07 +00008791 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
Alp Toker4925ba72014-06-07 23:30:42 +00008792 if (getTarget().getABI() == "darwinpcs")
Tim Northover573cbee2014-05-24 12:52:07 +00008793 Kind = AArch64ABIInfo::DarwinPCS;
Martin Storsjo502de222017-07-13 17:59:14 +00008794 else if (Triple.isOSWindows())
Martin Storsjo1c8af272017-07-20 05:47:06 +00008795 return SetCGInfo(
8796 new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
Tim Northovera2ee4332014-03-29 15:09:45 +00008797
Reid Kleckner9305fd12016-04-13 23:37:17 +00008798 return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
Tim Northovera2ee4332014-03-29 15:09:45 +00008799 }
8800
Dan Gohmanc2853072015-09-03 22:51:53 +00008801 case llvm::Triple::wasm32:
8802 case llvm::Triple::wasm64:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008803 return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types));
Dan Gohmanc2853072015-09-03 22:51:53 +00008804
Daniel Dunbard59655c2009-09-12 00:59:49 +00008805 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +00008806 case llvm::Triple::armeb:
Daniel Dunbard59655c2009-09-12 00:59:49 +00008807 case llvm::Triple::thumb:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008808 case llvm::Triple::thumbeb: {
8809 if (Triple.getOS() == llvm::Triple::Win32) {
8810 return SetCGInfo(
8811 new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
Sandeep Patel45df3dd2011-04-05 00:23:47 +00008812 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00008813
Reid Kleckner9305fd12016-04-13 23:37:17 +00008814 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
8815 StringRef ABIStr = getTarget().getABI();
8816 if (ABIStr == "apcs-gnu")
8817 Kind = ARMABIInfo::APCS;
8818 else if (ABIStr == "aapcs16")
8819 Kind = ARMABIInfo::AAPCS16_VFP;
8820 else if (CodeGenOpts.FloatABI == "hard" ||
8821 (CodeGenOpts.FloatABI != "soft" &&
Oleg Ranevskyy7232f662016-05-13 14:45:57 +00008822 (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
Rafael Espindola0fa66802016-06-24 21:35:06 +00008823 Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
Oleg Ranevskyy7232f662016-05-13 14:45:57 +00008824 Triple.getEnvironment() == llvm::Triple::EABIHF)))
Reid Kleckner9305fd12016-04-13 23:37:17 +00008825 Kind = ARMABIInfo::AAPCS_VFP;
8826
8827 return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
8828 }
8829
John McCallea8d8bb2010-03-11 00:10:12 +00008830 case llvm::Triple::ppc:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008831 return SetCGInfo(
8832 new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft"));
Roman Divackyd966e722012-05-09 18:22:46 +00008833 case llvm::Triple::ppc64:
Ulrich Weigandb7122372014-07-21 00:48:09 +00008834 if (Triple.isOSBinFormatELF()) {
Ulrich Weigandb7122372014-07-21 00:48:09 +00008835 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
Ulrich Weigand8afad612014-07-28 13:17:52 +00008836 if (getTarget().getABI() == "elfv2")
8837 Kind = PPC64_SVR4_ABIInfo::ELFv2;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00008838 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Hal Finkel415c2a32016-10-02 02:10:45 +00008839 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
Ulrich Weigand8afad612014-07-28 13:17:52 +00008840
Hal Finkel415c2a32016-10-02 02:10:45 +00008841 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
8842 IsSoftFloat));
Ulrich Weigandb7122372014-07-21 00:48:09 +00008843 } else
Reid Kleckner9305fd12016-04-13 23:37:17 +00008844 return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
Ulrich Weigandb7122372014-07-21 00:48:09 +00008845 case llvm::Triple::ppc64le: {
Bill Schmidt778d3872013-07-26 01:36:11 +00008846 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
Ulrich Weigandb7122372014-07-21 00:48:09 +00008847 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00008848 if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
Ulrich Weigand8afad612014-07-28 13:17:52 +00008849 Kind = PPC64_SVR4_ABIInfo::ELFv1;
Hal Finkel0d0a1a52015-03-11 19:14:15 +00008850 bool HasQPX = getTarget().getABI() == "elfv1-qpx";
Hal Finkel415c2a32016-10-02 02:10:45 +00008851 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
Ulrich Weigand8afad612014-07-28 13:17:52 +00008852
Hal Finkel415c2a32016-10-02 02:10:45 +00008853 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
8854 IsSoftFloat));
Ulrich Weigandb7122372014-07-21 00:48:09 +00008855 }
John McCallea8d8bb2010-03-11 00:10:12 +00008856
Peter Collingbournec947aae2012-05-20 23:28:41 +00008857 case llvm::Triple::nvptx:
8858 case llvm::Triple::nvptx64:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008859 return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00008860
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00008861 case llvm::Triple::msp430:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008862 return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00008863
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00008864 case llvm::Triple::systemz: {
8865 bool HasVector = getTarget().getABI() == "vector";
Reid Kleckner9305fd12016-04-13 23:37:17 +00008866 return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector));
Ulrich Weigand66ff51b2015-05-05 19:35:52 +00008867 }
Ulrich Weigand47445072013-05-06 16:26:41 +00008868
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00008869 case llvm::Triple::tce:
Pekka Jaaskelainen67354482016-11-16 15:22:31 +00008870 case llvm::Triple::tcele:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008871 return SetCGInfo(new TCETargetCodeGenInfo(Types));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00008872
Eli Friedman33465822011-07-08 23:31:17 +00008873 case llvm::Triple::x86: {
John McCall1fe2a8c2013-06-18 02:46:29 +00008874 bool IsDarwinVectorABI = Triple.isOSDarwin();
Michael Kupersteindc745202015-10-19 07:52:25 +00008875 bool RetSmallStructInRegABI =
John McCall1fe2a8c2013-06-18 02:46:29 +00008876 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
Saleem Abdulrasoolec5c6242014-11-23 02:16:24 +00008877 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00008878
John McCall1fe2a8c2013-06-18 02:46:29 +00008879 if (Triple.getOS() == llvm::Triple::Win32) {
Reid Kleckner9305fd12016-04-13 23:37:17 +00008880 return SetCGInfo(new WinX86_32TargetCodeGenInfo(
8881 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
8882 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
John McCall1fe2a8c2013-06-18 02:46:29 +00008883 } else {
Reid Kleckner9305fd12016-04-13 23:37:17 +00008884 return SetCGInfo(new X86_32TargetCodeGenInfo(
8885 Types, IsDarwinVectorABI, RetSmallStructInRegABI,
8886 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
8887 CodeGenOpts.FloatABI == "soft"));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00008888 }
Eli Friedman33465822011-07-08 23:31:17 +00008889 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00008890
Eli Friedmanbfd5add2011-12-02 00:11:43 +00008891 case llvm::Triple::x86_64: {
Ahmed Bougachad39a4152015-06-22 21:30:39 +00008892 StringRef ABI = getTarget().getABI();
Reid Kleckner9305fd12016-04-13 23:37:17 +00008893 X86AVXABILevel AVXLevel =
8894 (ABI == "avx512"
8895 ? X86AVXABILevel::AVX512
8896 : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
Ahmed Bougachad39a4152015-06-22 21:30:39 +00008897
Chris Lattner04dc9572010-08-31 16:44:54 +00008898 switch (Triple.getOS()) {
8899 case llvm::Triple::Win32:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008900 return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
Alex Rosenberg12207fa2015-01-27 14:47:44 +00008901 case llvm::Triple::PS4:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008902 return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel));
Chris Lattner04dc9572010-08-31 16:44:54 +00008903 default:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008904 return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
Chris Lattner04dc9572010-08-31 16:44:54 +00008905 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00008906 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00008907 case llvm::Triple::hexagon:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008908 return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
Jacques Pienaard964cc22016-03-28 21:02:54 +00008909 case llvm::Triple::lanai:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008910 return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
Matt Arsenault43fae6c2014-12-04 20:38:18 +00008911 case llvm::Triple::r600:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008912 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
Tom Stellardd8e38a32015-01-06 20:34:47 +00008913 case llvm::Triple::amdgcn:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008914 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
Chris Dewhurst7e7ee962016-06-08 14:47:25 +00008915 case llvm::Triple::sparc:
8916 return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
Jakob Stoklund Olesend28ab7e2013-05-27 21:48:25 +00008917 case llvm::Triple::sparcv9:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008918 return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
Robert Lytton0e076492013-08-13 09:43:10 +00008919 case llvm::Triple::xcore:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008920 return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
Xiuli Pan972bea82016-03-24 03:57:17 +00008921 case llvm::Triple::spir:
8922 case llvm::Triple::spir64:
Reid Kleckner9305fd12016-04-13 23:37:17 +00008923 return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
Eli Friedmanbfd5add2011-12-02 00:11:43 +00008924 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00008925}
Yaxun Liuc2a87a02017-10-14 12:23:50 +00008926
8927/// Create an OpenCL kernel for an enqueued block.
8928///
8929/// The kernel has the same function type as the block invoke function. Its
8930/// name is the name of the block invoke function postfixed with "_kernel".
8931/// It simply calls the block invoke function then returns.
8932llvm::Function *
8933TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
8934 llvm::Function *Invoke,
8935 llvm::Value *BlockLiteral) const {
8936 auto *InvokeFT = Invoke->getFunctionType();
8937 llvm::SmallVector<llvm::Type *, 2> ArgTys;
8938 for (auto &P : InvokeFT->params())
8939 ArgTys.push_back(P);
8940 auto &C = CGF.getLLVMContext();
8941 std::string Name = Invoke->getName().str() + "_kernel";
8942 auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
8943 auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
8944 &CGF.CGM.getModule());
8945 auto IP = CGF.Builder.saveIP();
8946 auto *BB = llvm::BasicBlock::Create(C, "entry", F);
8947 auto &Builder = CGF.Builder;
8948 Builder.SetInsertPoint(BB);
8949 llvm::SmallVector<llvm::Value *, 2> Args;
8950 for (auto &A : F->args())
8951 Args.push_back(&A);
8952 Builder.CreateCall(Invoke, Args);
8953 Builder.CreateRetVoid();
8954 Builder.restoreIP(IP);
8955 return F;
8956}
8957
8958/// Create an OpenCL kernel for an enqueued block.
8959///
8960/// The type of the first argument (the block literal) is the struct type
8961/// of the block literal instead of a pointer type. The first argument
8962/// (block literal) is passed directly by value to the kernel. The kernel
8963/// allocates the same type of struct on stack and stores the block literal
8964/// to it and passes its pointer to the block invoke function. The kernel
8965/// has "enqueued-block" function attribute and kernel argument metadata.
8966llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
8967 CodeGenFunction &CGF, llvm::Function *Invoke,
8968 llvm::Value *BlockLiteral) const {
8969 auto &Builder = CGF.Builder;
8970 auto &C = CGF.getLLVMContext();
8971
8972 auto *BlockTy = BlockLiteral->getType()->getPointerElementType();
8973 auto *InvokeFT = Invoke->getFunctionType();
8974 llvm::SmallVector<llvm::Type *, 2> ArgTys;
8975 llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
8976 llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
8977 llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
8978 llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
8979 llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
8980 llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
8981
8982 ArgTys.push_back(BlockTy);
8983 ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
8984 AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
8985 ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
8986 ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
8987 AccessQuals.push_back(llvm::MDString::get(C, "none"));
8988 ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
8989 for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
8990 ArgTys.push_back(InvokeFT->getParamType(I));
8991 ArgTys.push_back(BlockTy);
8992 ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
8993 AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
8994 AccessQuals.push_back(llvm::MDString::get(C, "none"));
8995 ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
8996 ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
8997 ArgNames.push_back(
Yaxun Liu98f0c432017-10-14 12:51:52 +00008998 llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
Yaxun Liuc2a87a02017-10-14 12:23:50 +00008999 }
9000 std::string Name = Invoke->getName().str() + "_kernel";
9001 auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
9002 auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
9003 &CGF.CGM.getModule());
9004 F->addFnAttr("enqueued-block");
9005 auto IP = CGF.Builder.saveIP();
9006 auto *BB = llvm::BasicBlock::Create(C, "entry", F);
9007 Builder.SetInsertPoint(BB);
9008 unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy);
9009 auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
9010 BlockPtr->setAlignment(BlockAlign);
9011 Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
9012 auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
9013 llvm::SmallVector<llvm::Value *, 2> Args;
9014 Args.push_back(Cast);
9015 for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I)
9016 Args.push_back(I);
9017 Builder.CreateCall(Invoke, Args);
9018 Builder.CreateRetVoid();
9019 Builder.restoreIP(IP);
9020
9021 F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
9022 F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
9023 F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
9024 F->setMetadata("kernel_arg_base_type",
9025 llvm::MDNode::get(C, ArgBaseTypeNames));
9026 F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
9027 if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
9028 F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
9029
9030 return F;
9031}